← Index
NYTProf Performance Profile   « block view • line view • sub view »
For ddd2.pl
  Run on Tue May 25 16:52:24 2010
Reported on Tue May 25 16:57:06 2010

File /usr/local/lib/perl5/5.8.8/Time/Local.pm
Statements Executed 108
Statement Execution Time 4.39ms
Subroutines — ordered by exclusive time
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
631231µs282µsTime::Local::::_daygmTime::Local::_daygm
111143µs643µsTime::Local::::timelocalTime::Local::timelocal
221118µs275µsTime::Local::::_zoneadjustTime::Local::_zoneadjust
431118µs279µsTime::Local::::_timegmTime::Local::_timegm
11160µs103µsTime::Local::::timegmTime::Local::timegm
61251µs51µsTime::Local::::CORE:packTime::Local::CORE:pack (opcode)
11136µs679µsTime::Local::::timelocal_nocheckTime::Local::timelocal_nocheck
0000s0sTime::Local::::BEGINTime::Local::BEGIN
0000s0sTime::Local::::timegm_nocheckTime::Local::timegm_nocheck
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package Time::Local;
2
316µsrequire Exporter;
4398µs1237µsuse Carp;
# spent 237µs making 1 call to Exporter::import
5378µs164µsuse Config;
# spent 64µs making 1 call to Config::import
6387µs121µsuse strict;
# spent 21µs making 1 call to strict::import
73474µs116µsuse integer;
# spent 16µs making 1 call to integer::import
8
93497µs1390µsuse vars qw( $VERSION @ISA @EXPORT @EXPORT_OK );
# spent 390µs making 1 call to vars::import
1015µs$VERSION = '1.11';
11163µs$VERSION = eval $VERSION;
1219µs@ISA = qw( Exporter );
1316µs@EXPORT = qw( timegm timelocal );
1417µs@EXPORT_OK = qw( timegm_nocheck timelocal_nocheck );
15
1617µsmy @MonthDays = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
17
18# Determine breakpoint for rolling century
19136µsmy $ThisYear = (localtime())[5];
2017µsmy $Breakpoint = ($ThisYear + 50) % 100;
2115µsmy $NextCentury = $ThisYear - $ThisYear % 100;
2214µs $NextCentury += 100 if $Breakpoint < 50;
2314µsmy $Century = $NextCentury - 100;
2414µsmy $SecOff = 0;
25
2615µsmy (%Options, %Cheat, %Min, %Max);
2714µsmy ($MinInt, $MaxInt);
28
29114µsif ($^O eq 'MacOS') {
30 # time_t is unsigned...
31 $MaxInt = (1 << (8 * $Config{intsize})) - 1;
32 $MinInt = 0;
33} else {
34139µs126µs $MaxInt = ((1 << (8 * $Config{intsize} - 2))-1)*2 + 1;
# spent 26µs making 1 call to Config::FETCH
3515µs $MinInt = -$MaxInt - 1;
36
37 # On Win32 (and others?) time_t appears to be signed, but negative
38 # epochs still don't work. - XXX - this is experimental
3919µs $MinInt = 0
40 unless defined ((localtime(-1))[0]);
41}
42
4316µs$Max{Day} = ($MaxInt >> 1) / 43200;
4416µs$Min{Day} = $MinInt ? -($Max{Day} + 1) : 0;
45
4615µs$Max{Sec} = $MaxInt - 86400 * $Max{Day};
4714µs$Min{Sec} = $MinInt - 86400 * $Min{Day};
48
49# Determine the EPOC day for this machine
5014µsmy $Epoc = 0;
51112µsif ($^O eq 'vos') {
52# work around posix-977 -- VOS doesn't handle dates in
53# the range 1970-1980.
54 $Epoc = _daygm((0, 0, 0, 1, 0, 70, 4, 0));
55}
56elsif ($^O eq 'MacOS') {
573503µs117µs no integer;
# spent 17µs making 1 call to integer::unimport
58
59 # MacOS time() is seconds since 1 Jan 1904, localtime
60 # so we need to calculate an offset to apply later
61 $Epoc = 693901;
62 $SecOff = timelocal(localtime(0)) - timelocal(gmtime(0));
63 $Epoc += _daygm(gmtime(0));
64}
65else {
66125µs178µs $Epoc = _daygm(gmtime(0));
# spent 78µs making 1 call to Time::Local::_daygm
67}
68
6916µs%Cheat=(); # clear the cache as epoc has changed
70
71
# spent 282µs (231+51) within Time::Local::_daygm which was called 6 times, avg 47µs/call: # 4 times (137µs+24µs) by Time::Local::_timegm at line 85, avg 40µs/call # once (57µs+21µs) by Class::DBI::_require_class at line 66 # once (37µs+6µs) by Time::Local::timegm at line 129
sub _daygm {
726247µs651µs $_[3] + ($Cheat{pack("ss",@_[4,5])} ||= do {
# spent 51µs making 6 calls to Time::Local::CORE:pack, avg 8µs/call
73420µs my $month = ($_[4] + 10) % 12;
74417µs my $year = $_[5] + 1900 - $month/10;
75421µs 365*$year + $year/4 - $year/100 + $year/400 + ($month*306 + 5)/10 - $Epoc
76 });
77}
78
79
80
# spent 279µs (118+161) within Time::Local::_timegm which was called 4 times, avg 70µs/call: # 2 times (63µs+94µs) by Time::Local::_zoneadjust at line 92, avg 78µs/call # once (28µs+43µs) by Time::Local::timelocal at line 178 # once (27µs+24µs) by Time::Local::timelocal at line 164
sub _timegm {
81420µs my $sec = $SecOff + $_[0] + 60 * $_[1] + 3600 * $_[2];
82
833872µs120µs no integer;
# spent 20µs making 1 call to integer::unimport
84
85497µs4161µs $sec + 86400 * &_daygm;
# spent 161µs making 4 calls to Time::Local::_daygm, avg 40µs/call
86}
87
88
89
# spent 275µs (118+157) within Time::Local::_zoneadjust which was called 2 times, avg 137µs/call: # once (59µs+87µs) by Time::Local::timelocal at line 158 # once (59µs+70µs) by Time::Local::timelocal at line 159
sub _zoneadjust {
90211µs my ($day, $sec, $time) = @_;
91
92243µs2157µs $sec = $sec + _timegm(localtime($time)) - $time;
# spent 157µs making 2 calls to Time::Local::_timegm, avg 78µs/call
93421µs if ($sec >= 86400) { $day++; $sec -= 86400; }
94210µs if ($sec < 0) { $day--; $sec += 86400; }
95
96234µs ($day, $sec);
97}
98
99
100
# spent 103µs (60+43) within Time::Local::timegm which was called # once (60µs+43µs) by Time::Local::timelocal at line 160
sub timegm {
10118µs my ($sec,$min,$hour,$mday,$month,$year) = @_;
102
10315µs if ($year >= 1000) {
104 $year -= 1900;
105 }
106 elsif ($year < 100 and $year >= 0) {
107 $year += ($year > $Breakpoint) ? $Century : $NextCentury;
108 }
109
11015µs unless ($Options{no_range_check}) {
111 if (abs($year) >= 0x7fff) {
112 $year += 1900;
113 croak "Cannot handle date ($sec, $min, $hour, $mday, $month, *$year*)";
114 }
115
116 croak "Month '$month' out of range 0..11" if $month > 11 or $month < 0;
117
118 my $md = $MonthDays[$month];
119# ++$md if $month == 1 and $year % 4 == 0 and
120# ($year % 100 != 0 or ($year + 1900) % 400 == 0);
121 ++$md unless $month != 1 or $year % 4 or !($year % 400);
122
123 croak "Day '$mday' out of range 1..$md" if $mday > $md or $mday < 1;
124 croak "Hour '$hour' out of range 0..23" if $hour > 23 or $hour < 0;
125 croak "Minute '$min' out of range 0..59" if $min > 59 or $min < 0;
126 croak "Second '$sec' out of range 0..59" if $sec > 59 or $sec < 0;
127 }
128
129115µs143µs my $days = _daygm(undef, undef, undef, $mday, $month, $year);
# spent 43µs making 1 call to Time::Local::_daygm
13016µs my $xsec = $sec + $SecOff + 60*$min + 3600*$hour;
131
13215µs unless ($Options{no_range_check}
133 or ($days > $Min{Day} or $days == $Min{Day} and $xsec >= $Min{Sec})
134 and ($days < $Max{Day} or $days == $Max{Day} and $xsec <= $Max{Sec}))
135 {
136 warn "Day too small - $days > $Min{Day}\n" if $days < $Min{Day};
137 warn "Day too big - $days > $Max{Day}\n" if $days > $Max{Day};
138 warn "Sec too small - $days < $Min{Sec}\n" if $days < $Min{Sec};
139 warn "Sec too big - $days > $Max{Sec}\n" if $days > $Max{Sec};
140 $year += 1900;
141 croak "Cannot handle date ($sec, $min, $hour, $mday, $month, $year)";
142 }
143
1443653µs118µs no integer;
# spent 18µs making 1 call to integer::unimport
145
146115µs $xsec + 86400 * $days;
147}
148
149
150sub timegm_nocheck {
151 local $Options{no_range_check} = 1;
152 &timegm;
153}
154
155
156
# spent 643µs (143+500) within Time::Local::timelocal which was called # once (143µs+500µs) by Time::Local::timelocal_nocheck at line 199
sub timelocal {
157 # Adjust Max/Min allowed times to fit local time zone and call timegm
158120µs1146µs local ($Max{Day}, $Max{Sec}) = _zoneadjust($Max{Day}, $Max{Sec}, $MaxInt);
# spent 146µs making 1 call to Time::Local::_zoneadjust
159120µs1129µs local ($Min{Day}, $Min{Sec}) = _zoneadjust($Min{Day}, $Min{Sec}, $MinInt);
# spent 129µs making 1 call to Time::Local::_zoneadjust
160117µs1103µs my $ref_t = &timegm;
# spent 103µs making 1 call to Time::Local::timegm
161
162 # Calculate first guess with a one-day delta to avoid localtime overflow
16315µs my $delta = ($_[5] < 100)? 86400 : -86400;
164120µs151µs my $loc_t = _timegm(localtime( $ref_t + $delta )) - $delta;
# spent 51µs making 1 call to Time::Local::_timegm
165
166 # Is there a timezone offset from GMT or are we done
16714µs my $zone_off = $ref_t - $loc_t
168 or return $loc_t;
169
170 # This hack is needed to always pick the first matching time
171 # during a DST change when time would otherwise be ambiguous
17216µs $zone_off -= 3600 if ($delta > 0 && $ref_t >= 3600);
173
174 # Adjust for timezone
17514µs $loc_t = $ref_t + $zone_off;
176
177 # Are we close to a DST change or are we done
178134µs171µs my $dst_off = $ref_t - _timegm(localtime($loc_t))
# spent 71µs making 1 call to Time::Local::_timegm
179 or return $loc_t;
180
181 # Adjust for DST change
182 $loc_t += $dst_off;
183
184 return $loc_t if $dst_off >= 0;
185
186 # for a negative offset from GMT, and if the original date
187 # was a non-extent gap in a forward DST jump, we should
188 # now have the wrong answer - undo the DST adjust;
189
190 my ($s,$m,$h) = localtime($loc_t);
191 $loc_t -= $dst_off if $s != $_[0] || $m != $_[1] || $h != $_[2];
192
193 $loc_t;
194}
195
196
197
# spent 679µs (36+643) within Time::Local::timelocal_nocheck which was called # once (36µs+643µs) by Class::Date::__ANON__[/project/perl/lib/Class/Date.pm:290] at line 288 of Class/Date.pm
sub timelocal_nocheck {
19818µs local $Options{no_range_check} = 1;
199126µs1643µs &timelocal;
# spent 643µs making 1 call to Time::Local::timelocal
200}
201
202156µs1;
203
204__END__
205
206=head1 NAME
207
208Time::Local - efficiently compute time from local and GMT time
209
210=head1 SYNOPSIS
211
212 $time = timelocal($sec,$min,$hour,$mday,$mon,$year);
213 $time = timegm($sec,$min,$hour,$mday,$mon,$year);
214
215=head1 DESCRIPTION
216
217These routines are the inverse of built-in perl functions localtime()
218and gmtime(). They accept a date as a six-element array, and return
219the corresponding time(2) value in seconds since the system epoch
220(Midnight, January 1, 1970 GMT on Unix, for example). This value can
221be positive or negative, though POSIX only requires support for
222positive values, so dates before the system's epoch may not work on
223all operating systems.
224
225It is worth drawing particular attention to the expected ranges for
226the values provided. The value for the day of the month is the actual day
227(ie 1..31), while the month is the number of months since January (0..11).
228This is consistent with the values returned from localtime() and gmtime().
229
230The timelocal() and timegm() functions perform range checking on the
231input $sec, $min, $hour, $mday, and $mon values by default. If you'd
232rather they didn't, you can explicitly import the timelocal_nocheck()
233and timegm_nocheck() functions.
234
235 use Time::Local 'timelocal_nocheck';
236
237 {
238 # The 365th day of 1999
239 print scalar localtime timelocal_nocheck 0,0,0,365,0,99;
240
241 # The twenty thousandth day since 1970
242 print scalar localtime timelocal_nocheck 0,0,0,20000,0,70;
243
244 # And even the 10,000,000th second since 1999!
245 print scalar localtime timelocal_nocheck 10000000,0,0,1,0,99;
246 }
247
248Your mileage may vary when trying these with minutes and hours,
249and it doesn't work at all for months.
250
251Strictly speaking, the year should also be specified in a form consistent
252with localtime(), i.e. the offset from 1900.
253In order to make the interpretation of the year easier for humans,
254however, who are more accustomed to seeing years as two-digit or four-digit
255values, the following conventions are followed:
256
257=over 4
258
259=item *
260
261Years greater than 999 are interpreted as being the actual year,
262rather than the offset from 1900. Thus, 1964 would indicate the year
263Martin Luther King won the Nobel prize, not the year 3864.
264
265=item *
266
267Years in the range 100..999 are interpreted as offset from 1900,
268so that 112 indicates 2012. This rule also applies to years less than zero
269(but see note below regarding date range).
270
271=item *
272
273Years in the range 0..99 are interpreted as shorthand for years in the
274rolling "current century," defined as 50 years on either side of the current
275year. Thus, today, in 1999, 0 would refer to 2000, and 45 to 2045,
276but 55 would refer to 1955. Twenty years from now, 55 would instead refer
277to 2055. This is messy, but matches the way people currently think about
278two digit dates. Whenever possible, use an absolute four digit year instead.
279
280=back
281
282The scheme above allows interpretation of a wide range of dates, particularly
283if 4-digit years are used.
284
285Please note, however, that the range of dates that can be actually be handled
286depends on the size of an integer (time_t) on a given platform.
287Currently, this is 32 bits for most systems, yielding an approximate range
288from Dec 1901 to Jan 2038.
289
290Both timelocal() and timegm() croak if given dates outside the supported
291range.
292
293=head2 Ambiguous Local Times (DST)
294
295Because of DST changes, there are many time zones where the same local
296time occurs for two different GMT times on the same day. For example,
297in the "Europe/Paris" time zone, the local time of 2001-10-28 02:30:00
298can represent either 2001-10-28 00:30:00 GMT, B<or> 2001-10-28
29901:30:00 GMT.
300
301When given an ambiguous local time, the timelocal() function should
302always return the epoch for the I<earlier> of the two possible GMT
303times.
304
305=head2 Non-Existent Local Times (DST)
306
307When a DST change causes a locale clock to skip one hour forward,
308there will be an hour's worth of local times that don't exist. Again,
309for the "Europe/Paris" time zone, the local clock jumped from
3102001-03-25 01:59:59 to 2001-03-25 03:00:00.
311
312If the timelocal() function is given a non-existent local time, it
313will simply return an epoch value for the time one hour later.
314
315=head2 Negative Epoch Values
316
317Negative epoch (time_t) values are not officially supported by the
318POSIX standards, so this module's tests do not test them. On some
319systems, they are known not to work. These include MacOS (pre-OSX)
320and Win32.
321
322On systems which do support negative epoch values, this module should
323be able to cope with dates before the start of the epoch, down the
324minimum value of time_t for the system.
325
326=head1 IMPLEMENTATION
327
328These routines are quite efficient and yet are always guaranteed to agree
329with localtime() and gmtime(). We manage this by caching the start times
330of any months we've seen before. If we know the start time of the month,
331we can always calculate any time within the month. The start times
332are calculated using a mathematical formula. Unlike other algorithms
333that do multiple calls to gmtime().
334
335timelocal() is implemented using the same cache. We just assume that we're
336translating a GMT time, and then fudge it when we're done for the timezone
337and daylight savings arguments. Note that the timezone is evaluated for
338each date because countries occasionally change their official timezones.
339Assuming that localtime() corrects for these changes, this routine will
340also be correct.
341
342=head1 BUGS
343
344The whole scheme for interpreting two-digit years can be considered a bug.
345
346=head1 SUPPORT
347
348Support for this module is provided via the datetime@perl.org
349email list. See http://lists.perl.org/ for more details.
350
351Please submit bugs using the RT system at rt.cpan.org, or as a last
352resort, to the datetime@perl.org list.
353
354=head1 AUTHOR
355
356This module is based on a Perl 4 library, timelocal.pl, that was
357included with Perl 4.036, and was most likely written by Tom
358Christiansen.
359
360The current version was written by Graham Barr.
361
362It is now being maintained separately from the Perl core by Dave
363Rolsky, <autarch@urth.org>.
364
365=cut
366
# spent 51µs within Time::Local::CORE:pack which was called 6 times, avg 8µs/call: # 6 times (51µs+0s) by Time::Local::_daygm at line 72 of Time/Local.pm, avg 8µs/call
sub Time::Local::CORE:pack; # xsub