mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Laurent Bercot <ska-dietlibc@skarnet.org>
To: musl@lists.openwall.com
Subject: Re: [PATCHv2] Add support for leap seconds in zoneinfo files
Date: Thu, 05 Dec 2013 01:13:30 +0000	[thread overview]
Message-ID: <529FD33A.8000509@skarnet.org> (raw)
In-Reply-To: <52965735.9070409@skarnet.org>

[-- Attachment #1: Type: text/plain, Size: 1480 bytes --]


  OK, so actually, the patch was right: in a zoneinfo file containing leap
seconds, the transition times are given in TAI-10, not "UTC seconds", i.e.
times with leap seconds *added*.
(Yes, this is confusing, and the tzfile documentation does not make it
clear enough, but it's the only behaviour that makes sense.)
  So all the calculations involving times given in seconds, including
transition times, are done within the same referential. i.e. TAI-10,
and this is also valid for __tm_gmtoff.
  Only when converting from/to "seconds since the Epoch" to/from "struct tm"
must leap seconds be substracted/added.

  The new version of the patch, provided in attachment, reads leap seconds
directly from the mmapped zoneinfo file instead of storing them in a static
table, as requested by Rich; and it's still not invasive.
  The leap second table scan is still linear, because:
  * when substracting leap seconds, the scan is done from the future to the past.
Since the huge majority of calls involve the current time or a time close to it,
the scan usually stops at the first or second element.
  * when adding leap seconds, the scan has to be done from the past to the future
linearly anyway, to apply successive corrections while testing the correct TAI-10
transition time instead of comparing a UTC value to a TAI-10 value. The previous
version of the patch didn't get this right.

  I would love it if this version, modulo any bugs, could make it into 0.9.15.

-- 
  Laurent


[-- Attachment #2: leapsecs.patch --]
[-- Type: text/plain, Size: 3819 bytes --]

diff -rNU3 src-old/src/time/__secs_to_tm.c src/src/time/__secs_to_tm.c
--- src-old/src/time/__secs_to_tm.c	2013-12-04 15:50:13.000000000 +0100
+++ src/src/time/__secs_to_tm.c	2013-12-05 01:34:35.000000000 +0100
@@ -8,6 +8,23 @@
 #define DAYS_PER_100Y (365*100 + 24)
 #define DAYS_PER_4Y   (365*4   + 1)
 
+static int leapsecs_sub(long long *t)
+{
+	long long trans;
+	int corr;
+	unsigned int i = __leapsecs_num;
+	int hit = 0;
+	for (; i; i--) {
+		__leapsecs_read(i-1, &trans, &corr);
+		if (*t >= trans) break;
+	}
+	if (i) {
+		if (*t == trans) hit = 1;
+		*t -= corr;
+	}
+	return hit;
+}
+
 int __secs_to_tm(long long t, struct tm *tm)
 {
 	long long days, secs;
@@ -21,6 +38,7 @@
 	if (t < INT_MIN * 31622400LL || t > INT_MAX * 31622400LL)
 		return -1;
 
+	int hit = leapsecs_sub(&t);
 	secs = t - LEAPOCH;
 	days = secs / 86400;
 	remsecs = secs % 86400;
@@ -76,6 +94,7 @@
 	tm->tm_hour = remsecs / 3600;
 	tm->tm_min = remsecs / 60 % 60;
 	tm->tm_sec = remsecs % 60;
+	if (hit) tm->tm_sec++;
 
 	return 0;
 }
diff -rNU3 src-old/src/time/__tm_to_secs.c src/src/time/__tm_to_secs.c
--- src-old/src/time/__tm_to_secs.c	2013-12-04 15:50:13.000000000 +0100
+++ src/src/time/__tm_to_secs.c	2013-12-05 01:38:39.000000000 +0100
@@ -1,5 +1,21 @@
 #include "time_impl.h"
 
+static void leapsecs_add(long long *t, int hit)
+{
+	int oldcorr = 0;
+	unsigned int i = 0;
+	for (; i < __leapsecs_num; i++) {
+		long long trans;
+		int newcorr;
+		__leapsecs_read(i, &trans, &newcorr);
+		if (*t < trans) break;
+		if (!hit || (*t > trans)) {
+			*t += newcorr - oldcorr;
+		}
+		oldcorr = newcorr;
+	}
+}
+
 long long __tm_to_secs(const struct tm *tm)
 {
 	int is_leap;
@@ -20,5 +36,6 @@
 	t += 3600LL * tm->tm_hour;
 	t += 60LL * tm->tm_min;
 	t += tm->tm_sec;
+	leapsecs_add(&t, tm->tm_sec==60);
 	return t;
 }
diff -rNU3 src-old/src/time/__tz.c src/src/time/__tz.c
--- src-old/src/time/__tz.c	2013-12-04 15:50:13.000000000 +0100
+++ src/src/time/__tz.c	2013-12-05 01:42:19.000000000 +0100
@@ -17,6 +17,8 @@
 static char dst_name[TZNAME_MAX+1];
 const char __gmt[] = "GMT";
 
+unsigned int __leapsecs_num;
+
 static int dst_off;
 static int r0[5], r1[5];
 
@@ -105,6 +107,22 @@
 	return (unsigned)z[0]<<24 | z[1]<<16 | z[2]<<8 | z[3];
 }
 
+static uint64_t zi_read64(const unsigned char *z)
+{
+	return ((uint64_t)zi_read32(z) << 32) | (uint64_t)zi_read32(z+4);
+}
+
+void __leapsecs_read(unsigned int i, long long *tt, int *corr)
+{
+	if (trans > zi+44) {
+		*tt = (long long)zi_read64(abbrevs_end + 12*i);
+		*corr = (int)zi_read32(abbrevs_end + 12*i + 8);
+	} else {
+		*tt = (long long)zi_read32(abbrevs_end + 8*i);
+		*corr = (int)zi_read32(abbrevs_end + 8*i + 4);
+	}
+}
+
 static size_t zi_dotprod(const unsigned char *z, const unsigned char *v, size_t n)
 {
 	size_t y;
@@ -175,7 +193,7 @@
 	zi = map;
 	if (map) {
 		int scale = 2;
-		if (sizeof(time_t) > 4 && map[4]=='2') {
+		if (sizeof(time_t) > 4 && (map[4]=='2') || (map[4]=='3')) {
 			size_t skip = zi_dotprod(zi+20, VEC(1,1,8,5,6,1), 6);
 			trans = zi+skip+44+44;
 			scale++;
@@ -186,6 +204,7 @@
 		types = index + zi_read32(trans-12);
 		abbrevs = types + 6*zi_read32(trans-8);
 		abbrevs_end = abbrevs + zi_read32(trans-4);
+		__leapsecs_num = zi_read32(trans-16);
 		if (zi[map_size-1] == '\n') {
 			for (s = (const char *)zi+map_size-2; *s!='\n'; s--);
 			s++;
diff -rNU3 src-old/src/time/time_impl.h src/src/time/time_impl.h
--- src-old/src/time/time_impl.h	2013-12-04 15:50:13.000000000 +0100
+++ src/src/time/time_impl.h	2013-12-04 16:50:21.000000000 +0100
@@ -7,3 +7,5 @@
 int __secs_to_tm(long long, struct tm *);
 void __secs_to_zone(long long, int, int *, long *, long *, const char **);
 const unsigned char *__map_file(const char *, size_t *);
+extern unsigned int __leapsecs_num;
+void __leapsecs_read(unsigned int, long long *, int *);

  reply	other threads:[~2013-12-05  1:13 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-26 18:53 [PATCH] " Laurent Bercot
2013-11-26 18:59 ` Laurent Bercot
2013-11-26 23:32 ` Rich Felker
2013-11-27  1:06   ` Rich Felker
2013-11-27  4:10   ` Laurent Bercot
2013-11-27  4:25     ` Rich Felker
2013-11-27  5:53       ` Laurent Bercot
2013-11-27 18:43         ` Szabolcs Nagy
2013-11-27 20:33           ` Laurent Bercot
2013-12-05  1:13             ` Laurent Bercot [this message]
2013-12-05  5:18               ` [PATCHv2] " Szabolcs Nagy
2013-12-05  8:58                 ` Laurent Bercot
2013-12-05 14:21                   ` Szabolcs Nagy
2013-12-05 14:43               ` Rich Felker
2013-12-05 16:31                 ` Laurent Bercot
2013-12-05 16:40                   ` Rich Felker
2013-12-06  0:36                     ` Laurent Bercot
2013-12-06  0:45                       ` Rich Felker
2013-12-06  1:15                         ` Laurent Bercot
2013-12-06  5:31                           ` Szabolcs Nagy
2013-12-06 10:48                             ` Laurent Bercot
2013-12-06 11:38                               ` Raphael Cohn
2013-12-06 23:29                                 ` Laurent Bercot
2013-12-07  2:31                                   ` Rich Felker
2013-12-07  4:02                                     ` Szabolcs Nagy
2013-12-07  8:52                                     ` Laurent Bercot
2013-12-06  6:29                           ` Rich Felker
2013-12-06 10:37                             ` Laurent Bercot
2013-12-06 12:50                               ` Rich Felker
2013-12-06 13:27                                 ` Szabolcs Nagy
2013-12-06 15:48                                   ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=529FD33A.8000509@skarnet.org \
    --to=ska-dietlibc@skarnet.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).