14616d0f9Sopenharmony_ci# Generate zic format 'leapseconds' from NIST/IERS format 'leap-seconds.list'. 24616d0f9Sopenharmony_ci 34616d0f9Sopenharmony_ci# This file is in the public domain. 44616d0f9Sopenharmony_ci 54616d0f9Sopenharmony_ci# This program uses awk arithmetic. POSIX requires awk to support 64616d0f9Sopenharmony_ci# exact integer arithmetic only through 10**10, which means for NTP 74616d0f9Sopenharmony_ci# timestamps this program works only to the year 2216, which is the 84616d0f9Sopenharmony_ci# year 1900 plus 10**10 seconds. However, in practice 94616d0f9Sopenharmony_ci# POSIX-conforming awk implementations invariably use IEEE-754 double 104616d0f9Sopenharmony_ci# and so support exact integers through 2**53. By the year 2216, 114616d0f9Sopenharmony_ci# POSIX will almost surely require at least 2**53 for awk, so for NTP 124616d0f9Sopenharmony_ci# timestamps this program should be good until the year 285,428,681 134616d0f9Sopenharmony_ci# (the year 1900 plus 2**53 seconds). By then leap seconds will be 144616d0f9Sopenharmony_ci# long obsolete, as the Earth will likely slow down so much that 154616d0f9Sopenharmony_ci# there will be more than 25 hours per day and so some other scheme 164616d0f9Sopenharmony_ci# will be needed. 174616d0f9Sopenharmony_ci 184616d0f9Sopenharmony_ciBEGIN { 194616d0f9Sopenharmony_ci print "# Allowance for leap seconds added to each time zone file." 204616d0f9Sopenharmony_ci print "" 214616d0f9Sopenharmony_ci print "# This file is in the public domain." 224616d0f9Sopenharmony_ci print "" 234616d0f9Sopenharmony_ci print "# This file is generated automatically from the data in the public-domain" 244616d0f9Sopenharmony_ci print "# NIST/IERS format leap-seconds.list file, which can be copied from" 254616d0f9Sopenharmony_ci print "# <https://hpiers.obspm.fr/iers/bul/bulc/ntp/leap-seconds.list>" 264616d0f9Sopenharmony_ci print "# or, in a variant with different comments, from" 274616d0f9Sopenharmony_ci print "# <ftp://ftp.boulder.nist.gov/pub/time/leap-seconds.list>." 284616d0f9Sopenharmony_ci print "# For more about leap-seconds.list, please see" 294616d0f9Sopenharmony_ci print "# The NTP Timescale and Leap Seconds" 304616d0f9Sopenharmony_ci print "# <https://www.eecis.udel.edu/~mills/leap.html>." 314616d0f9Sopenharmony_ci print "" 324616d0f9Sopenharmony_ci print "# The rules for leap seconds are specified in Annex 1 (Time scales) of:" 334616d0f9Sopenharmony_ci print "# Standard-frequency and time-signal emissions." 344616d0f9Sopenharmony_ci print "# International Telecommunication Union - Radiocommunication Sector" 354616d0f9Sopenharmony_ci print "# (ITU-R) Recommendation TF.460-6 (02/2002)" 364616d0f9Sopenharmony_ci print "# <https://www.itu.int/rec/R-REC-TF.460-6-200202-I/>." 374616d0f9Sopenharmony_ci print "# The International Earth Rotation and Reference Systems Service (IERS)" 384616d0f9Sopenharmony_ci print "# periodically uses leap seconds to keep UTC to within 0.9 s of UT1" 394616d0f9Sopenharmony_ci print "# (a proxy for Earth's angle in space as measured by astronomers)" 404616d0f9Sopenharmony_ci print "# and publishes leap second data in a copyrighted file" 414616d0f9Sopenharmony_ci print "# <https://hpiers.obspm.fr/iers/bul/bulc/Leap_Second.dat>." 424616d0f9Sopenharmony_ci print "# See: Levine J. Coordinated Universal Time and the leap second." 434616d0f9Sopenharmony_ci print "# URSI Radio Sci Bull. 2016;89(4):30-6. doi:10.23919/URSIRSB.2016.7909995" 444616d0f9Sopenharmony_ci print "# <https://ieeexplore.ieee.org/document/7909995>." 454616d0f9Sopenharmony_ci print "" 464616d0f9Sopenharmony_ci print "# There were no leap seconds before 1972, as no official mechanism" 474616d0f9Sopenharmony_ci print "# accounted for the discrepancy between atomic time (TAI) and the earth's" 484616d0f9Sopenharmony_ci print "# rotation. The first (\"1 Jan 1972\") data line in leap-seconds.list" 494616d0f9Sopenharmony_ci print "# does not denote a leap second; it denotes the start of the current definition" 504616d0f9Sopenharmony_ci print "# of UTC." 514616d0f9Sopenharmony_ci print "" 524616d0f9Sopenharmony_ci print "# All leap-seconds are Stationary (S) at the given UTC time." 534616d0f9Sopenharmony_ci print "# The correction (+ or -) is made at the given time, so in the unlikely" 544616d0f9Sopenharmony_ci print "# event of a negative leap second, a line would look like this:" 554616d0f9Sopenharmony_ci print "# Leap YEAR MON DAY 23:59:59 - S" 564616d0f9Sopenharmony_ci print "# Typical lines look like this:" 574616d0f9Sopenharmony_ci print "# Leap YEAR MON DAY 23:59:60 + S" 584616d0f9Sopenharmony_ci 594616d0f9Sopenharmony_ci monthabbr[ 1] = "Jan" 604616d0f9Sopenharmony_ci monthabbr[ 2] = "Feb" 614616d0f9Sopenharmony_ci monthabbr[ 3] = "Mar" 624616d0f9Sopenharmony_ci monthabbr[ 4] = "Apr" 634616d0f9Sopenharmony_ci monthabbr[ 5] = "May" 644616d0f9Sopenharmony_ci monthabbr[ 6] = "Jun" 654616d0f9Sopenharmony_ci monthabbr[ 7] = "Jul" 664616d0f9Sopenharmony_ci monthabbr[ 8] = "Aug" 674616d0f9Sopenharmony_ci monthabbr[ 9] = "Sep" 684616d0f9Sopenharmony_ci monthabbr[10] = "Oct" 694616d0f9Sopenharmony_ci monthabbr[11] = "Nov" 704616d0f9Sopenharmony_ci monthabbr[12] = "Dec" 714616d0f9Sopenharmony_ci 724616d0f9Sopenharmony_ci sstamp_init() 734616d0f9Sopenharmony_ci} 744616d0f9Sopenharmony_ci 754616d0f9Sopenharmony_ci# In case the input has CRLF form a la NIST. 764616d0f9Sopenharmony_ci{ sub(/\r$/, "") } 774616d0f9Sopenharmony_ci 784616d0f9Sopenharmony_ci/^#[ \t]*[Uu]pdated through/ || /^#[ \t]*[Ff]ile expires on/ { 794616d0f9Sopenharmony_ci last_lines = last_lines $0 "\n" 804616d0f9Sopenharmony_ci} 814616d0f9Sopenharmony_ci 824616d0f9Sopenharmony_ci/^#[$][ \t]/ { updated = $2 } 834616d0f9Sopenharmony_ci/^#[@][ \t]/ { expires = $2 } 844616d0f9Sopenharmony_ci 854616d0f9Sopenharmony_ci/^[ \t]*#/ { next } 864616d0f9Sopenharmony_ci 874616d0f9Sopenharmony_ci{ 884616d0f9Sopenharmony_ci NTP_timestamp = $1 894616d0f9Sopenharmony_ci TAI_minus_UTC = $2 904616d0f9Sopenharmony_ci if (old_TAI_minus_UTC) { 914616d0f9Sopenharmony_ci if (old_TAI_minus_UTC < TAI_minus_UTC) { 924616d0f9Sopenharmony_ci sign = "23:59:60\t+" 934616d0f9Sopenharmony_ci } else { 944616d0f9Sopenharmony_ci sign = "23:59:59\t-" 954616d0f9Sopenharmony_ci } 964616d0f9Sopenharmony_ci sstamp_to_ymdhMs(NTP_timestamp - 1, ss_NTP) 974616d0f9Sopenharmony_ci printf "Leap\t%d\t%s\t%d\t%s\tS\n", \ 984616d0f9Sopenharmony_ci ss_year, monthabbr[ss_month], ss_mday, sign 994616d0f9Sopenharmony_ci } 1004616d0f9Sopenharmony_ci old_TAI_minus_UTC = TAI_minus_UTC 1014616d0f9Sopenharmony_ci} 1024616d0f9Sopenharmony_ci 1034616d0f9Sopenharmony_ciEND { 1044616d0f9Sopenharmony_ci print "" 1054616d0f9Sopenharmony_ci 1064616d0f9Sopenharmony_ci if (expires) { 1074616d0f9Sopenharmony_ci sstamp_to_ymdhMs(expires, ss_NTP) 1084616d0f9Sopenharmony_ci 1094616d0f9Sopenharmony_ci print "# UTC timestamp when this leap second list expires." 1104616d0f9Sopenharmony_ci print "# Any additional leap seconds will come after this." 1114616d0f9Sopenharmony_ci if (! EXPIRES_LINE) { 1124616d0f9Sopenharmony_ci print "# This Expires line is commented out for now," 1134616d0f9Sopenharmony_ci print "# so that pre-2020a zic implementations do not reject this file." 1144616d0f9Sopenharmony_ci } 1154616d0f9Sopenharmony_ci printf "%sExpires %.4d\t%s\t%.2d\t%.2d:%.2d:%.2d\n", \ 1164616d0f9Sopenharmony_ci EXPIRES_LINE ? "" : "#", \ 1174616d0f9Sopenharmony_ci ss_year, monthabbr[ss_month], ss_mday, ss_hour, ss_min, ss_sec 1184616d0f9Sopenharmony_ci } else { 1194616d0f9Sopenharmony_ci print "# (No Expires line, since the expires time is unknown.)" 1204616d0f9Sopenharmony_ci } 1214616d0f9Sopenharmony_ci 1224616d0f9Sopenharmony_ci # The difference between the NTP and POSIX epochs is 70 years 1234616d0f9Sopenharmony_ci # (including 17 leap days), each 24 hours of 60 minutes of 60 1244616d0f9Sopenharmony_ci # seconds each. 1254616d0f9Sopenharmony_ci epoch_minus_NTP = ((1970 - 1900) * 365 + 17) * 24 * 60 * 60 1264616d0f9Sopenharmony_ci 1274616d0f9Sopenharmony_ci print "" 1284616d0f9Sopenharmony_ci print "# POSIX timestamps for the data in this file:" 1294616d0f9Sopenharmony_ci if (updated) { 1304616d0f9Sopenharmony_ci sstamp_to_ymdhMs(updated, ss_NTP) 1314616d0f9Sopenharmony_ci printf "#updated %d (%.4d-%.2d-%.2d %.2d:%.2d:%.2d UTC)\n", \ 1324616d0f9Sopenharmony_ci updated - epoch_minus_NTP, \ 1334616d0f9Sopenharmony_ci ss_year, ss_month, ss_mday, ss_hour, ss_min, ss_sec 1344616d0f9Sopenharmony_ci } else { 1354616d0f9Sopenharmony_ci print "#(updated time unknown)" 1364616d0f9Sopenharmony_ci } 1374616d0f9Sopenharmony_ci if (expires) { 1384616d0f9Sopenharmony_ci sstamp_to_ymdhMs(expires, ss_NTP) 1394616d0f9Sopenharmony_ci printf "#expires %d (%.4d-%.2d-%.2d %.2d:%.2d:%.2d UTC)\n", \ 1404616d0f9Sopenharmony_ci expires - epoch_minus_NTP, \ 1414616d0f9Sopenharmony_ci ss_year, ss_month, ss_mday, ss_hour, ss_min, ss_sec 1424616d0f9Sopenharmony_ci } else { 1434616d0f9Sopenharmony_ci print "#(expires time unknown)" 1444616d0f9Sopenharmony_ci } 1454616d0f9Sopenharmony_ci printf "\n%s", last_lines 1464616d0f9Sopenharmony_ci} 1474616d0f9Sopenharmony_ci 1484616d0f9Sopenharmony_ci# sstamp_to_ymdhMs - convert seconds timestamp to date and time 1494616d0f9Sopenharmony_ci# 1504616d0f9Sopenharmony_ci# Call as: 1514616d0f9Sopenharmony_ci# 1524616d0f9Sopenharmony_ci# sstamp_to_ymdhMs(sstamp, epoch_days) 1534616d0f9Sopenharmony_ci# 1544616d0f9Sopenharmony_ci# where: 1554616d0f9Sopenharmony_ci# 1564616d0f9Sopenharmony_ci# sstamp - is the seconds timestamp. 1574616d0f9Sopenharmony_ci# epoch_days - is the timestamp epoch in Gregorian days since 1600-03-01. 1584616d0f9Sopenharmony_ci# ss_NTP is appropriate for an NTP sstamp. 1594616d0f9Sopenharmony_ci# 1604616d0f9Sopenharmony_ci# Both arguments should be nonnegative integers. 1614616d0f9Sopenharmony_ci# On return, the following variables are set based on sstamp: 1624616d0f9Sopenharmony_ci# 1634616d0f9Sopenharmony_ci# ss_year - Gregorian calendar year 1644616d0f9Sopenharmony_ci# ss_month - month of the year (1-January to 12-December) 1654616d0f9Sopenharmony_ci# ss_mday - day of the month (1-31) 1664616d0f9Sopenharmony_ci# ss_hour - hour (0-23) 1674616d0f9Sopenharmony_ci# ss_min - minute (0-59) 1684616d0f9Sopenharmony_ci# ss_sec - second (0-59) 1694616d0f9Sopenharmony_ci# ss_wday - day of week (0-Sunday to 6-Saturday) 1704616d0f9Sopenharmony_ci# 1714616d0f9Sopenharmony_ci# The function sstamp_init should be called prior to using sstamp_to_ymdhMs. 1724616d0f9Sopenharmony_ci 1734616d0f9Sopenharmony_cifunction sstamp_init() 1744616d0f9Sopenharmony_ci{ 1754616d0f9Sopenharmony_ci # Days in month N, where March is month 0 and January month 10. 1764616d0f9Sopenharmony_ci ss_mon_days[ 0] = 31 1774616d0f9Sopenharmony_ci ss_mon_days[ 1] = 30 1784616d0f9Sopenharmony_ci ss_mon_days[ 2] = 31 1794616d0f9Sopenharmony_ci ss_mon_days[ 3] = 30 1804616d0f9Sopenharmony_ci ss_mon_days[ 4] = 31 1814616d0f9Sopenharmony_ci ss_mon_days[ 5] = 31 1824616d0f9Sopenharmony_ci ss_mon_days[ 6] = 30 1834616d0f9Sopenharmony_ci ss_mon_days[ 7] = 31 1844616d0f9Sopenharmony_ci ss_mon_days[ 8] = 30 1854616d0f9Sopenharmony_ci ss_mon_days[ 9] = 31 1864616d0f9Sopenharmony_ci ss_mon_days[10] = 31 1874616d0f9Sopenharmony_ci 1884616d0f9Sopenharmony_ci # Counts of days in a Gregorian year, quad-year, century, and quad-century. 1894616d0f9Sopenharmony_ci ss_year_days = 365 1904616d0f9Sopenharmony_ci ss_quadyear_days = ss_year_days * 4 + 1 1914616d0f9Sopenharmony_ci ss_century_days = ss_quadyear_days * 25 - 1 1924616d0f9Sopenharmony_ci ss_quadcentury_days = ss_century_days * 4 + 1 1934616d0f9Sopenharmony_ci 1944616d0f9Sopenharmony_ci # Standard day epochs, suitable for epoch_days. 1954616d0f9Sopenharmony_ci # ss_MJD = 94493 1964616d0f9Sopenharmony_ci # ss_POSIX = 135080 1974616d0f9Sopenharmony_ci ss_NTP = 109513 1984616d0f9Sopenharmony_ci} 1994616d0f9Sopenharmony_ci 2004616d0f9Sopenharmony_cifunction sstamp_to_ymdhMs(sstamp, epoch_days, \ 2014616d0f9Sopenharmony_ci quadcentury, century, quadyear, year, month, day) 2024616d0f9Sopenharmony_ci{ 2034616d0f9Sopenharmony_ci ss_hour = int(sstamp / 3600) % 24 2044616d0f9Sopenharmony_ci ss_min = int(sstamp / 60) % 60 2054616d0f9Sopenharmony_ci ss_sec = sstamp % 60 2064616d0f9Sopenharmony_ci 2074616d0f9Sopenharmony_ci # Start with a count of days since 1600-03-01 Gregorian. 2084616d0f9Sopenharmony_ci day = epoch_days + int(sstamp / (24 * 60 * 60)) 2094616d0f9Sopenharmony_ci 2104616d0f9Sopenharmony_ci # Compute a year-month-day date with days of the month numbered 2114616d0f9Sopenharmony_ci # 0-30, months (March-February) numbered 0-11, and years that start 2124616d0f9Sopenharmony_ci # start March 1 and end after the last day of February. A quad-year 2134616d0f9Sopenharmony_ci # starts on March 1 of a year evenly divisible by 4 and ends after 2144616d0f9Sopenharmony_ci # the last day of February 4 years later. A century starts on and 2154616d0f9Sopenharmony_ci # ends before March 1 in years evenly divisible by 100. 2164616d0f9Sopenharmony_ci # A quad-century starts on and ends before March 1 in years divisible 2174616d0f9Sopenharmony_ci # by 400. While the number of days in a quad-century is a constant, 2184616d0f9Sopenharmony_ci # the number of days in each other time period can vary by 1. 2194616d0f9Sopenharmony_ci # Any variation is in the last day of the time period (there might 2204616d0f9Sopenharmony_ci # or might not be a February 29) where it is easy to deal with. 2214616d0f9Sopenharmony_ci 2224616d0f9Sopenharmony_ci quadcentury = int(day / ss_quadcentury_days) 2234616d0f9Sopenharmony_ci day -= quadcentury * ss_quadcentury_days 2244616d0f9Sopenharmony_ci ss_wday = (day + 3) % 7 2254616d0f9Sopenharmony_ci century = int(day / ss_century_days) 2264616d0f9Sopenharmony_ci century -= century == 4 2274616d0f9Sopenharmony_ci day -= century * ss_century_days 2284616d0f9Sopenharmony_ci quadyear = int(day / ss_quadyear_days) 2294616d0f9Sopenharmony_ci day -= quadyear * ss_quadyear_days 2304616d0f9Sopenharmony_ci year = int(day / ss_year_days) 2314616d0f9Sopenharmony_ci year -= year == 4 2324616d0f9Sopenharmony_ci day -= year * ss_year_days 2334616d0f9Sopenharmony_ci for (month = 0; month < 11; month++) { 2344616d0f9Sopenharmony_ci if (day < ss_mon_days[month]) 2354616d0f9Sopenharmony_ci break 2364616d0f9Sopenharmony_ci day -= ss_mon_days[month] 2374616d0f9Sopenharmony_ci } 2384616d0f9Sopenharmony_ci 2394616d0f9Sopenharmony_ci # Convert the date to a conventional day of month (1-31), 2404616d0f9Sopenharmony_ci # month (1-12, January-December) and Gregorian year. 2414616d0f9Sopenharmony_ci ss_mday = day + 1 2424616d0f9Sopenharmony_ci if (month <= 9) { 2434616d0f9Sopenharmony_ci ss_month = month + 3 2444616d0f9Sopenharmony_ci } else { 2454616d0f9Sopenharmony_ci ss_month = month - 9 2464616d0f9Sopenharmony_ci year++ 2474616d0f9Sopenharmony_ci } 2484616d0f9Sopenharmony_ci ss_year = 1600 + quadcentury * 400 + century * 100 + quadyear * 4 + year 2494616d0f9Sopenharmony_ci} 250