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