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