DelphiFAQ Home Search:

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

 

comments3 comments. Current rating: 4 stars (1 votes). Leave comments and/ or rate it.

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;
}

Comments:

2006-10-04, 06:54:51
anonymous from Russian Federation  
...and what if 2 requests at one second occur?
2008-03-28, 17:32:11   (updated: 2008-03-28, 17:35:05)
anonymous from United States  
rating
Shouldn't $mday be zero-justified as well? There should be one more line in there to make it consistent:

if ($mday < 10) { $mday = ''0$mday''; }

-Br.Bill
2008-05-02, 14:53:51
anonymous from United States  
Several problems here:

1) $wday,$yday,$isdst should be 'undef,undef,undef', as they're not used and might throw a warning.
2) $mon needs to be incremented: $mon++; #january is 0, right?
3)

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Washington, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
Leave your comment here:
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.