Dates and Times (Programming Perl) Book Home Programming PerlSearch this book

25.8. Dates and Times

Where possible, use the ISO-8601 standard ("YYYY-MM-DD") to represent dates. Strings like "1987-12-18" can be easily converted into a system-specific value with a module like Date::Parse. A list of time and date values (such as that returned by the built-in localtime function) can be converted to a system-specific representation using Time::Local.

The built-in time function will always return the number of seconds since the beginning of the "epoch", but operating systems differ in their opinions of when that was. On many systems, the epoch began on January 1, 1970, at 00:00:00 UTC, but it began 66 years earlier on Macs, and on VMS it began on November 17, 1858, at 00:00:00. So for portable times you may want to calculate an offset for the epoch:

require Time::Local;
$offset = Time::Local::timegm(0, 0, 0, 1, 0, 70);
The value for $offset in Unix and Windows will always be 0, but on Macs and VMS it may be some large number. $offset can then be added to a Unix time value to get what should be the same value on any system.

A system's representation of the time of day and the calendar date can be controlled in widely different ways. Don't assume the time zone is stored in $ENV{TZ}. Even if it is, don't assume that you can control the time zone through that variable.



Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.