Neuter socketpair on Stratus VOS
[p5sagit/p5-mst-13.2.git] / lib / Time / Local.pm
CommitLineData
a0d0e21e 1package Time::Local;
3b825e41 2use 5.006;
a0d0e21e 3require Exporter;
4use Carp;
e7ec2331 5use Config;
b75c8c73 6use strict;
326557bd 7use integer;
a0d0e21e 8
e7ec2331 9our $VERSION = '1.04';
b75c8c73 10our @ISA = qw( Exporter );
11our @EXPORT = qw( timegm timelocal );
12our @EXPORT_OK = qw( timegm_nocheck timelocal_nocheck );
a0d0e21e 13
326557bd 14my @MonthDays = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
15
06ef4121 16# Determine breakpoint for rolling century
326557bd 17my $ThisYear = (localtime())[5];
18my $Breakpoint = ($ThisYear + 50) % 100;
19my $NextCentury = $ThisYear - $ThisYear % 100;
20 $NextCentury += 100 if $Breakpoint < 50;
21my $Century = $NextCentury - 100;
22
23my (%Options, %Cheat);
24
25# Determine the EPOC day for this machine
26my $Epoc = 0; $Epoc = _daygm(gmtime(0));
27%Cheat=(); # clear the cache as epoc has changed
28
e7ec2331 29my $MaxInt = ((1<<(8 * $Config{intsize} - 2))-1)*2 + 1;
30my $MaxDay = int(($MaxInt-43200)/86400)-1;
326557bd 31
32
33sub _daygm {
34 $_[3] + ($Cheat{pack("ss",@_[4,5])} ||= do {
35 my $month = ($_[4] + 10) % 12;
36 my $year = $_[5] + 1900 - $month/10;
37 365*$year + $year/4 - $year/100 + $year/400 + ($month*306 + 5)/10 - $Epoc
38 });
39}
40
41
42sub _timegm {
43 $_[0] + 60 * $_[1] + 3600 * $_[2] + 86400 * &_daygm;
44}
9bb8015a 45
e36f48eb 46
9bb8015a 47sub timegm {
326557bd 48 my ($sec,$min,$hour,$mday,$month,$year) = @_;
49
50 if ($year >= 1000) {
51 $year -= 1900;
52 }
53 elsif ($year < 100 and $year >= 0) {
54 $year += ($year > $Breakpoint) ? $Century : $NextCentury;
55 }
56
57 unless ($Options{no_range_check}) {
58 if (abs($year) >= 0x7fff) {
59 $year += 1900;
60 croak "Cannot handle date ($sec, $min, $hour, $mday, $month, $year)";
61 }
62
63 croak "Month '$month' out of range 0..11" if $month > 11 or $month < 0;
64
65 my $md = $MonthDays[$month];
66 ++$md unless $month != 1 or $year % 4 or !($year % 400);
67
68 croak "Day '$mday' out of range 1..$md" if $mday > $md or $mday < 1;
69 croak "Hour '$hour' out of range 0..23" if $hour > 23 or $hour < 0;
70 croak "Minute '$min' out of range 0..59" if $min > 59 or $min < 0;
71 croak "Second '$sec' out of range 0..59" if $sec > 59 or $sec < 0;
06ef4121 72 }
326557bd 73
74 my $days = _daygm(undef, undef, undef, $mday, $month, $year);
75
76 unless ($Options{no_range_check} or abs($days) < $MaxDay) {
77 $year += 1900;
78 croak "Cannot handle date ($sec, $min, $hour, $mday, $month, $year)";
06ef4121 79 }
326557bd 80
81 $sec + 60*$min + 3600*$hour + 86400*$days;
9bb8015a 82}
83
326557bd 84
e36f48eb 85sub timegm_nocheck {
b75c8c73 86 local $Options{no_range_check} = 1;
e36f48eb 87 &timegm;
88}
89
326557bd 90
9bb8015a 91sub timelocal {
326557bd 92 my $ref_t = &timegm;
93 my $loc_t = _timegm(localtime($ref_t));
a0d0e21e 94
326557bd 95 # Is there a timezone offset from GMT or are we done
96 my $zone_off = $ref_t - $loc_t
97 or return $loc_t;
16bb4654 98
326557bd 99 # Adjust for timezone
100 $loc_t = $ref_t + $zone_off;
16bb4654 101
326557bd 102 # Are we close to a DST change or are we done
103 my $dst_off = $ref_t - _timegm(localtime($loc_t))
104 or return $loc_t;
105
106 # Adjust for DST change
107 $loc_t + $dst_off;
a0d0e21e 108}
109
326557bd 110
e36f48eb 111sub timelocal_nocheck {
b75c8c73 112 local $Options{no_range_check} = 1;
e36f48eb 113 &timelocal;
114}
115
a0d0e21e 1161;
06ef4121 117
118__END__
119
120=head1 NAME
121
122Time::Local - efficiently compute time from local and GMT time
123
124=head1 SYNOPSIS
125
396e3838 126 $time = timelocal($sec,$min,$hour,$mday,$mon,$year);
127 $time = timegm($sec,$min,$hour,$mday,$mon,$year);
06ef4121 128
129=head1 DESCRIPTION
130
396e3838 131These routines are the inverse of built-in perl functions localtime()
06ef4121 132and gmtime(). They accept a date as a six-element array, and return
133the corresponding time(2) value in seconds since the Epoch (Midnight,
134January 1, 1970). This value can be positive or negative.
135
136It is worth drawing particular attention to the expected ranges for
eee32007 137the values provided. The value for the day of the month is the actual day
138(ie 1..31), while the month is the number of months since January (0..11).
06ef4121 139This is consistent with the values returned from localtime() and gmtime().
140
e36f48eb 141The timelocal() and timegm() functions perform range checking on the
396e3838 142input $sec, $min, $hour, $mday, and $mon values by default. If you'd
e36f48eb 143rather they didn't, you can explicitly import the timelocal_nocheck()
144and timegm_nocheck() functions.
ac54365a 145
e36f48eb 146 use Time::Local 'timelocal_nocheck';
3cb6de81 147
a1f33342 148 {
a1f33342 149 # The 365th day of 1999
e36f48eb 150 print scalar localtime timelocal_nocheck 0,0,0,365,0,99;
ac54365a 151
a1f33342 152 # The twenty thousandth day since 1970
e36f48eb 153 print scalar localtime timelocal_nocheck 0,0,0,20000,0,70;
ac54365a 154
a1f33342 155 # And even the 10,000,000th second since 1999!
e36f48eb 156 print scalar localtime timelocal_nocheck 10000000,0,0,1,0,99;
a1f33342 157 }
ac54365a 158
e36f48eb 159Your mileage may vary when trying these with minutes and hours,
ac54365a 160and it doesn't work at all for months.
161
06ef4121 162Strictly speaking, the year should also be specified in a form consistent
163with localtime(), i.e. the offset from 1900.
164In order to make the interpretation of the year easier for humans,
165however, who are more accustomed to seeing years as two-digit or four-digit
166values, the following conventions are followed:
167
168=over 4
169
170=item *
171
172Years greater than 999 are interpreted as being the actual year,
173rather than the offset from 1900. Thus, 1963 would indicate the year
90ca0aaa 174Martin Luther King won the Nobel prize, not the year 2863.
06ef4121 175
176=item *
177
178Years in the range 100..999 are interpreted as offset from 1900,
179so that 112 indicates 2012. This rule also applies to years less than zero
180(but see note below regarding date range).
181
182=item *
183
184Years in the range 0..99 are interpreted as shorthand for years in the
185rolling "current century," defined as 50 years on either side of the current
186year. Thus, today, in 1999, 0 would refer to 2000, and 45 to 2045,
187but 55 would refer to 1955. Twenty years from now, 55 would instead refer
188to 2055. This is messy, but matches the way people currently think about
189two digit dates. Whenever possible, use an absolute four digit year instead.
190
191=back
192
193The scheme above allows interpretation of a wide range of dates, particularly
194if 4-digit years are used.
90ca0aaa 195
06ef4121 196Please note, however, that the range of dates that can be actually be handled
197depends on the size of an integer (time_t) on a given platform.
198Currently, this is 32 bits for most systems, yielding an approximate range
199from Dec 1901 to Jan 2038.
200
201Both timelocal() and timegm() croak if given dates outside the supported
202range.
203
204=head1 IMPLEMENTATION
205
206These routines are quite efficient and yet are always guaranteed to agree
207with localtime() and gmtime(). We manage this by caching the start times
208of any months we've seen before. If we know the start time of the month,
209we can always calculate any time within the month. The start times
326557bd 210are calculated using a mathematical formula. Unlike other algorithms
211that do multiple calls to gmtime().
06ef4121 212
213timelocal() is implemented using the same cache. We just assume that we're
214translating a GMT time, and then fudge it when we're done for the timezone
215and daylight savings arguments. Note that the timezone is evaluated for
216each date because countries occasionally change their official timezones.
217Assuming that localtime() corrects for these changes, this routine will
326557bd 218also be correct.
06ef4121 219
220=head1 BUGS
221
222The whole scheme for interpreting two-digit years can be considered a bug.
223
06ef4121 224The proclivity to croak() is probably a bug.
225
226=cut
326557bd 227