Re: [chrony-dev] [PATCH v2 1/6] reference: move leap second source into leapdb

2023-12-06 Thread Miroslav Lichvar
On Thu, Nov 30, 2023 at 02:23:24PM +1100, [email protected] wrote:
> +++ b/leapdb.c
> @@ -0,0 +1,147 @@
> +/*
> +  chronyd/chronyc - Programs for keeping computer clocks accurate.
> +
> + **
> + * Copyright (C) Patrick Oppenlander 2023

When code is moved, the copyright should be assigned to the author of
the code as reported by git blame.

> +void
> +LDB_Finalise(void)
> +{
> +  /* nothing to do */

Just a nit, comments start with capital letter. 

> --- a/main.c
> +++ b/main.c
> @@ -57,6 +57,7 @@
>  #include "smooth.h"
>  #include "tempcomp.h"
>  #include "util.h"
> +#include "leapdb.h"

Try to keep the inclusion in alphabetical order.

> +++ b/reference.c
> @@ -36,6 +36,7 @@
>  #include "logging.h"
>  #include "local.h"
>  #include "sched.h"
> +#include "leapdb.h"

Same here.

-- 
Miroslav Lichvar


-- 
To unsubscribe email [email protected] with "unsubscribe" 
in the subject.
For help email [email protected] with "help" in the 
subject.
Trouble?  Email [email protected].



[chrony-dev] [PATCH v2 1/6] reference: move leap second source into leapdb

2023-11-29 Thread patrick . oppenlander
From: Patrick Oppenlander 

Separate out source of leap second data into a new module in preparation
for supporting more sources such as leap-seconds.list.
---
 Makefile.in |   2 +-
 leapdb.c| 147 
 leapdb.h|  37 +
 main.c  |   3 ++
 reference.c |  99 +++
 5 files changed, 194 insertions(+), 94 deletions(-)
 create mode 100644 leapdb.c
 create mode 100644 leapdb.h

diff --git a/Makefile.in b/Makefile.in
index 101e0c6..318109b 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -37,7 +37,7 @@ GETDATE_CFLAGS = @GETDATE_CFLAGS@
 
 EXTRA_OBJS = @EXTRA_OBJS@
 
-OBJS = array.o cmdparse.o conf.o local.o logging.o main.o memory.o quantiles.o 
\
+OBJS = array.o cmdparse.o conf.o leapdb.o local.o logging.o main.o memory.o 
quantiles.o \
reference.o regress.o rtc.o samplefilt.o sched.o socket.o sources.o 
sourcestats.o \
stubs.o smooth.o sys.o sys_null.o tempcomp.o util.o $(EXTRA_OBJS)
 
diff --git a/leapdb.c b/leapdb.c
new file mode 100644
index 000..d184390
--- /dev/null
+++ b/leapdb.c
@@ -0,0 +1,147 @@
+/*
+  chronyd/chronyc - Programs for keeping computer clocks accurate.
+
+ **
+ * Copyright (C) Patrick Oppenlander 2023
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ **
+
+  ===
+
+  This module provides leap second information. */
+
+#include "config.h"
+
+#include "sysincl.h"
+
+#include "leapdb.h"
+#include "conf.h"
+#include "logging.h"
+
+/* == */
+
+/* Name of a system timezone containing leap seconds occuring at midnight */
+static char *leap_tzname;
+
+/* == */
+
+static NTP_Leap
+get_tz_leap(time_t when, int *tai_offset)
+{
+  static time_t last_tz_leap_check;
+  static NTP_Leap tz_leap;
+  static int tz_tai_offset;
+
+  struct tm stm, *tm;
+  time_t t;
+  char *tz_env, tz_orig[128];
+
+  *tai_offset = tz_tai_offset;
+
+  /* Do this check at most twice a day */
+  when = when / (12 * 3600) * (12 * 3600);
+  if (last_tz_leap_check == when)
+  return tz_leap;
+
+  last_tz_leap_check = when;
+  tz_leap = LEAP_Normal;
+  tz_tai_offset = 0;
+
+  tm = gmtime(&when);
+  if (!tm)
+return tz_leap;
+
+  stm = *tm;
+
+  /* Temporarily switch to the timezone containing leap seconds */
+  tz_env = getenv("TZ");
+  if (tz_env) {
+if (strlen(tz_env) >= sizeof (tz_orig))
+  return tz_leap;
+strcpy(tz_orig, tz_env);
+  }
+  setenv("TZ", leap_tzname, 1);
+  tzset();
+
+  /* Get the TAI-UTC offset, which started at the epoch at 10 seconds */
+  t = mktime(&stm);
+  if (t != -1)
+tz_tai_offset = t - when + 10;
+
+  /* Set the time to 23:59:60 and see how it overflows in mktime() */
+  stm.tm_sec = 60;
+  stm.tm_min = 59;
+  stm.tm_hour = 23;
+
+  t = mktime(&stm);
+
+  if (tz_env)
+setenv("TZ", tz_orig, 1);
+  else
+unsetenv("TZ");
+  tzset();
+
+  if (t == -1)
+return tz_leap;
+
+  if (stm.tm_sec == 60)
+tz_leap = LEAP_InsertSecond;
+  else if (stm.tm_sec == 1)
+tz_leap = LEAP_DeleteSecond;
+
+  *tai_offset = tz_tai_offset;
+
+  return tz_leap;
+}
+
+/* == */
+
+void
+LDB_Initialise(void)
+{
+  int tai_offset;
+
+  leap_tzname = CNF_GetLeapSecTimezone();
+  if (leap_tzname) {
+/* Check that the timezone has good data for Jun 30 2012 and Dec 31 2012 */
+if (get_tz_leap(1341014400, &tai_offset) == LEAP_InsertSecond && 
tai_offset == 34 &&
+get_tz_leap(1356912000, &tai_offset) == LEAP_Normal && tai_offset == 
35) {
+  LOG(LOGS_INFO, "Using %s timezone to obtain leap second data", 
leap_tzname);
+} else {
+  LOG(LOGS_WARN, "Timezone %s failed leap second check, ignoring", 
leap_tzname);
+  leap_tzname = NULL;
+}
+  }
+}
+
+/* == */
+
+NTP_Leap
+LDB_GetLeap(time_t when, int *tai_offset)
+{
+  *tai_offset = 0;
+  if (leap_tzname)
+return get_tz_leap(when, tai_offset);
+  return LEAP_Normal;
+}
+
+/* == */
+
+void
+LDB_Finalise(void)
+{
+  /* nothing to do */
+}
diff