1352354Sphilip# Generate zic format 'leapseconds' from NIST format 'leap-seconds.list'.
2308265Sgjb
3308265Sgjb# This file is in the public domain.
4308265Sgjb
5352354Sphilip# This program uses awk arithmetic.  POSIX requires awk to support
6352354Sphilip# exact integer arithmetic only through 10**10, which means for NTP
7352354Sphilip# timestamps this program works only to the year 2216, which is the
8352354Sphilip# year 1900 plus 10**10 seconds.  However, in practice
9352354Sphilip# POSIX-conforming awk implementations invariably use IEEE-754 double
10352354Sphilip# and so support exact integers through 2**53.  By the year 2216,
11352354Sphilip# POSIX will almost surely require at least 2**53 for awk, so for NTP
12352354Sphilip# timestamps this program should be good until the year 285,428,681
13352354Sphilip# (the year 1900 plus 2**53 seconds).  By then leap seconds will be
14352354Sphilip# long obsolete, as the Earth will likely slow down so much that
15352354Sphilip# there will be more than 25 hours per day and so some other scheme
16352354Sphilip# will be needed.
17352354Sphilip
18308265SgjbBEGIN {
19308265Sgjb  print "# Allowance for leap seconds added to each time zone file."
20308265Sgjb  print ""
21308265Sgjb  print "# This file is in the public domain."
22308265Sgjb  print ""
23308265Sgjb  print "# This file is generated automatically from the data in the public-domain"
24352354Sphilip  print "# NIST format leap-seconds.list file, which can be copied from"
25339630Sphilip  print "# <ftp://ftp.nist.gov/pub/time/leap-seconds.list>"
26352354Sphilip  print "# or <ftp://ftp.boulder.nist.gov/pub/time/leap-seconds.list>."
27368822Sphilip  print "# The NIST file is used instead of its IERS upstream counterpart"
28368822Sphilip  print "# <https://hpiers.obspm.fr/iers/bul/bulc/ntp/leap-seconds.list>"
29368822Sphilip  print "# because under US law the NIST file is public domain"
30368822Sphilip  print "# whereas the IERS file's copyright and license status is unclear."
31308265Sgjb  print "# For more about leap-seconds.list, please see"
32308265Sgjb  print "# The NTP Timescale and Leap Seconds"
33339630Sphilip  print "# <https://www.eecis.udel.edu/~mills/leap.html>."
34308265Sgjb  print ""
35352354Sphilip  print "# The rules for leap seconds are specified in Annex 1 (Time scales) of:"
36352354Sphilip  print "# Standard-frequency and time-signal emissions."
37352354Sphilip  print "# International Telecommunication Union - Radiocommunication Sector"
38352354Sphilip  print "# (ITU-R) Recommendation TF.460-6 (02/2002)"
39352354Sphilip  print "# <https://www.itu.int/rec/R-REC-TF.460-6-200202-I/>."
40352354Sphilip  print "# The International Earth Rotation and Reference Systems Service (IERS)"
41308265Sgjb  print "# periodically uses leap seconds to keep UTC to within 0.9 s of UT1"
42352354Sphilip  print "# (a proxy for Earth's angle in space as measured by astronomers)"
43339630Sphilip  print "# and publishes leap second data in a copyrighted file"
44339630Sphilip  print "# <https://hpiers.obspm.fr/iers/bul/bulc/Leap_Second.dat>."
45339630Sphilip  print "# See: Levine J. Coordinated Universal Time and the leap second."
46325159Sphilip  print "# URSI Radio Sci Bull. 2016;89(4):30-6. doi:10.23919/URSIRSB.2016.7909995"
47339630Sphilip  print "# <https://ieeexplore.ieee.org/document/7909995>."
48342668Sphilip  print ""
49352354Sphilip  print "# There were no leap seconds before 1972, as no official mechanism"
50352354Sphilip  print "# accounted for the discrepancy between atomic time (TAI) and the earth's"
51352354Sphilip  print "# rotation.  The first (\"1 Jan 1972\") data line in leap-seconds.list"
52342668Sphilip  print "# does not denote a leap second; it denotes the start of the current definition"
53352354Sphilip  print "# of UTC."
54308265Sgjb  print ""
55352354Sphilip  print "# All leap-seconds are Stationary (S) at the given UTC time."
56352354Sphilip  print "# The correction (+ or -) is made at the given time, so in the unlikely"
57352354Sphilip  print "# event of a negative leap second, a line would look like this:"
58352354Sphilip  print "# Leap	YEAR	MON	DAY	23:59:59	-	S"
59352354Sphilip  print "# Typical lines look like this:"
60352354Sphilip  print "# Leap	YEAR	MON	DAY	23:59:60	+	S"
61339630Sphilip
62339630Sphilip  monthabbr[ 1] = "Jan"
63339630Sphilip  monthabbr[ 2] = "Feb"
64339630Sphilip  monthabbr[ 3] = "Mar"
65339630Sphilip  monthabbr[ 4] = "Apr"
66339630Sphilip  monthabbr[ 5] = "May"
67339630Sphilip  monthabbr[ 6] = "Jun"
68339630Sphilip  monthabbr[ 7] = "Jul"
69339630Sphilip  monthabbr[ 8] = "Aug"
70339630Sphilip  monthabbr[ 9] = "Sep"
71339630Sphilip  monthabbr[10] = "Oct"
72339630Sphilip  monthabbr[11] = "Nov"
73339630Sphilip  monthabbr[12] = "Dec"
74352354Sphilip
75352354Sphilip  sstamp_init()
76308265Sgjb}
77308265Sgjb
78360362Sphilip# In case the input has CRLF form a la NIST.
79360362Sphilip{ sub(/\r$/, "") }
80360362Sphilip
81352354Sphilip/^#[ \t]*[Uu]pdated through/ || /^#[ \t]*[Ff]ile expires on/ {
82308265Sgjb    last_lines = last_lines $0 "\n"
83308265Sgjb}
84308265Sgjb
85339630Sphilip/^#[$][ \t]/ { updated = $2 }
86339630Sphilip/^#[@][ \t]/ { expires = $2 }
87339630Sphilip
88352354Sphilip/^[ \t]*#/ { next }
89308265Sgjb
90308265Sgjb{
91308265Sgjb    NTP_timestamp = $1
92308265Sgjb    TAI_minus_UTC = $2
93308265Sgjb    if (old_TAI_minus_UTC) {
94308265Sgjb	if (old_TAI_minus_UTC < TAI_minus_UTC) {
95308265Sgjb	    sign = "23:59:60\t+"
96308265Sgjb	} else {
97308265Sgjb	    sign = "23:59:59\t-"
98308265Sgjb	}
99352354Sphilip	sstamp_to_ymdhMs(NTP_timestamp - 1, ss_NTP)
100352354Sphilip	printf "Leap\t%d\t%s\t%d\t%s\tS\n", \
101352354Sphilip	  ss_year, monthabbr[ss_month], ss_mday, sign
102308265Sgjb    }
103308265Sgjb    old_TAI_minus_UTC = TAI_minus_UTC
104308265Sgjb}
105308265Sgjb
106308265SgjbEND {
107360362Sphilip    sstamp_to_ymdhMs(expires, ss_NTP)
108360362Sphilip
109360362Sphilip    print ""
110360362Sphilip    print "# UTC timestamp when this leap second list expires."
111360362Sphilip    print "# Any additional leap seconds will come after this."
112366625Sphilip    if (! EXPIRES_LINE) {
113366625Sphilip      print "# This Expires line is commented out for now,"
114366625Sphilip      print "# so that pre-2020a zic implementations do not reject this file."
115366625Sphilip    }
116360362Sphilip    printf "%sExpires %.4d\t%s\t%.2d\t%.2d:%.2d:%.2d\n", \
117360362Sphilip      EXPIRES_LINE ? "" : "#", \
118360362Sphilip      ss_year, monthabbr[ss_month], ss_mday, ss_hour, ss_min, ss_sec
119360362Sphilip
120339630Sphilip    # The difference between the NTP and POSIX epochs is 70 years
121339630Sphilip    # (including 17 leap days), each 24 hours of 60 minutes of 60
122339630Sphilip    # seconds each.
123339630Sphilip    epoch_minus_NTP = ((1970 - 1900) * 365 + 17) * 24 * 60 * 60
124339630Sphilip
125339630Sphilip    print ""
126339630Sphilip    print "# POSIX timestamps for the data in this file:"
127352354Sphilip    sstamp_to_ymdhMs(updated, ss_NTP)
128352354Sphilip    printf "#updated %d (%.4d-%.2d-%.2d %.2d:%.2d:%.2d UTC)\n", \
129352354Sphilip      updated - epoch_minus_NTP, \
130352354Sphilip      ss_year, ss_month, ss_mday, ss_hour, ss_min, ss_sec
131352354Sphilip    sstamp_to_ymdhMs(expires, ss_NTP)
132352354Sphilip    printf "#expires %d (%.4d-%.2d-%.2d %.2d:%.2d:%.2d UTC)\n", \
133352354Sphilip      expires - epoch_minus_NTP, \
134352354Sphilip      ss_year, ss_month, ss_mday, ss_hour, ss_min, ss_sec
135352354Sphilip
136308265Sgjb    printf "\n%s", last_lines
137308265Sgjb}
138352354Sphilip
139352354Sphilip# sstamp_to_ymdhMs - convert seconds timestamp to date and time
140352354Sphilip#
141352354Sphilip# Call as:
142352354Sphilip#
143352354Sphilip#    sstamp_to_ymdhMs(sstamp, epoch_days)
144352354Sphilip#
145352354Sphilip# where:
146352354Sphilip#
147352354Sphilip#    sstamp - is the seconds timestamp.
148352354Sphilip#    epoch_days - is the timestamp epoch in Gregorian days since 1600-03-01.
149352354Sphilip#	ss_NTP is appropriate for an NTP sstamp.
150352354Sphilip#
151352354Sphilip# Both arguments should be nonnegative integers.
152352354Sphilip# On return, the following variables are set based on sstamp:
153352354Sphilip#
154352354Sphilip#    ss_year	- Gregorian calendar year
155352354Sphilip#    ss_month	- month of the year (1-January to 12-December)
156352354Sphilip#    ss_mday	- day of the month (1-31)
157352354Sphilip#    ss_hour	- hour (0-23)
158352354Sphilip#    ss_min	- minute (0-59)
159352354Sphilip#    ss_sec	- second (0-59)
160352354Sphilip#    ss_wday	- day of week (0-Sunday to 6-Saturday)
161352354Sphilip#
162352354Sphilip# The function sstamp_init should be called prior to using sstamp_to_ymdhMs.
163352354Sphilip
164352354Sphilipfunction sstamp_init()
165352354Sphilip{
166352354Sphilip  # Days in month N, where March is month 0 and January month 10.
167352354Sphilip  ss_mon_days[ 0] = 31
168352354Sphilip  ss_mon_days[ 1] = 30
169352354Sphilip  ss_mon_days[ 2] = 31
170352354Sphilip  ss_mon_days[ 3] = 30
171352354Sphilip  ss_mon_days[ 4] = 31
172352354Sphilip  ss_mon_days[ 5] = 31
173352354Sphilip  ss_mon_days[ 6] = 30
174352354Sphilip  ss_mon_days[ 7] = 31
175352354Sphilip  ss_mon_days[ 8] = 30
176352354Sphilip  ss_mon_days[ 9] = 31
177352354Sphilip  ss_mon_days[10] = 31
178352354Sphilip
179352354Sphilip  # Counts of days in a Gregorian year, quad-year, century, and quad-century.
180352354Sphilip  ss_year_days = 365
181352354Sphilip  ss_quadyear_days = ss_year_days * 4 + 1
182352354Sphilip  ss_century_days = ss_quadyear_days * 25 - 1
183352354Sphilip  ss_quadcentury_days = ss_century_days * 4 + 1
184352354Sphilip
185352354Sphilip  # Standard day epochs, suitable for epoch_days.
186352354Sphilip  # ss_MJD = 94493
187352354Sphilip  # ss_POSIX = 135080
188352354Sphilip  ss_NTP = 109513
189352354Sphilip}
190352354Sphilip
191352354Sphilipfunction sstamp_to_ymdhMs(sstamp, epoch_days, \
192352354Sphilip			  quadcentury, century, quadyear, year, month, day)
193352354Sphilip{
194352354Sphilip  ss_hour = int(sstamp / 3600) % 24
195352354Sphilip  ss_min = int(sstamp / 60) % 60
196352354Sphilip  ss_sec = sstamp % 60
197352354Sphilip
198352354Sphilip  # Start with a count of days since 1600-03-01 Gregorian.
199352354Sphilip  day = epoch_days + int(sstamp / (24 * 60 * 60))
200352354Sphilip
201352354Sphilip  # Compute a year-month-day date with days of the month numbered
202352354Sphilip  # 0-30, months (March-February) numbered 0-11, and years that start
203352354Sphilip  # start March 1 and end after the last day of February.  A quad-year
204352354Sphilip  # starts on March 1 of a year evenly divisible by 4 and ends after
205352354Sphilip  # the last day of February 4 years later.  A century starts on and
206352354Sphilip  # ends before March 1 in years evenly divisible by 100.
207352354Sphilip  # A quad-century starts on and ends before March 1 in years divisible
208352354Sphilip  # by 400.  While the number of days in a quad-century is a constant,
209352354Sphilip  # the number of days in each other time period can vary by 1.
210352354Sphilip  # Any variation is in the last day of the time period (there might
211352354Sphilip  # or might not be a February 29) where it is easy to deal with.
212352354Sphilip
213352354Sphilip  quadcentury = int(day / ss_quadcentury_days)
214352354Sphilip  day -= quadcentury * ss_quadcentury_days
215352354Sphilip  ss_wday = (day + 3) % 7
216352354Sphilip  century = int(day / ss_century_days)
217352354Sphilip  century -= century == 4
218352354Sphilip  day -= century * ss_century_days
219352354Sphilip  quadyear = int(day / ss_quadyear_days)
220352354Sphilip  day -= quadyear * ss_quadyear_days
221352354Sphilip  year = int(day / ss_year_days)
222352354Sphilip  year -= year == 4
223352354Sphilip  day -= year * ss_year_days
224352354Sphilip  for (month = 0; month < 11; month++) {
225352354Sphilip    if (day < ss_mon_days[month])
226352354Sphilip      break
227352354Sphilip    day -= ss_mon_days[month]
228352354Sphilip  }
229352354Sphilip
230352354Sphilip  # Convert the date to a conventional day of month (1-31),
231352354Sphilip  # month (1-12, January-December) and Gregorian year.
232352354Sphilip  ss_mday = day + 1
233352354Sphilip  if (month <= 9) {
234352354Sphilip    ss_month = month + 3
235352354Sphilip  } else {
236352354Sphilip    ss_month = month - 9
237352354Sphilip    year++
238352354Sphilip  }
239352354Sphilip  ss_year = 1600 + quadcentury * 400 + century * 100 + quadyear * 4 + year
240352354Sphilip}
241