use Time::Local qw( timelocal_posix timegm_posix ); my $time = timelocal_posix( $sec, $min, $hour, $mday, $mon, $year ); my $time = timegm_posix( $sec, $min, $hour, $mday, $mon, $year );
It is worth drawing particular attention to the expected ranges for the values provided. The value for the day of the month is the actual day (i.e. 1..31), while the month is the number of months since January (0..11). This is consistent with the values returned from "localtime()" and "gmtime()".
The one exception is when the value returned from "localtime()" represents an ambiguous local time because of a DST change. See the documentation below for more details.
These functions expect the year value to be the number of years since 1900, which is what the "localtime()" and "gmtime()" built-ins returns.
They perform range checking by default on the input $sec, $min, $hour, $mday, and $mon values and will croak (using "Carp::croak()") if given a value outside the allowed ranges.
While it would be nice to make this the default behavior, that would almost certainly break a lot of code, so you must explicitly import these functions and use them instead of the default "timelocal()" and "timegm()".
You are strongly encouraged to use these functions in any new code which uses this module. It will almost certainly make your code's behavior less surprising.
The default exports of "timelocal()" and "timegm()" do a complicated calculation when given a year value less than 1000. This leads to surprising results in many cases. See ``Year Value Interpretation'' for details.
The "time*_modern()" functions do not do this year munging and simply take the year value as provided.
They perform range checking by default on the input $sec, $min, $hour, $mday, and $mon values and will croak (using "Carp::croak()") if given a value outside the allowed ranges.
They perform range checking by default on the input $sec, $min, $hour, $mday, and $mon values and will croak (using "Carp::croak()") if given a value outside the allowed ranges.
Warning: The year value interpretation that these functions and their nocheck variants use will almost certainly lead to bugs in your code, if not now, then in the future. You are strongly discouraged from using these in new code, and you should convert old code to using either the *_posix or *_modern functions if possible.
If you supply data which is not valid (month 27, second 1,000) the results will be unpredictable (so don't do that).
Note that my benchmarks show that this is just a 3% speed increase over the checked versions, so unless calling "Time::Local" is the hottest spot in your application, using these nocheck variants is unlikely to have much impact on your application.
Strictly speaking, the year should be specified in a form consistent with "localtime()", i.e. the offset from 1900. In order to make the interpretation of the year easier for humans, however, who are more accustomed to seeing years as two-digit or four-digit values, the following conventions are followed:
The scheme above allows interpretation of a wide range of dates, particularly if 4-digit years are used. But it also means that the behavior of your code changes as time passes, because the rolling ``current century'' changes each year.
Both "timelocal()" and "timegm()" croak if given dates outside the supported range.
As of version 5.12.0, perl has stopped using the time implementation of the operating system it's running on. Instead, it has its own implementation of those routines with a safe range of at least +/- 2**52 (about 142 million years)
When given an ambiguous local time, the timelocal() function will always return the epoch for the earlier of the two possible GMT times.
If the "timelocal()" function is given a non-existent local time, it will simply return an epoch value for the time one hour later.
On older versions of perl, negative epoch ("time_t") values, which are not officially supported by the POSIX standards, are known not to work on some systems. These include MacOS (pre-OSX) and Win32.
On systems which do support negative epoch values, this module should be able to cope with dates before the start of the epoch, down the minimum value of time_t for the system.
The "timelocal()" function is implemented using the same cache. We just assume that we're translating a GMT time, and then fudge it when we're done for the timezone and daylight savings arguments. Note that the timezone is evaluated for each date because countries occasionally change their official timezones. Assuming that "localtime()" corrects for these changes, this routine will also be correct.
The current version was written by Graham Barr.
Bugs may be submitted at <https://github.com/houseabsolute/Time-Local/issues>.
There is a mailing list available for users of this distribution, <mailto:datetime@perl.org>.
I am also usually active on IRC as 'autarch' on "irc://irc.perl.org".
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
The full text of the license can be found in the LICENSE file included with this distribution.