leapseconds.awk revision 352354
1# Generate zic format 'leapseconds' from NIST format 'leap-seconds.list'.
2
3# This file is in the public domain.
4
5# This program uses awk arithmetic.  POSIX requires awk to support
6# exact integer arithmetic only through 10**10, which means for NTP
7# timestamps this program works only to the year 2216, which is the
8# year 1900 plus 10**10 seconds.  However, in practice
9# POSIX-conforming awk implementations invariably use IEEE-754 double
10# and so support exact integers through 2**53.  By the year 2216,
11# POSIX will almost surely require at least 2**53 for awk, so for NTP
12# timestamps this program should be good until the year 285,428,681
13# (the year 1900 plus 2**53 seconds).  By then leap seconds will be
14# long obsolete, as the Earth will likely slow down so much that
15# there will be more than 25 hours per day and so some other scheme
16# will be needed.
17
18BEGIN {
19  print "# Allowance for leap seconds added to each time zone file."
20  print ""
21  print "# This file is in the public domain."
22  print ""
23  print "# This file is generated automatically from the data in the public-domain"
24  print "# NIST format leap-seconds.list file, which can be copied from"
25  print "# <ftp://ftp.nist.gov/pub/time/leap-seconds.list>"
26  print "# or <ftp://ftp.boulder.nist.gov/pub/time/leap-seconds.list>."
27  print "# For more about leap-seconds.list, please see"
28  print "# The NTP Timescale and Leap Seconds"
29  print "# <https://www.eecis.udel.edu/~mills/leap.html>."
30  print ""
31  print "# The rules for leap seconds are specified in Annex 1 (Time scales) of:"
32  print "# Standard-frequency and time-signal emissions."
33  print "# International Telecommunication Union - Radiocommunication Sector"
34  print "# (ITU-R) Recommendation TF.460-6 (02/2002)"
35  print "# <https://www.itu.int/rec/R-REC-TF.460-6-200202-I/>."
36  print "# The International Earth Rotation and Reference Systems Service (IERS)"
37  print "# periodically uses leap seconds to keep UTC to within 0.9 s of UT1"
38  print "# (a proxy for Earth's angle in space as measured by astronomers)"
39  print "# and publishes leap second data in a copyrighted file"
40  print "# <https://hpiers.obspm.fr/iers/bul/bulc/Leap_Second.dat>."
41  print "# See: Levine J. Coordinated Universal Time and the leap second."
42  print "# URSI Radio Sci Bull. 2016;89(4):30-6. doi:10.23919/URSIRSB.2016.7909995"
43  print "# <https://ieeexplore.ieee.org/document/7909995>."
44  print ""
45  print "# There were no leap seconds before 1972, as no official mechanism"
46  print "# accounted for the discrepancy between atomic time (TAI) and the earth's"
47  print "# rotation.  The first (\"1 Jan 1972\") data line in leap-seconds.list"
48  print "# does not denote a leap second; it denotes the start of the current definition"
49  print "# of UTC."
50  print ""
51  print "# All leap-seconds are Stationary (S) at the given UTC time."
52  print "# The correction (+ or -) is made at the given time, so in the unlikely"
53  print "# event of a negative leap second, a line would look like this:"
54  print "# Leap	YEAR	MON	DAY	23:59:59	-	S"
55  print "# Typical lines look like this:"
56  print "# Leap	YEAR	MON	DAY	23:59:60	+	S"
57
58  monthabbr[ 1] = "Jan"
59  monthabbr[ 2] = "Feb"
60  monthabbr[ 3] = "Mar"
61  monthabbr[ 4] = "Apr"
62  monthabbr[ 5] = "May"
63  monthabbr[ 6] = "Jun"
64  monthabbr[ 7] = "Jul"
65  monthabbr[ 8] = "Aug"
66  monthabbr[ 9] = "Sep"
67  monthabbr[10] = "Oct"
68  monthabbr[11] = "Nov"
69  monthabbr[12] = "Dec"
70
71  # Strip trailing CR, in case the input has CRLF form a la NIST.
72  RS = "\r?\n"
73
74  sstamp_init()
75}
76
77/^#[ \t]*[Uu]pdated through/ || /^#[ \t]*[Ff]ile expires on/ {
78    last_lines = last_lines $0 "\n"
79}
80
81/^#[$][ \t]/ { updated = $2 }
82/^#[@][ \t]/ { expires = $2 }
83
84/^[ \t]*#/ { next }
85
86{
87    NTP_timestamp = $1
88    TAI_minus_UTC = $2
89    if (old_TAI_minus_UTC) {
90	if (old_TAI_minus_UTC < TAI_minus_UTC) {
91	    sign = "23:59:60\t+"
92	} else {
93	    sign = "23:59:59\t-"
94	}
95	sstamp_to_ymdhMs(NTP_timestamp - 1, ss_NTP)
96	printf "Leap\t%d\t%s\t%d\t%s\tS\n", \
97	  ss_year, monthabbr[ss_month], ss_mday, sign
98    }
99    old_TAI_minus_UTC = TAI_minus_UTC
100}
101
102END {
103    # The difference between the NTP and POSIX epochs is 70 years
104    # (including 17 leap days), each 24 hours of 60 minutes of 60
105    # seconds each.
106    epoch_minus_NTP = ((1970 - 1900) * 365 + 17) * 24 * 60 * 60
107
108    print ""
109    print "# POSIX timestamps for the data in this file:"
110    sstamp_to_ymdhMs(updated, ss_NTP)
111    printf "#updated %d (%.4d-%.2d-%.2d %.2d:%.2d:%.2d UTC)\n", \
112      updated - epoch_minus_NTP, \
113      ss_year, ss_month, ss_mday, ss_hour, ss_min, ss_sec
114    sstamp_to_ymdhMs(expires, ss_NTP)
115    printf "#expires %d (%.4d-%.2d-%.2d %.2d:%.2d:%.2d UTC)\n", \
116      expires - epoch_minus_NTP, \
117      ss_year, ss_month, ss_mday, ss_hour, ss_min, ss_sec
118
119    printf "\n%s", last_lines
120}
121
122# sstamp_to_ymdhMs - convert seconds timestamp to date and time
123#
124# Call as:
125#
126#    sstamp_to_ymdhMs(sstamp, epoch_days)
127#
128# where:
129#
130#    sstamp - is the seconds timestamp.
131#    epoch_days - is the timestamp epoch in Gregorian days since 1600-03-01.
132#	ss_NTP is appropriate for an NTP sstamp.
133#
134# Both arguments should be nonnegative integers.
135# On return, the following variables are set based on sstamp:
136#
137#    ss_year	- Gregorian calendar year
138#    ss_month	- month of the year (1-January to 12-December)
139#    ss_mday	- day of the month (1-31)
140#    ss_hour	- hour (0-23)
141#    ss_min	- minute (0-59)
142#    ss_sec	- second (0-59)
143#    ss_wday	- day of week (0-Sunday to 6-Saturday)
144#
145# The function sstamp_init should be called prior to using sstamp_to_ymdhMs.
146
147function sstamp_init()
148{
149  # Days in month N, where March is month 0 and January month 10.
150  ss_mon_days[ 0] = 31
151  ss_mon_days[ 1] = 30
152  ss_mon_days[ 2] = 31
153  ss_mon_days[ 3] = 30
154  ss_mon_days[ 4] = 31
155  ss_mon_days[ 5] = 31
156  ss_mon_days[ 6] = 30
157  ss_mon_days[ 7] = 31
158  ss_mon_days[ 8] = 30
159  ss_mon_days[ 9] = 31
160  ss_mon_days[10] = 31
161
162  # Counts of days in a Gregorian year, quad-year, century, and quad-century.
163  ss_year_days = 365
164  ss_quadyear_days = ss_year_days * 4 + 1
165  ss_century_days = ss_quadyear_days * 25 - 1
166  ss_quadcentury_days = ss_century_days * 4 + 1
167
168  # Standard day epochs, suitable for epoch_days.
169  # ss_MJD = 94493
170  # ss_POSIX = 135080
171  ss_NTP = 109513
172}
173
174function sstamp_to_ymdhMs(sstamp, epoch_days, \
175			  quadcentury, century, quadyear, year, month, day)
176{
177  ss_hour = int(sstamp / 3600) % 24
178  ss_min = int(sstamp / 60) % 60
179  ss_sec = sstamp % 60
180
181  # Start with a count of days since 1600-03-01 Gregorian.
182  day = epoch_days + int(sstamp / (24 * 60 * 60))
183
184  # Compute a year-month-day date with days of the month numbered
185  # 0-30, months (March-February) numbered 0-11, and years that start
186  # start March 1 and end after the last day of February.  A quad-year
187  # starts on March 1 of a year evenly divisible by 4 and ends after
188  # the last day of February 4 years later.  A century starts on and
189  # ends before March 1 in years evenly divisible by 100.
190  # A quad-century starts on and ends before March 1 in years divisible
191  # by 400.  While the number of days in a quad-century is a constant,
192  # the number of days in each other time period can vary by 1.
193  # Any variation is in the last day of the time period (there might
194  # or might not be a February 29) where it is easy to deal with.
195
196  quadcentury = int(day / ss_quadcentury_days)
197  day -= quadcentury * ss_quadcentury_days
198  ss_wday = (day + 3) % 7
199  century = int(day / ss_century_days)
200  century -= century == 4
201  day -= century * ss_century_days
202  quadyear = int(day / ss_quadyear_days)
203  day -= quadyear * ss_quadyear_days
204  year = int(day / ss_year_days)
205  year -= year == 4
206  day -= year * ss_year_days
207  for (month = 0; month < 11; month++) {
208    if (day < ss_mon_days[month])
209      break
210    day -= ss_mon_days[month]
211  }
212
213  # Convert the date to a conventional day of month (1-31),
214  # month (1-12, January-December) and Gregorian year.
215  ss_mday = day + 1
216  if (month <= 9) {
217    ss_month = month + 3
218  } else {
219    ss_month = month - 9
220    year++
221  }
222  ss_year = 1600 + quadcentury * 400 + century * 100 + quadyear * 4 + year
223}
224