leapseconds.awk 8.9 KB

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