leapseconds.awk revision 339630
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 "# There were no leap seconds before 1972, because the official mechanism"
28  print "# accounting for the discrepancy between atomic time and the earth's rotation"
29  print "# did not exist."
30  print ""
31  print "# The correction (+ or -) is made at the given time, so lines"
32  print "# will typically look like:"
33  print "#	Leap	YEAR	MON	DAY	23:59:60	+	R/S"
34  print "# or"
35  print "#	Leap	YEAR	MON	DAY	23:59:59	-	R/S"
36  print ""
37  print "# If the leap second is Rolling (R) the given time is local time (unused here)."
38
39  monthabbr[ 1] = "Jan"
40  monthabbr[ 2] = "Feb"
41  monthabbr[ 3] = "Mar"
42  monthabbr[ 4] = "Apr"
43  monthabbr[ 5] = "May"
44  monthabbr[ 6] = "Jun"
45  monthabbr[ 7] = "Jul"
46  monthabbr[ 8] = "Aug"
47  monthabbr[ 9] = "Sep"
48  monthabbr[10] = "Oct"
49  monthabbr[11] = "Nov"
50  monthabbr[12] = "Dec"
51  for (i in monthabbr) {
52      monthnum[monthabbr[i]] = i
53      monthlen[i] = 31
54  }
55  monthlen[2] = 28
56  monthlen[4] = monthlen[6] = monthlen[9] = monthlen[11] = 30
57}
58
59/^#\tUpdated through/ || /^#\tFile expires on:/ {
60    last_lines = last_lines $0 "\n"
61}
62
63/^#[$][ \t]/ { updated = $2 }
64/^#[@][ \t]/ { expires = $2 }
65
66/^#/ { next }
67
68{
69    NTP_timestamp = $1
70    TAI_minus_UTC = $2
71    hash_mark = $3
72    one = $4
73    month = $5
74    year = $6
75    if (old_TAI_minus_UTC) {
76	if (old_TAI_minus_UTC < TAI_minus_UTC) {
77	    sign = "23:59:60\t+"
78	} else {
79	    sign = "23:59:59\t-"
80	}
81	m = monthnum[month] - 1
82	if (m == 0) {
83	    year--;
84	    m = 12
85	}
86	month = monthabbr[m]
87	day = monthlen[m]
88	day += m == 2 && year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
89	printf "Leap\t%s\t%s\t%s\t%s\tS\n", year, month, day, sign
90    }
91    old_TAI_minus_UTC = TAI_minus_UTC
92}
93
94END {
95    # The difference between the NTP and POSIX epochs is 70 years
96    # (including 17 leap days), each 24 hours of 60 minutes of 60
97    # seconds each.
98    epoch_minus_NTP = ((1970 - 1900) * 365 + 17) * 24 * 60 * 60
99
100    print ""
101    print "# POSIX timestamps for the data in this file:"
102    printf "#updated %s\n", updated - epoch_minus_NTP
103    printf "#expires %s\n", expires - epoch_minus_NTP
104    printf "\n%s", last_lines
105}
106