DelphiFAQ Home Search:
General :: Programming :: perl
General questions about perl.

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.
Recommended links on this topic:
Featured Article

perl code to generate a time stamp for a unique file name

Question:

I need some perl code to generate a time stamp for a unique file name. Can you help out?

Answer:

Use the function below which returns a date/ time as a string.
The first line shows how to use the function.

$unique_filename = '~/report_' . get_timestamp();
 
 sub get_timestamp {
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
    if ($mon < 10) { $mon = "0$mon"; }
    if ($hour < 10) { $hour = "0$hour"; }
    if ($min < 10) { $min = "0$min"; }
    if ($sec < 10) { $sec = "0$sec"; }
    $year=$year+1900;
 
    return $year . '_' . $mon . '_' . $mday . '__' . $hour . '_' . $min . '_' . $sec;
 }
 

Generated 12:01:42 on Apr 21, 2018