leapseconds.awk revision 342669
1# Generate the 'leapseconds' file from 'leap-seconds.list'.
2
3# This file is in the public domain.
4
5BEGIN {
6  print "# Allowance for leap seconds added to each time zone file."
7  print ""
8  print "# This file is in the public domain."
9  print ""
10  print "# This file is generated automatically from the data in the public-domain"
11  print "# leap-seconds.list file, which can be copied from"
12  print "# <ftp://ftp.nist.gov/pub/time/leap-seconds.list>"
13  print "# or <ftp://ftp.boulder.nist.gov/pub/time/leap-seconds.list>"
14  print "# or <ftp://tycho.usno.navy.mil/pub/ntp/leap-seconds.list>."
15  print "# For more about leap-seconds.list, please see"
16  print "# The NTP Timescale and Leap Seconds"
17  print "# <https://www.eecis.udel.edu/~mills/leap.html>."
18  print ""
19  print "# The International Earth Rotation and Reference Systems Service"
20  print "# periodically uses leap seconds to keep UTC to within 0.9 s of UT1"
21  print "# (which measures the true angular orientation of the earth in space)"
22  print "# and publishes leap second data in a copyrighted file"
23  print "# <https://hpiers.obspm.fr/iers/bul/bulc/Leap_Second.dat>."
24  print "# See: Levine J. Coordinated Universal Time and the leap second."
25  print "# URSI Radio Sci Bull. 2016;89(4):30-6. doi:10.23919/URSIRSB.2016.7909995"
26  print "# <https://ieeexplore.ieee.org/document/7909995>."
27  print ""
28  print "# There were no leap seconds before 1972, because the official mechanism"
29  print "# accounting for the discrepancy between atomic time and the earth's rotation"
30  print "# did not exist.  The first (\"1 Jan 1972\") data line in leap-seconds.list"
31  print "# does not denote a leap second; it denotes the start of the current definition"
32  print"# of UTC."
33  print ""
34  print "# The correction (+ or -) is made at the given time, so lines"
35  print "# will typically look like:"
36  print "#	Leap	YEAR	MON	DAY	23:59:60	+	R/S"
37  print "# or"
38  print "#	Leap	YEAR	MON	DAY	23:59:59	-	R/S"
39  print ""
40  print "# If the leap second is Rolling (R) the given time is local time (unused here)."
41
42  monthabbr[ 1] = "Jan"
43  monthabbr[ 2] = "Feb"
44  monthabbr[ 3] = "Mar"
45  monthabbr[ 4] = "Apr"
46  monthabbr[ 5] = "May"
47  monthabbr[ 6] = "Jun"
48  monthabbr[ 7] = "Jul"
49  monthabbr[ 8] = "Aug"
50  monthabbr[ 9] = "Sep"
51  monthabbr[10] = "Oct"
52  monthabbr[11] = "Nov"
53  monthabbr[12] = "Dec"
54  for (i in monthabbr) {
55      monthnum[monthabbr[i]] = i
56      monthlen[i] = 31
57  }
58  monthlen[2] = 28
59  monthlen[4] = monthlen[6] = monthlen[9] = monthlen[11] = 30
60}
61
62/^#\tUpdated through/ || /^#\tFile expires on:/ {
63    last_lines = last_lines $0 "\n"
64}
65
66/^#[$][ \t]/ { updated = $2 }
67/^#[@][ \t]/ { expires = $2 }
68
69/^#/ { next }
70
71{
72    NTP_timestamp = $1
73    TAI_minus_UTC = $2
74    hash_mark = $3
75    one = $4
76    month = $5
77    year = $6
78    if (old_TAI_minus_UTC) {
79	if (old_TAI_minus_UTC < TAI_minus_UTC) {
80	    sign = "23:59:60\t+"
81	} else {
82	    sign = "23:59:59\t-"
83	}
84	m = monthnum[month] - 1
85	if (m == 0) {
86	    year--;
87	    m = 12
88	}
89	month = monthabbr[m]
90	day = monthlen[m]
91	day += m == 2 && year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
92	printf "Leap\t%s\t%s\t%s\t%s\tS\n", year, month, day, sign
93    }
94    old_TAI_minus_UTC = TAI_minus_UTC
95}
96
97END {
98    # The difference between the NTP and POSIX epochs is 70 years
99    # (including 17 leap days), each 24 hours of 60 minutes of 60
100    # seconds each.
101    epoch_minus_NTP = ((1970 - 1900) * 365 + 17) * 24 * 60 * 60
102
103    print ""
104    print "# POSIX timestamps for the data in this file:"
105    printf "#updated %s\n", updated - epoch_minus_NTP
106    printf "#expires %s\n", expires - epoch_minus_NTP
107    printf "\n%s", last_lines
108}
109