leapseconds.awk revision 339630
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"
11339630Sphilip  print "# leap-seconds.list file, which can be copied from"
12339630Sphilip  print "# <ftp://ftp.nist.gov/pub/time/leap-seconds.list>"
13339630Sphilip  print "# or <ftp://ftp.boulder.nist.gov/pub/time/leap-seconds.list>"
14339630Sphilip  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"
17339630Sphilip  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"
21339630Sphilip  print "# (which measures the true angular orientation of the earth in space)"
22339630Sphilip  print "# and publishes leap second data in a copyrighted file"
23339630Sphilip  print "# <https://hpiers.obspm.fr/iers/bul/bulc/Leap_Second.dat>."
24339630Sphilip  print "# See: Levine J. Coordinated Universal Time and the leap second."
25325159Sphilip  print "# URSI Radio Sci Bull. 2016;89(4):30-6. doi:10.23919/URSIRSB.2016.7909995"
26339630Sphilip  print "# <https://ieeexplore.ieee.org/document/7909995>."
27308265Sgjb  print "# There were no leap seconds before 1972, because the official mechanism"
28308265Sgjb  print "# accounting for the discrepancy between atomic time and the earth's rotation"
29339630Sphilip  print "# did not exist."
30308265Sgjb  print ""
31308265Sgjb  print "# The correction (+ or -) is made at the given time, so lines"
32308265Sgjb  print "# will typically look like:"
33308265Sgjb  print "#	Leap	YEAR	MON	DAY	23:59:60	+	R/S"
34308265Sgjb  print "# or"
35308265Sgjb  print "#	Leap	YEAR	MON	DAY	23:59:59	-	R/S"
36308265Sgjb  print ""
37339630Sphilip  print "# If the leap second is Rolling (R) the given time is local time (unused here)."
38339630Sphilip
39339630Sphilip  monthabbr[ 1] = "Jan"
40339630Sphilip  monthabbr[ 2] = "Feb"
41339630Sphilip  monthabbr[ 3] = "Mar"
42339630Sphilip  monthabbr[ 4] = "Apr"
43339630Sphilip  monthabbr[ 5] = "May"
44339630Sphilip  monthabbr[ 6] = "Jun"
45339630Sphilip  monthabbr[ 7] = "Jul"
46339630Sphilip  monthabbr[ 8] = "Aug"
47339630Sphilip  monthabbr[ 9] = "Sep"
48339630Sphilip  monthabbr[10] = "Oct"
49339630Sphilip  monthabbr[11] = "Nov"
50339630Sphilip  monthabbr[12] = "Dec"
51339630Sphilip  for (i in monthabbr) {
52339630Sphilip      monthnum[monthabbr[i]] = i
53339630Sphilip      monthlen[i] = 31
54339630Sphilip  }
55339630Sphilip  monthlen[2] = 28
56339630Sphilip  monthlen[4] = monthlen[6] = monthlen[9] = monthlen[11] = 30
57308265Sgjb}
58308265Sgjb
59308265Sgjb/^#\tUpdated through/ || /^#\tFile expires on:/ {
60308265Sgjb    last_lines = last_lines $0 "\n"
61308265Sgjb}
62308265Sgjb
63339630Sphilip/^#[$][ \t]/ { updated = $2 }
64339630Sphilip/^#[@][ \t]/ { expires = $2 }
65339630Sphilip
66308265Sgjb/^#/ { next }
67308265Sgjb
68308265Sgjb{
69308265Sgjb    NTP_timestamp = $1
70308265Sgjb    TAI_minus_UTC = $2
71308265Sgjb    hash_mark = $3
72308265Sgjb    one = $4
73308265Sgjb    month = $5
74308265Sgjb    year = $6
75308265Sgjb    if (old_TAI_minus_UTC) {
76308265Sgjb	if (old_TAI_minus_UTC < TAI_minus_UTC) {
77308265Sgjb	    sign = "23:59:60\t+"
78308265Sgjb	} else {
79308265Sgjb	    sign = "23:59:59\t-"
80308265Sgjb	}
81339630Sphilip	m = monthnum[month] - 1
82339630Sphilip	if (m == 0) {
83308265Sgjb	    year--;
84339630Sphilip	    m = 12
85308265Sgjb	}
86339630Sphilip	month = monthabbr[m]
87339630Sphilip	day = monthlen[m]
88339630Sphilip	day += m == 2 && year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
89308265Sgjb	printf "Leap\t%s\t%s\t%s\t%s\tS\n", year, month, day, sign
90308265Sgjb    }
91308265Sgjb    old_TAI_minus_UTC = TAI_minus_UTC
92308265Sgjb}
93308265Sgjb
94308265SgjbEND {
95339630Sphilip    # The difference between the NTP and POSIX epochs is 70 years
96339630Sphilip    # (including 17 leap days), each 24 hours of 60 minutes of 60
97339630Sphilip    # seconds each.
98339630Sphilip    epoch_minus_NTP = ((1970 - 1900) * 365 + 17) * 24 * 60 * 60
99339630Sphilip
100339630Sphilip    print ""
101339630Sphilip    print "# POSIX timestamps for the data in this file:"
102339630Sphilip    printf "#updated %s\n", updated - epoch_minus_NTP
103339630Sphilip    printf "#expires %s\n", expires - epoch_minus_NTP
104308265Sgjb    printf "\n%s", last_lines
105308265Sgjb}
106