Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Perl (http://www.programmingforums.org/forum21.html)
-   -   Date Routine (http://www.programmingforums.org/showthread.php?t=11631)

tbohon Oct 18th, 2006 3:50 PM

Date Routine
 
This may be old news to some but at the same time may be of value to someone else.

I was recently faced with creating a date stamp for the previous day's date. Using some hints found online, I concocted the following routine which works nicely:



### function to build dstamp (date stamp for previous day's date)

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time - 86400);

$mon = $mon + 1;
$year = $year + 1900;

$dstamp = sprintf "%04d%02d%02d", $year, $mon, $mday;

### end of dstamp function



The nice thing is that the value 86400 in the call to 'localtime' is equivalent to one (1) day ... so, by adjusting this value, you can create the same sort of date stamp (format is YYYYMMDD) for any offset.

Enjoy!

Tom

chrisranjana.com Jan 15th, 2007 10:22 AM

Will it take into account Daylight Savings time too ?

suppose yesterday was NON DST and today is a DST ?

Arevos Jan 15th, 2007 11:14 AM

Subtracting 86400 from the time will work most of the time, but not all of the time. If you need accuracy, use a module like DateTime:

:

  1. use DateTime;
  2. my $yesterday = DateTime->now->subtract( days => 1 );



All times are GMT -5. The time now is 10:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC