• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Generate zic format 'leapseconds' from NIST/IERS 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/IERS format leap-seconds.list file, which can be copied from"
25  print "# <https://hpiers.obspm.fr/iers/bul/bulc/ntp/leap-seconds.list>"
26  print "# or via a less-secure protocol and with different comments and"
27  print "# less volatile last-modified and expiration timestamps, from"
28  print "# <ftp://ftp.boulder.nist.gov/pub/time/leap-seconds.list>."
29  print "# For more about leap-seconds.list, please see"
30  print "# The NTP Timescale and Leap Seconds"
31  print "# <https://www.eecis.udel.edu/~mills/leap.html>."
32  print ""
33  print "# The rules for leap seconds are specified in Annex 1 (Time scales) of:"
34  print "# Standard-frequency and time-signal emissions."
35  print "# International Telecommunication Union - Radiocommunication Sector"
36  print "# (ITU-R) Recommendation TF.460-6 (02/2002)"
37  print "# <https://www.itu.int/rec/R-REC-TF.460-6-200202-I/>."
38  print "# The International Earth Rotation and Reference Systems Service (IERS)"
39  print "# periodically uses leap seconds to keep UTC to within 0.9 s of UT1"
40  print "# (a proxy for Earth's angle in space as measured by astronomers)"
41  print "# and publishes leap second data in a copyrighted file"
42  print "# <https://hpiers.obspm.fr/iers/bul/bulc/Leap_Second.dat>."
43  print "# See: Levine J. Coordinated Universal Time and the leap second."
44  print "# URSI Radio Sci Bull. 2016;89(4):30-6. doi:10.23919/URSIRSB.2016.7909995"
45  print "# <https://ieeexplore.ieee.org/document/7909995>."
46  print ""
47  print "# There were no leap seconds before 1972, as no official mechanism"
48  print "# accounted for the discrepancy between atomic time (TAI) and the earth's"
49  print "# rotation.  The first (\"1 Jan 1972\") data line in leap-seconds.list"
50  print "# does not denote a leap second; it denotes the start of the current definition"
51  print "# of UTC."
52  print ""
53  print "# All leap-seconds are Stationary (S) at the given UTC time."
54  print "# The correction (+ or -) is made at the given time, so in the unlikely"
55  print "# event of a negative leap second, a line would look like this:"
56  print "# Leap	YEAR	MON	DAY	23:59:59	-	S"
57  print "# Typical lines look like this:"
58  print "# Leap	YEAR	MON	DAY	23:59:60	+	S"
59
60  monthabbr[ 1] = "Jan"
61  monthabbr[ 2] = "Feb"
62  monthabbr[ 3] = "Mar"
63  monthabbr[ 4] = "Apr"
64  monthabbr[ 5] = "May"
65  monthabbr[ 6] = "Jun"
66  monthabbr[ 7] = "Jul"
67  monthabbr[ 8] = "Aug"
68  monthabbr[ 9] = "Sep"
69  monthabbr[10] = "Oct"
70  monthabbr[11] = "Nov"
71  monthabbr[12] = "Dec"
72
73  sstamp_init()
74}
75
76# In case the input has CRLF form a la NIST.
77{ sub(/\r$/, "") }
78
79/^#[ \t]*[Uu]pdated through/ || /^#[ \t]*[Ff]ile expires on/ {
80    last_lines = last_lines $0 "\n"
81}
82
83/^#[$][ \t]/ { updated = $2 }
84/^#[@][ \t]/ { expires = $2 }
85
86/^[ \t]*#/ { next }
87
88{
89    NTP_timestamp = $1
90    TAI_minus_UTC = $2
91    if (old_TAI_minus_UTC) {
92	if (old_TAI_minus_UTC < TAI_minus_UTC) {
93	    sign = "23:59:60\t+"
94	} else {
95	    sign = "23:59:59\t-"
96	}
97	sstamp_to_ymdhMs(NTP_timestamp - 1, ss_NTP)
98	printf "Leap\t%d\t%s\t%d\t%s\tS\n", \
99	  ss_year, monthabbr[ss_month], ss_mday, sign
100    }
101    old_TAI_minus_UTC = TAI_minus_UTC
102}
103
104END {
105    print ""
106
107    if (expires) {
108      sstamp_to_ymdhMs(expires, ss_NTP)
109
110      print "# UTC timestamp when this leap second list expires."
111      print "# Any additional leap seconds will come after this."
112      if (! EXPIRES_LINE) {
113	print "# This Expires line is commented out for now,"
114	print "# so that pre-2020a zic implementations do not reject this file."
115      }
116      printf "%sExpires %.4d\t%s\t%.2d\t%.2d:%.2d:%.2d\n", \
117	EXPIRES_LINE ? "" : "#", \
118	ss_year, monthabbr[ss_month], ss_mday, ss_hour, ss_min, ss_sec
119    } else {
120      print "# (No Expires line, since the expires time is unknown.)"
121    }
122
123    # The difference between the NTP and POSIX epochs is 70 years
124    # (including 17 leap days), each 24 hours of 60 minutes of 60
125    # seconds each.
126    epoch_minus_NTP = ((1970 - 1900) * 365 + 17) * 24 * 60 * 60
127
128    print ""
129    print "# Here are POSIX timestamps for the data in this file."
130    print "# \"#updated\" gives the last time the leap seconds data changed"
131    print "# or, if this file was derived from the IERS leap-seconds.list,"
132    print "# the last time that file changed in any way."
133    print "# \"#expires\" gives the first time this file might be wrong;"
134    print "# if this file was derived from the IERS leap-seconds.list,"
135    print "# this is typically a bit less than one year after \"updated\"."
136    if (updated) {
137      sstamp_to_ymdhMs(updated, ss_NTP)
138      printf "#updated %d (%.4d-%.2d-%.2d %.2d:%.2d:%.2d UTC)\n", \
139	updated - epoch_minus_NTP, \
140	ss_year, ss_month, ss_mday, ss_hour, ss_min, ss_sec
141    } else {
142      print "#(updated time unknown)"
143    }
144    if (expires) {
145      sstamp_to_ymdhMs(expires, ss_NTP)
146      printf "#expires %d (%.4d-%.2d-%.2d %.2d:%.2d:%.2d UTC)\n", \
147	expires - epoch_minus_NTP, \
148	ss_year, ss_month, ss_mday, ss_hour, ss_min, ss_sec
149    } else {
150      print "#(expires time unknown)"
151    }
152    printf "\n%s", last_lines
153}
154
155# sstamp_to_ymdhMs - convert seconds timestamp to date and time
156#
157# Call as:
158#
159#    sstamp_to_ymdhMs(sstamp, epoch_days)
160#
161# where:
162#
163#    sstamp - is the seconds timestamp.
164#    epoch_days - is the timestamp epoch in Gregorian days since 1600-03-01.
165#	ss_NTP is appropriate for an NTP sstamp.
166#
167# Both arguments should be nonnegative integers.
168# On return, the following variables are set based on sstamp:
169#
170#    ss_year	- Gregorian calendar year
171#    ss_month	- month of the year (1-January to 12-December)
172#    ss_mday	- day of the month (1-31)
173#    ss_hour	- hour (0-23)
174#    ss_min	- minute (0-59)
175#    ss_sec	- second (0-59)
176#    ss_wday	- day of week (0-Sunday to 6-Saturday)
177#
178# The function sstamp_init should be called prior to using sstamp_to_ymdhMs.
179
180function sstamp_init()
181{
182  # Days in month N, where March is month 0 and January month 10.
183  ss_mon_days[ 0] = 31
184  ss_mon_days[ 1] = 30
185  ss_mon_days[ 2] = 31
186  ss_mon_days[ 3] = 30
187  ss_mon_days[ 4] = 31
188  ss_mon_days[ 5] = 31
189  ss_mon_days[ 6] = 30
190  ss_mon_days[ 7] = 31
191  ss_mon_days[ 8] = 30
192  ss_mon_days[ 9] = 31
193  ss_mon_days[10] = 31
194
195  # Counts of days in a Gregorian year, quad-year, century, and quad-century.
196  ss_year_days = 365
197  ss_quadyear_days = ss_year_days * 4 + 1
198  ss_century_days = ss_quadyear_days * 25 - 1
199  ss_quadcentury_days = ss_century_days * 4 + 1
200
201  # Standard day epochs, suitable for epoch_days.
202  # ss_MJD = 94493
203  # ss_POSIX = 135080
204  ss_NTP = 109513
205}
206
207function sstamp_to_ymdhMs(sstamp, epoch_days, \
208			  quadcentury, century, quadyear, year, month, day)
209{
210  ss_hour = int(sstamp / 3600) % 24
211  ss_min = int(sstamp / 60) % 60
212  ss_sec = sstamp % 60
213
214  # Start with a count of days since 1600-03-01 Gregorian.
215  day = epoch_days + int(sstamp / (24 * 60 * 60))
216
217  # Compute a year-month-day date with days of the month numbered
218  # 0-30, months (March-February) numbered 0-11, and years that start
219  # start March 1 and end after the last day of February.  A quad-year
220  # starts on March 1 of a year evenly divisible by 4 and ends after
221  # the last day of February 4 years later.  A century starts on and
222  # ends before March 1 in years evenly divisible by 100.
223  # A quad-century starts on and ends before March 1 in years divisible
224  # by 400.  While the number of days in a quad-century is a constant,
225  # the number of days in each other time period can vary by 1.
226  # Any variation is in the last day of the time period (there might
227  # or might not be a February 29) where it is easy to deal with.
228
229  quadcentury = int(day / ss_quadcentury_days)
230  day -= quadcentury * ss_quadcentury_days
231  ss_wday = (day + 3) % 7
232  century = int(day / ss_century_days)
233  century -= century == 4
234  day -= century * ss_century_days
235  quadyear = int(day / ss_quadyear_days)
236  day -= quadyear * ss_quadyear_days
237  year = int(day / ss_year_days)
238  year -= year == 4
239  day -= year * ss_year_days
240  for (month = 0; month < 11; month++) {
241    if (day < ss_mon_days[month])
242      break
243    day -= ss_mon_days[month]
244  }
245
246  # Convert the date to a conventional day of month (1-31),
247  # month (1-12, January-December) and Gregorian year.
248  ss_mday = day + 1
249  if (month <= 9) {
250    ss_month = month + 3
251  } else {
252    ss_month = month - 9
253    year++
254  }
255  ss_year = 1600 + quadcentury * 400 + century * 100 + quadyear * 4 + year
256}
257