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