[tz] why does mktime binary search?
enh at google.com
Fri Jul 29 20:36:38 UTC 2016
is there a more detailed explanation of why mktime does binary search?
** Adapted from code provided by Robert Elz, who writes:
** The "best" way to do mktime I think is based on an idea of Bob
** Kridle's (so its said...) from a long time ago.
** It does a binary search of the time_t space. Since time_t's are
** just 32 bits, its a max of 32 iterations (even at 64 bits it
** would still be very reasonable).
this came up on Android because we had some code that did "errno = 0;
mktime(...); if (errno == EOVERFLOW) ..." rather than checking the
return value of mktime. this failed for us because the tzcode mktime
sets errno even on non-failure.
obviously i can fix this by saving errno around the call to
mktime_tzname in mktime, but i'm curious to know why there's a binary
search at all.
i also notice that, of the BSDs, NetBSD seems to have fixed this
locally, FreeBSD has an old enough copy of tzcode that it's not
setting errno at all, and OpenBSD is in the same boat as Android.
here's the NetBSD change, which is does the errno save-restore in time1 instead:
date: 2014-10-16 10:53:32 -0700; author: christos; state: Exp;
lines: +24 -36; commitid: 4woGqYWkKe0i6sUx;
- don't leak errno in mktime()
- when we load a new timezone, don't change anything unless the load succeeded.
More information about the tz