From 27b4d76d1f6f9987bedb4e0b16369779020a9c97 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Mon, 8 Jan 2024 17:21:05 -0800
Subject: [PROPOSED] Automate copying of leap-seconds.list

* Makefile (leaplist_URI, leaplist_TZ, EXTRACT_AUTHOR): New macros.
(fetch-leap-seconds.list, commit-leap-seconds.list):
New phony rules, for maintainer use.
* NEWS, leapseconds.awk: Mention this.
---
 Makefile        | 34 ++++++++++++++++++++++++++++++++--
 NEWS            |  7 +++++++
 leapseconds.awk | 10 ++++------
 3 files changed, 43 insertions(+), 8 deletions(-)

diff --git a/Makefile b/Makefile
index 4e45f93b..97a17314 100644
--- a/Makefile
+++ b/Makefile
@@ -439,6 +439,12 @@ GCC_DEBUG_FLAGS = -DGCC_LINT -g3 -O3 -fno-common \
 
 LEAPSECONDS=
 
+# Where to fetch leap-seconds.list from.
+leaplist_URI = \
+  https://hpiers.obspm.fr/iers/bul/bulc/ntp/leap-seconds.list
+# The file is generated by the IERS Earth Orientation Centre, in Paris.
+leaplist_TZ = Europe/Paris
+
 # The zic command and its arguments.
 
 zic=		./zic
@@ -471,7 +477,8 @@ AWK=		awk
 # is typically nicer if it works.
 KSHELL=		/bin/bash
 
-# Name of curl <https://curl.haxx.se/>, used for HTML validation.
+# Name of curl <https://curl.haxx.se/>, used for HTML validation
+# and to fetch leap-seconds.list from upstream.
 CURL=		curl
 
 # Name of GNU Privacy Guard <https://gnupg.org/>, used to sign distributions.
@@ -718,6 +725,28 @@ leapseconds:	$(LEAP_DEPS)
 		  -f leapseconds.awk leap-seconds.list >$@.out
 		mv $@.out $@
 
+# Awk script to extract a Git-style author from leap-seconds.list comments.
+EXTRACT_AUTHOR = \
+  author_line { sub(/^.[[:space:]]*/, ""); \
+      sub(/:[[:space:]]*/, " <"); \
+      printf "%s>\n", $$0; \
+      success = 1; \
+      exit \
+  } \
+  /Questions or comments to:/ { author_line = 1 } \
+  END { exit !success }
+
+# Fetch leap-seconds.list from upstream.
+fetch-leap-seconds.list:
+		$(CURL) -OR $(leaplist_URI)
+
+# Fetch leap-seconds.list from upstream and commit it to the local repository.
+commit-leap-seconds.list: fetch-leap-seconds.list
+		author=$$($(AWK) '$(EXTRACT_AUTHOR)' leap-seconds.list) && \
+		date=$$(TZ=$(leaplist_TZ) stat -c%y leap-seconds.list) && \
+		git commit --author="$$author" --date="$$date" -m'make $@' \
+		  leap-seconds.list
+
 # Arguments to pass to submakes of install_data.
 # They can be overridden by later submake arguments.
 INSTALLARGS = \
@@ -1315,7 +1344,8 @@ zic.o:		private.h tzfile.h tzdir.h version.h
 .PHONY: ALL INSTALL all
 .PHONY: check check_mild check_time_t_alternatives
 .PHONY: check_web check_zishrink
-.PHONY: clean clean_misc dummy.zd force_tzs
+.PHONY: clean clean_misc commit-leap-seconds.list dummy.zd
+.PHONY: fetch-leap-seconds.list force_tzs
 .PHONY: install install_data maintainer-clean names
 .PHONY: posix_only posix_right public
 .PHONY: rearguard_signatures rearguard_signatures_version
diff --git a/NEWS b/NEWS
index b136c9b6..013e2f40 100644
--- a/NEWS
+++ b/NEWS
@@ -27,6 +27,13 @@ Unreleased, experimental changes
     for some timestamps in November 2422, November 2822, etc. in
     America/Ciudad_Juarez.  (Problem reported by Gilmore Davidson.)
 
+  Changes to build procedure
+
+    The leap-seconds.list file is now copied from the IERS instead of
+    from its downstream counterpart at NIST, as the IERS version is
+    now in the public domain too and tends to be more up-to-date.
+    (Thanks to Martin Burnicki for liaisoning with the IERS.)
+
 
 Release 2023d - 2023-12-21 20:02:24 -0800
 
diff --git a/leapseconds.awk b/leapseconds.awk
index 7d2556bf..7af13e2c 100644
--- a/leapseconds.awk
+++ b/leapseconds.awk
@@ -1,4 +1,4 @@
-# Generate zic format 'leapseconds' from NIST format 'leap-seconds.list'.
+# Generate zic format 'leapseconds' from NIST/IERS format 'leap-seconds.list'.
 
 # This file is in the public domain.
 
@@ -21,13 +21,11 @@ BEGIN {
   print "# This file is in the public domain."
   print ""
   print "# This file is generated automatically from the data in the public-domain"
-  print "# NIST format leap-seconds.list file, which can be copied from"
+  print "# NIST/IERS format leap-seconds.list file, which can be copied from"
+  print "# <https://hpiers.obspm.fr/iers/bul/bulc/ntp/leap-seconds.list>"
+  print "# or, in a variant with different comments, from"
   print "# <ftp://ftp.nist.gov/pub/time/leap-seconds.list>"
   print "# or <ftp://ftp.boulder.nist.gov/pub/time/leap-seconds.list>."
-  print "# The NIST file is used instead of its IERS upstream counterpart"
-  print "# <https://hpiers.obspm.fr/iers/bul/bulc/ntp/leap-seconds.list>"
-  print "# because under US law the NIST file is public domain"
-  print "# whereas the IERS file's copyright and license status is unclear."
   print "# For more about leap-seconds.list, please see"
   print "# The NTP Timescale and Leap Seconds"
   print "# <https://www.eecis.udel.edu/~mills/leap.html>."
-- 
2.43.0

