This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
c5d8a929209fa00e7d6c5942475e45dbb2598b50
[perl5.git] / lib / timelocal.pl
1 ;# timelocal.pl
2 ;#
3 ;# Usage:
4 ;#      $time = timelocal($sec,$min,$hours,$mday,$mon,$year);
5 ;#      $time = timegm($sec,$min,$hours,$mday,$mon,$year);
6
7 ;# These routines are quite efficient and yet are always guaranteed to agree
8 ;# with localtime() and gmtime().  We manage this by caching the start times
9 ;# of any months we've seen before.  If we know the start time of the month,
10 ;# we can always calculate any time within the month.  The start times
11 ;# themselves are guessed by successive approximation starting at the
12 ;# current time, since most dates seen in practice are close to the
13 ;# current date.  Unlike algorithms that do a binary search (calling gmtime
14 ;# once for each bit of the time value, resulting in 32 calls), this algorithm
15 ;# calls it at most 6 times, and usually only once or twice.  If you hit
16 ;# the month cache, of course, it doesn't call it at all.
17
18 ;# timelocal is implemented using the same cache.  We just assume that we're
19 ;# translating a GMT time, and then fudge it when we're done for the timezone
20 ;# and daylight savings arguments.  The timezone is determined by examining
21 ;# the result of localtime(0) when the package is initialized.  The daylight
22 ;# savings offset is currently assumed to be one hour.
23
24 CONFIG: {
25     package timelocal;
26     
27     local($[) = 0;
28     @epoch = localtime(0);
29     $tzmin = $epoch[2] * 60 + $epoch[1];        # minutes east of GMT
30     if ($tzmin > 0) {
31         $tzmin = 24 * 60 - $tzmin;              # minutes west of GMT
32         $tzmin -= 24 * 60 if $epoch[5] == 70;   # account for the date line
33     }
34
35     $SEC = 1;
36     $MIN = 60 * $SEC;
37     $HR = 60 * $MIN;
38     $DAYS = 24 * $HR;
39     $YearFix = ((gmtime(946684800))[5] == 100) ? 100 : 0;
40     1;
41 }
42
43 sub timegm {
44     package timelocal;
45
46     local($[) = 0;
47     $ym = pack(C2, @_[5,4]);
48     $cheat = $cheat{$ym} || &cheat;
49     $cheat + $_[0] * $SEC + $_[1] * $MIN + $_[2] * $HR + ($_[3]-1) * $DAYS;
50 }
51
52 sub timelocal {
53     package timelocal;
54
55     local($[) = 0;
56     $time = &main'timegm + $tzmin*$MIN;
57     @test = localtime($time);
58     $time -= $HR if $test[2] != $_[2];
59     $time;
60 }
61
62 package timelocal;
63
64 sub cheat {
65     $year = $_[5];
66     $month = $_[4];
67     die "Month out of range 0..11 in ctime.pl\n" if $month > 11;
68     $guess = $^T;
69     @g = gmtime($guess);
70     $year += $YearFix if $year < $epoch[5];
71     while ($diff = $year - $g[5]) {
72         $guess += $diff * (363 * $DAYS);
73         @g = gmtime($guess);
74     }
75     while ($diff = $month - $g[4]) {
76         $guess += $diff * (27 * $DAYS);
77         @g = gmtime($guess);
78     }
79     $g[3]--;
80     $guess -= $g[0] * $SEC + $g[1] * $MIN + $g[2] * $HR + $g[3] * $DAYS;
81     $cheat{$ym} = $guess;
82 }