1308265Sgjb# Generate the 'leapseconds' file from 'leap-seconds.list'.
2308265Sgjb
3308265Sgjb# This file is in the public domain.
4308265Sgjb
5308265SgjbBEGIN {
6308265Sgjb  print "# Allowance for leap seconds added to each time zone file."
7308265Sgjb  print ""
8308265Sgjb  print "# This file is in the public domain."
9308265Sgjb  print ""
10308265Sgjb  print "# This file is generated automatically from the data in the public-domain"
11339631Sphilip  print "# leap-seconds.list file, which can be copied from"
12339631Sphilip  print "# <ftp://ftp.nist.gov/pub/time/leap-seconds.list>"
13339631Sphilip  print "# or <ftp://ftp.boulder.nist.gov/pub/time/leap-seconds.list>"
14339631Sphilip  print "# or <ftp://tycho.usno.navy.mil/pub/ntp/leap-seconds.list>."
15308265Sgjb  print "# For more about leap-seconds.list, please see"
16308265Sgjb  print "# The NTP Timescale and Leap Seconds"
17339631Sphilip  print "# <https://www.eecis.udel.edu/~mills/leap.html>."
18308265Sgjb  print ""
19308265Sgjb  print "# The International Earth Rotation and Reference Systems Service"
20308265Sgjb  print "# periodically uses leap seconds to keep UTC to within 0.9 s of UT1"
21339631Sphilip  print "# (which measures the true angular orientation of the earth in space)"
22339631Sphilip  print "# and publishes leap second data in a copyrighted file"
23339631Sphilip  print "# <https://hpiers.obspm.fr/iers/bul/bulc/Leap_Second.dat>."
24339631Sphilip  print "# See: Levine J. Coordinated Universal Time and the leap second."
25325160Sphilip  print "# URSI Radio Sci Bull. 2016;89(4):30-6. doi:10.23919/URSIRSB.2016.7909995"
26339631Sphilip  print "# <https://ieeexplore.ieee.org/document/7909995>."
27342669Sphilip  print ""
28308265Sgjb  print "# There were no leap seconds before 1972, because the official mechanism"
29308265Sgjb  print "# accounting for the discrepancy between atomic time and the earth's rotation"
30342669Sphilip  print "# did not exist.  The first (\"1 Jan 1972\") data line in leap-seconds.list"
31342669Sphilip  print "# does not denote a leap second; it denotes the start of the current definition"
32342669Sphilip  print"# of UTC."
33308265Sgjb  print ""
34308265Sgjb  print "# The correction (+ or -) is made at the given time, so lines"
35308265Sgjb  print "# will typically look like:"
36308265Sgjb  print "#	Leap	YEAR	MON	DAY	23:59:60	+	R/S"
37308265Sgjb  print "# or"
38308265Sgjb  print "#	Leap	YEAR	MON	DAY	23:59:59	-	R/S"
39308265Sgjb  print ""
40339631Sphilip  print "# If the leap second is Rolling (R) the given time is local time (unused here)."
41339631Sphilip
42339631Sphilip  monthabbr[ 1] = "Jan"
43339631Sphilip  monthabbr[ 2] = "Feb"
44339631Sphilip  monthabbr[ 3] = "Mar"
45339631Sphilip  monthabbr[ 4] = "Apr"
46339631Sphilip  monthabbr[ 5] = "May"
47339631Sphilip  monthabbr[ 6] = "Jun"
48339631Sphilip  monthabbr[ 7] = "Jul"
49339631Sphilip  monthabbr[ 8] = "Aug"
50339631Sphilip  monthabbr[ 9] = "Sep"
51339631Sphilip  monthabbr[10] = "Oct"
52339631Sphilip  monthabbr[11] = "Nov"
53339631Sphilip  monthabbr[12] = "Dec"
54339631Sphilip  for (i in monthabbr) {
55339631Sphilip      monthnum[monthabbr[i]] = i
56339631Sphilip      monthlen[i] = 31
57339631Sphilip  }
58339631Sphilip  monthlen[2] = 28
59339631Sphilip  monthlen[4] = monthlen[6] = monthlen[9] = monthlen[11] = 30
60308265Sgjb}
61308265Sgjb
62308265Sgjb/^#\tUpdated through/ || /^#\tFile expires on:/ {
63308265Sgjb    last_lines = last_lines $0 "\n"
64308265Sgjb}
65308265Sgjb
66339631Sphilip/^#[$][ \t]/ { updated = $2 }
67339631Sphilip/^#[@][ \t]/ { expires = $2 }
68339631Sphilip
69308265Sgjb/^#/ { next }
70308265Sgjb
71308265Sgjb{
72308265Sgjb    NTP_timestamp = $1
73308265Sgjb    TAI_minus_UTC = $2
74308265Sgjb    hash_mark = $3
75308265Sgjb    one = $4
76308265Sgjb    month = $5
77308265Sgjb    year = $6
78308265Sgjb    if (old_TAI_minus_UTC) {
79308265Sgjb	if (old_TAI_minus_UTC < TAI_minus_UTC) {
80308265Sgjb	    sign = "23:59:60\t+"
81308265Sgjb	} else {
82308265Sgjb	    sign = "23:59:59\t-"
83308265Sgjb	}
84339631Sphilip	m = monthnum[month] - 1
85339631Sphilip	if (m == 0) {
86308265Sgjb	    year--;
87339631Sphilip	    m = 12
88308265Sgjb	}
89339631Sphilip	month = monthabbr[m]
90339631Sphilip	day = monthlen[m]
91339631Sphilip	day += m == 2 && year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
92308265Sgjb	printf "Leap\t%s\t%s\t%s\t%s\tS\n", year, month, day, sign
93308265Sgjb    }
94308265Sgjb    old_TAI_minus_UTC = TAI_minus_UTC
95308265Sgjb}
96308265Sgjb
97308265SgjbEND {
98339631Sphilip    # The difference between the NTP and POSIX epochs is 70 years
99339631Sphilip    # (including 17 leap days), each 24 hours of 60 minutes of 60
100339631Sphilip    # seconds each.
101339631Sphilip    epoch_minus_NTP = ((1970 - 1900) * 365 + 17) * 24 * 60 * 60
102339631Sphilip
103339631Sphilip    print ""
104339631Sphilip    print "# POSIX timestamps for the data in this file:"
105339631Sphilip    printf "#updated %s\n", updated - epoch_minus_NTP
106339631Sphilip    printf "#expires %s\n", expires - epoch_minus_NTP
107308265Sgjb    printf "\n%s", last_lines
108308265Sgjb}
109