2020-09-07 04:39:00 +00:00
|
|
|
/*-*- mode:c; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
|
|
|
|
│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-04-27 12:39:39 +00:00
|
|
|
#define LOCALTIME_IMPLEMENTATION
|
2022-11-06 02:49:41 +00:00
|
|
|
#include "libc/calls/blockcancel.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/calls.h"
|
2022-08-13 21:18:02 +00:00
|
|
|
#include "libc/intrin/bits.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/mem/gc.h"
|
2022-10-16 19:05:08 +00:00
|
|
|
#include "libc/mem/mem.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/str/str.h"
|
|
|
|
#include "libc/sysv/consts/o.h"
|
2022-10-16 19:05:08 +00:00
|
|
|
#include "libc/thread/thread.h"
|
|
|
|
#include "libc/thread/tls.h"
|
2022-08-14 00:20:50 +00:00
|
|
|
#include "libc/time/struct/tm.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/time/time.h"
|
2022-04-27 12:39:39 +00:00
|
|
|
#include "libc/time/tz.internal.h"
|
2020-11-25 16:19:00 +00:00
|
|
|
#include "libc/time/tzfile.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2023-07-26 20:54:49 +00:00
|
|
|
__static_yoink("zipos");
|
|
|
|
__static_yoink("usr/share/zoneinfo/");
|
|
|
|
__static_yoink("usr/share/zoneinfo/Anchorage");
|
|
|
|
__static_yoink("usr/share/zoneinfo/Beijing");
|
|
|
|
__static_yoink("usr/share/zoneinfo/Berlin");
|
|
|
|
__static_yoink("usr/share/zoneinfo/Boulder");
|
|
|
|
__static_yoink("usr/share/zoneinfo/Chicago");
|
|
|
|
__static_yoink("usr/share/zoneinfo/GMT");
|
|
|
|
__static_yoink("usr/share/zoneinfo/GST");
|
|
|
|
__static_yoink("usr/share/zoneinfo/Honolulu");
|
|
|
|
__static_yoink("usr/share/zoneinfo/Israel");
|
|
|
|
__static_yoink("usr/share/zoneinfo/Japan");
|
|
|
|
__static_yoink("usr/share/zoneinfo/London");
|
|
|
|
__static_yoink("usr/share/zoneinfo/Melbourne");
|
|
|
|
__static_yoink("usr/share/zoneinfo/New_York");
|
|
|
|
__static_yoink("usr/share/zoneinfo/UTC");
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-05-12 13:11:22 +00:00
|
|
|
// clang-format off
|
|
|
|
/* Convert timestamp from time_t to struct tm. */
|
2020-06-15 14:18:57 +00:00
|
|
|
/*
|
|
|
|
** This file is in the public domain, so clarified as of
|
|
|
|
** 1996-06-05 by Arthur David Olson.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Leap second handling from Bradley White.
|
|
|
|
** POSIX-style TZ environment variable handling from Guy Harris.
|
|
|
|
*/
|
|
|
|
|
2022-06-11 08:59:26 +00:00
|
|
|
static pthread_mutex_t locallock;
|
2022-04-27 12:39:39 +00:00
|
|
|
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
void localtime_wipe(void) {
|
|
|
|
pthread_mutex_init(&locallock, 0);
|
|
|
|
}
|
|
|
|
|
2022-10-16 19:05:08 +00:00
|
|
|
void localtime_lock(void) {
|
2022-06-11 08:59:26 +00:00
|
|
|
pthread_mutex_lock(&locallock);
|
2022-04-27 12:39:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 02:33:42 +00:00
|
|
|
void localtime_unlock(void) {
|
2022-06-11 08:59:26 +00:00
|
|
|
pthread_mutex_unlock(&locallock);
|
2022-04-27 12:39:39 +00:00
|
|
|
}
|
|
|
|
|
2022-10-16 19:05:08 +00:00
|
|
|
__attribute__((__constructor__)) static void localtime_init(void) {
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
localtime_wipe();
|
2022-10-16 19:05:08 +00:00
|
|
|
pthread_atfork(localtime_lock,
|
|
|
|
localtime_unlock,
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
localtime_wipe);
|
2022-10-16 19:05:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
#ifndef TZ_ABBR_MAX_LEN
|
|
|
|
#define TZ_ABBR_MAX_LEN 16
|
|
|
|
#endif /* !defined TZ_ABBR_MAX_LEN */
|
|
|
|
|
|
|
|
#ifndef TZ_ABBR_CHAR_SET
|
|
|
|
#define TZ_ABBR_CHAR_SET \
|
|
|
|
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :+-._"
|
|
|
|
#endif /* !defined TZ_ABBR_CHAR_SET */
|
|
|
|
|
|
|
|
#ifndef TZ_ABBR_ERR_CHAR
|
|
|
|
#define TZ_ABBR_ERR_CHAR '_'
|
|
|
|
#endif /* !defined TZ_ABBR_ERR_CHAR */
|
|
|
|
|
|
|
|
#ifndef WILDABBR
|
|
|
|
/*
|
|
|
|
** Someone might make incorrect use of a time zone abbreviation:
|
|
|
|
** 1. They might reference tzname[0] before calling tzset (explicitly
|
|
|
|
** or implicitly).
|
|
|
|
** 2. They might reference tzname[1] before calling tzset (explicitly
|
|
|
|
** or implicitly).
|
|
|
|
** 3. They might reference tzname[1] after setting to a time zone
|
|
|
|
** in which Daylight Saving Time is never observed.
|
|
|
|
** 4. They might reference tzname[0] after setting to a time zone
|
|
|
|
** in which Standard Time is never observed.
|
|
|
|
** 5. They might reference tm.TM_ZONE after calling offtime.
|
|
|
|
** What's best to do in the above cases is open to debate;
|
|
|
|
** for now, we just set things up so that in any of the five cases
|
|
|
|
** WILDABBR is used. Another possibility: initialize tzname[0] to the
|
|
|
|
** string "tzname[0] used before set", and similarly for the other cases.
|
|
|
|
** And another: initialize tzname[0] to "ERA", with an explanation in the
|
|
|
|
** manual page of what this "time zone abbreviation" means (doing this so
|
|
|
|
** that tzname[0] has the "normal" length of three characters).
|
|
|
|
*/
|
|
|
|
#define WILDABBR " "
|
|
|
|
#endif /* !defined WILDABBR */
|
|
|
|
|
|
|
|
static const char wildabbr[] = WILDABBR;
|
|
|
|
|
2022-04-27 12:39:39 +00:00
|
|
|
static const char gmt[] = "GMT";
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
** The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
|
2022-04-27 12:39:39 +00:00
|
|
|
** Default to US rules as of 2017-05-07.
|
|
|
|
** POSIX does not specify the default DST rules;
|
|
|
|
** for historical reasons, US rules are a common default.
|
2020-06-15 14:18:57 +00:00
|
|
|
*/
|
|
|
|
#ifndef TZDEFRULESTRING
|
2022-04-27 12:39:39 +00:00
|
|
|
#define TZDEFRULESTRING ",M3.2.0,M11.1.0"
|
|
|
|
#endif
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
struct ttinfo { /* time type information */
|
2023-10-11 21:54:42 +00:00
|
|
|
int32_t tt_utoff; /* UT offset in seconds */
|
2022-04-27 12:39:39 +00:00
|
|
|
bool tt_isdst; /* used to set tm_isdst */
|
|
|
|
int tt_desigidx; /* abbreviation list index */
|
|
|
|
bool tt_ttisstd; /* transition is std time */
|
|
|
|
bool tt_ttisut; /* transition is UT */
|
2020-06-15 14:18:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct lsinfo { /* leap second information */
|
2022-05-12 13:11:22 +00:00
|
|
|
time_t ls_trans; /* transition time */
|
2023-10-11 21:54:42 +00:00
|
|
|
int32_t ls_corr; /* correction to apply */
|
2020-06-15 14:18:57 +00:00
|
|
|
};
|
|
|
|
|
2022-04-27 12:39:39 +00:00
|
|
|
#define SMALLEST(a, b) (((a) < (b)) ? (a) : (b))
|
2020-06-15 14:18:57 +00:00
|
|
|
#define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
|
|
|
|
|
2022-04-27 12:39:39 +00:00
|
|
|
/* This abbreviation means local time is unspecified. */
|
|
|
|
static char const UNSPEC[] = "-00";
|
|
|
|
|
|
|
|
/* How many extra bytes are needed at the end of struct state's chars array.
|
|
|
|
This needs to be at least 1 for null termination in case the input
|
|
|
|
data isn't properly terminated, and it also needs to be big enough
|
|
|
|
for ttunspecified to work without crashing. */
|
|
|
|
enum { CHARS_EXTRA = BIGGEST(sizeof UNSPEC, 2) - 1 };
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
#ifdef TZNAME_MAX
|
|
|
|
#define MY_TZNAME_MAX TZNAME_MAX
|
|
|
|
#endif /* defined TZNAME_MAX */
|
|
|
|
#ifndef TZNAME_MAX
|
|
|
|
#define MY_TZNAME_MAX 255
|
|
|
|
#endif /* !defined TZNAME_MAX */
|
|
|
|
|
|
|
|
struct state {
|
|
|
|
int leapcnt;
|
|
|
|
int timecnt;
|
|
|
|
int typecnt;
|
|
|
|
int charcnt;
|
2022-04-27 12:39:39 +00:00
|
|
|
bool goback;
|
|
|
|
bool goahead;
|
2022-05-12 13:11:22 +00:00
|
|
|
time_t ats[TZ_MAX_TIMES];
|
2020-06-15 14:18:57 +00:00
|
|
|
unsigned char types[TZ_MAX_TIMES];
|
|
|
|
struct ttinfo ttis[TZ_MAX_TYPES];
|
2022-04-27 12:39:39 +00:00
|
|
|
char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + CHARS_EXTRA,
|
|
|
|
sizeof gmt),
|
2020-06-15 14:18:57 +00:00
|
|
|
(2 * (MY_TZNAME_MAX + 1)))];
|
|
|
|
struct lsinfo lsis[TZ_MAX_LEAPS];
|
2022-04-27 12:39:39 +00:00
|
|
|
|
|
|
|
/* The time type to use for early times or if no transitions.
|
|
|
|
It is always zero for recent tzdb releases.
|
|
|
|
It might be nonzero for data from tzdb 2018e or earlier. */
|
|
|
|
int defaulttype;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum r_type {
|
|
|
|
JULIAN_DAY, /* Jn = Julian day */
|
|
|
|
DAY_OF_YEAR, /* n = day of year */
|
|
|
|
MONTH_NTH_DAY_OF_WEEK /* Mm.n.d = month, week, day of week */
|
2020-06-15 14:18:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct rule {
|
2022-04-27 12:39:39 +00:00
|
|
|
enum r_type r_type; /* type of rule */
|
2020-06-15 14:18:57 +00:00
|
|
|
int r_day; /* day number of rule */
|
|
|
|
int r_week; /* week number of rule */
|
|
|
|
int r_mon; /* month number of rule */
|
2023-10-11 21:54:42 +00:00
|
|
|
int32_t r_time; /* transition time of rule */
|
2020-06-15 14:18:57 +00:00
|
|
|
};
|
|
|
|
|
2023-10-11 21:54:42 +00:00
|
|
|
static struct tm *gmtsub(struct state const *, time_t const *, int32_t,
|
2022-04-27 12:39:39 +00:00
|
|
|
struct tm *);
|
2022-05-12 13:11:22 +00:00
|
|
|
static bool increment_overflow(int *, int);
|
2023-10-11 21:54:42 +00:00
|
|
|
static bool increment_overflow_time(time_t *, int32_t);
|
|
|
|
static int32_t leapcorr(struct state const *, time_t);
|
|
|
|
static bool normalize_overflow32(int32_t *, int *, int);
|
|
|
|
static struct tm *localtime_timesub(time_t const *, int32_t,
|
2022-04-29 07:36:14 +00:00
|
|
|
struct state const *, struct tm *);
|
|
|
|
static bool localtime_typesequiv(struct state const *, int, int);
|
|
|
|
static bool localtime_tzparse(char const *, struct state *, struct state *);
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
static struct state * lclptr;
|
|
|
|
static struct state * gmtptr;
|
|
|
|
|
|
|
|
#ifndef TZ_STRLEN_MAX
|
|
|
|
#define TZ_STRLEN_MAX 255
|
|
|
|
#endif /* !defined TZ_STRLEN_MAX */
|
|
|
|
|
|
|
|
static char lcl_TZname[TZ_STRLEN_MAX + 1];
|
|
|
|
static int lcl_is_set;
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Section 4.12.3 of X3.159-1989 requires that
|
|
|
|
** Except for the strftime function, these functions [asctime,
|
|
|
|
** ctime, gmtime, localtime] return values in one of two static
|
|
|
|
** objects: a broken-down time structure and an array of char.
|
|
|
|
** Thanks to Paul Eggert for noting this.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct tm tm;
|
|
|
|
|
2022-05-12 13:11:22 +00:00
|
|
|
#if 2 <= HAVE_TZNAME + TZ_TIME_T
|
2022-04-27 12:39:39 +00:00
|
|
|
char * tzname[2] = {
|
|
|
|
(char *) wildabbr,
|
|
|
|
(char *) wildabbr
|
|
|
|
};
|
|
|
|
#endif
|
2022-05-12 13:11:22 +00:00
|
|
|
#if 2 <= USG_COMPAT + TZ_TIME_T
|
2022-04-27 12:39:39 +00:00
|
|
|
long timezone;
|
2020-09-07 04:39:00 +00:00
|
|
|
int daylight;
|
2022-04-27 12:39:39 +00:00
|
|
|
#endif
|
2022-05-12 13:11:22 +00:00
|
|
|
#if 2 <= ALTZONE + TZ_TIME_T
|
2022-04-27 12:39:39 +00:00
|
|
|
long altzone;
|
|
|
|
#endif
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-04-27 12:39:39 +00:00
|
|
|
/* Initialize *S to a value based on UTOFF, ISDST, and DESIGIDX. */
|
|
|
|
static void
|
2023-10-11 21:54:42 +00:00
|
|
|
init_ttinfo(struct ttinfo *s, int32_t utoff, bool isdst, int desigidx)
|
2022-04-27 12:39:39 +00:00
|
|
|
{
|
|
|
|
s->tt_utoff = utoff;
|
|
|
|
s->tt_isdst = isdst;
|
|
|
|
s->tt_desigidx = desigidx;
|
|
|
|
s->tt_ttisstd = false;
|
|
|
|
s->tt_ttisut = false;
|
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-04-27 12:39:39 +00:00
|
|
|
/* Return true if SP's time type I does not specify local time. */
|
|
|
|
static bool
|
|
|
|
ttunspecified(struct state const *sp, int i)
|
|
|
|
{
|
|
|
|
char const *abbr = &sp->chars[sp->ttis[i].tt_desigidx];
|
|
|
|
/* memcmp is likely faster than strcmp, and is safe due to CHARS_EXTRA. */
|
|
|
|
return memcmp(abbr, UNSPEC, sizeof UNSPEC) == 0;
|
|
|
|
}
|
|
|
|
|
2023-10-11 21:54:42 +00:00
|
|
|
forceinline int32_t detzcode(const char *const codep) {
|
2022-05-14 11:33:58 +00:00
|
|
|
return READ32BE(codep);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
2023-10-11 21:54:42 +00:00
|
|
|
forceinline int64_t detzcode64(const char *const codep) {
|
2022-05-14 11:33:58 +00:00
|
|
|
return READ64BE(codep);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-27 12:39:39 +00:00
|
|
|
static void
|
|
|
|
update_tzname_etc(struct state const *sp, struct ttinfo const *ttisp)
|
|
|
|
{
|
|
|
|
#if HAVE_TZNAME
|
|
|
|
tzname[ttisp->tt_isdst] = (char *) &sp->chars[ttisp->tt_desigidx];
|
|
|
|
#endif
|
|
|
|
if (!ttisp->tt_isdst)
|
|
|
|
timezone = - ttisp->tt_utoff;
|
|
|
|
#if ALTZONE
|
|
|
|
if (ttisp->tt_isdst)
|
|
|
|
altzone = - ttisp->tt_utoff;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
static void
|
2020-09-07 04:39:00 +00:00
|
|
|
settzname(void)
|
2020-06-15 14:18:57 +00:00
|
|
|
{
|
2022-04-27 12:39:39 +00:00
|
|
|
register struct state * const sp = lclptr;
|
|
|
|
register int i;
|
|
|
|
|
|
|
|
#if HAVE_TZNAME
|
|
|
|
tzname[0] = tzname[1] = (char *) (sp ? wildabbr : gmt);
|
|
|
|
#endif
|
2020-06-15 14:18:57 +00:00
|
|
|
daylight = 0;
|
|
|
|
timezone = 0;
|
2022-04-27 12:39:39 +00:00
|
|
|
#if ALTZONE
|
2020-06-15 14:18:57 +00:00
|
|
|
altzone = 0;
|
2022-04-27 12:39:39 +00:00
|
|
|
#endif
|
2020-06-15 14:18:57 +00:00
|
|
|
if (sp == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2022-04-27 12:39:39 +00:00
|
|
|
/*
|
|
|
|
** And to get the latest time zone abbreviations into tzname. . .
|
|
|
|
*/
|
2020-06-15 14:18:57 +00:00
|
|
|
for (i = 0; i < sp->typecnt; ++i) {
|
|
|
|
register const struct ttinfo * const ttisp = &sp->ttis[i];
|
2022-04-27 12:39:39 +00:00
|
|
|
update_tzname_etc(sp, ttisp);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < sp->timecnt; ++i) {
|
|
|
|
register const struct ttinfo * const ttisp =
|
|
|
|
&sp->ttis[
|
|
|
|
sp->types[i]];
|
2022-04-27 12:39:39 +00:00
|
|
|
update_tzname_etc(sp, ttisp);
|
|
|
|
if (ttisp->tt_isdst)
|
|
|
|
daylight = 1;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-04-27 12:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
scrub_abbrs(struct state *sp)
|
|
|
|
{
|
|
|
|
int i;
|
2020-06-15 14:18:57 +00:00
|
|
|
/*
|
|
|
|
** First, replace bogus characters.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < sp->charcnt; ++i)
|
|
|
|
if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL)
|
|
|
|
sp->chars[i] = TZ_ABBR_ERR_CHAR;
|
|
|
|
/*
|
|
|
|
** Second, truncate long abbreviations.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < sp->typecnt; ++i) {
|
|
|
|
register const struct ttinfo * const ttisp = &sp->ttis[i];
|
2022-04-27 12:39:39 +00:00
|
|
|
char *cp = &sp->chars[ttisp->tt_desigidx];
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
if (strlen(cp) > TZ_ABBR_MAX_LEN &&
|
|
|
|
strcmp(cp, GRANDPARENTED) != 0)
|
|
|
|
*(cp + TZ_ABBR_MAX_LEN) = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 12:39:39 +00:00
|
|
|
/* Input buffer for data read from a compiled tz file. */
|
|
|
|
union input_buffer {
|
2022-05-12 13:11:22 +00:00
|
|
|
/* The first part of the buffer, interpreted as a header. */
|
|
|
|
struct tzhead tzhead;
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-05-12 13:11:22 +00:00
|
|
|
/* The entire buffer. */
|
|
|
|
char buf[2 * sizeof(struct tzhead) + 2 * sizeof(struct state)
|
|
|
|
+ 4 * TZ_MAX_TIMES];
|
2022-04-27 12:39:39 +00:00
|
|
|
};
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-04-27 12:39:39 +00:00
|
|
|
/* TZDIR with a trailing '/' rather than a trailing '\0'. */
|
|
|
|
static char const tzdirslash[sizeof TZDIR] = TZDIR "/";
|
|
|
|
|
|
|
|
/* Local storage needed for 'tzloadbody'. */
|
|
|
|
union local_storage {
|
2022-05-12 13:11:22 +00:00
|
|
|
/* The results of analyzing the file's contents after it is opened. */
|
|
|
|
struct file_analysis {
|
|
|
|
/* The input buffer. */
|
|
|
|
union input_buffer u;
|
|
|
|
|
|
|
|
/* A temporary state used for parsing a TZ string in the file. */
|
|
|
|
struct state st;
|
|
|
|
} u;
|
|
|
|
|
|
|
|
/* The file name to be opened. */
|
|
|
|
char fullname[BIGGEST(sizeof(struct file_analysis),
|
|
|
|
sizeof tzdirslash + 1024)];
|
2022-04-27 12:39:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Load tz data from the file named NAME into *SP. Read extended
|
|
|
|
format if DOEXTEND. Use *LSP for temporary storage. Return 0 on
|
|
|
|
success, an errno value on failure. */
|
2020-06-15 14:18:57 +00:00
|
|
|
static int
|
2022-11-06 02:49:41 +00:00
|
|
|
localtime_tzloadbody_(char const *name, struct state *sp, bool doextend,
|
|
|
|
union local_storage *lsp)
|
2022-04-27 12:39:39 +00:00
|
|
|
{
|
2020-06-15 14:18:57 +00:00
|
|
|
register int i;
|
|
|
|
register int fid;
|
|
|
|
register int stored;
|
2022-04-27 12:39:39 +00:00
|
|
|
register ssize_t nread;
|
2022-05-12 13:11:22 +00:00
|
|
|
register bool doaccess;
|
|
|
|
register union input_buffer *up = &lsp->u.u;
|
|
|
|
register int tzheadsize = sizeof(struct tzhead);
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-04-27 12:39:39 +00:00
|
|
|
sp->goback = sp->goahead = false;
|
|
|
|
|
2022-05-12 13:11:22 +00:00
|
|
|
if (! name) {
|
2022-04-27 12:39:39 +00:00
|
|
|
name = TZDEFAULT;
|
2022-05-12 13:11:22 +00:00
|
|
|
if (! name)
|
|
|
|
return EINVAL;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (name[0] == ':')
|
|
|
|
++name;
|
2022-04-27 12:39:39 +00:00
|
|
|
#ifdef SUPPRESS_TZDIR
|
|
|
|
/* Do not prepend TZDIR. This is intended for specialized
|
|
|
|
applications only, due to its security implications. */
|
|
|
|
doaccess = true;
|
|
|
|
#else
|
|
|
|
doaccess = name[0] == '/';
|
|
|
|
#endif
|
|
|
|
if (!doaccess) {
|
|
|
|
char const *dot;
|
|
|
|
size_t namelen = strlen(name);
|
|
|
|
if (sizeof lsp->fullname - sizeof tzdirslash <= namelen)
|
|
|
|
return ENAMETOOLONG;
|
|
|
|
|
|
|
|
/* Create a string "TZDIR/NAME". Using sprintf here
|
|
|
|
would pull in stdio (and would fail if the
|
|
|
|
resulting string length exceeded INT_MAX!). */
|
|
|
|
memcpy(lsp->fullname, tzdirslash, sizeof tzdirslash);
|
|
|
|
strcpy(lsp->fullname + sizeof tzdirslash, name);
|
|
|
|
|
|
|
|
/* Set doaccess if NAME contains a ".." file name
|
|
|
|
component, as such a name could read a file outside
|
|
|
|
the TZDIR virtual subtree. */
|
|
|
|
for (dot = name; (dot = strchr(dot, '.')); dot++)
|
|
|
|
if ((dot == name || dot[-1] == '/') && dot[1] == '.'
|
|
|
|
&& (dot[2] == '/' || !dot[2])) {
|
|
|
|
doaccess = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = lsp->fullname;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-04-27 12:39:39 +00:00
|
|
|
if (doaccess && access(name, R_OK) != 0)
|
|
|
|
return errno;
|
|
|
|
fid = open(name, O_RDONLY);
|
|
|
|
if (fid < 0)
|
|
|
|
return errno;
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
nread = read(fid, up->buf, sizeof up->buf);
|
2022-04-27 12:39:39 +00:00
|
|
|
if (nread < tzheadsize) {
|
|
|
|
int err = nread < 0 ? errno : EINVAL;
|
|
|
|
close(fid);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
if (close(fid) < 0)
|
|
|
|
return errno;
|
2020-06-15 14:18:57 +00:00
|
|
|
for (stored = 4; stored <= 8; stored *= 2) {
|
2022-04-27 12:39:39 +00:00
|
|
|
char version = up->tzhead.tzh_version[0];
|
|
|
|
bool skip_datablock = stored == 4 && version;
|
2023-10-11 21:54:42 +00:00
|
|
|
int32_t datablock_size;
|
|
|
|
int32_t ttisstdcnt = detzcode(up->tzhead.tzh_ttisstdcnt);
|
|
|
|
int32_t ttisutcnt = detzcode(up->tzhead.tzh_ttisutcnt);
|
|
|
|
int64_t prevtr = -1;
|
|
|
|
int32_t prevcorr = 0;
|
|
|
|
int32_t leapcnt = detzcode(up->tzhead.tzh_leapcnt);
|
|
|
|
int32_t timecnt = detzcode(up->tzhead.tzh_timecnt);
|
|
|
|
int32_t typecnt = detzcode(up->tzhead.tzh_typecnt);
|
|
|
|
int32_t charcnt = detzcode(up->tzhead.tzh_charcnt);
|
2022-04-27 12:39:39 +00:00
|
|
|
char const *p = up->buf + tzheadsize;
|
|
|
|
/* Although tzfile(5) currently requires typecnt to be nonzero,
|
|
|
|
support future formats that may allow zero typecnt
|
|
|
|
in files that have a TZ string and no transitions. */
|
|
|
|
if (! (0 <= leapcnt && leapcnt < TZ_MAX_LEAPS
|
|
|
|
&& 0 <= typecnt && typecnt < TZ_MAX_TYPES
|
|
|
|
&& 0 <= timecnt && timecnt < TZ_MAX_TIMES
|
|
|
|
&& 0 <= charcnt && charcnt < TZ_MAX_CHARS
|
|
|
|
&& 0 <= ttisstdcnt && ttisstdcnt < TZ_MAX_TYPES
|
|
|
|
&& 0 <= ttisutcnt && ttisutcnt < TZ_MAX_TYPES))
|
|
|
|
return EINVAL;
|
|
|
|
datablock_size
|
|
|
|
= (timecnt * stored /* ats */
|
|
|
|
+ timecnt /* types */
|
|
|
|
+ typecnt * 6 /* ttinfos */
|
|
|
|
+ charcnt /* chars */
|
|
|
|
+ leapcnt * (stored + 4) /* lsinfos */
|
|
|
|
+ ttisstdcnt /* ttisstds */
|
|
|
|
+ ttisutcnt); /* ttisuts */
|
|
|
|
if (nread < tzheadsize + datablock_size)
|
|
|
|
return EINVAL;
|
|
|
|
if (skip_datablock)
|
|
|
|
p += datablock_size;
|
|
|
|
else {
|
|
|
|
if (! ((ttisstdcnt == typecnt || ttisstdcnt == 0)
|
|
|
|
&& (ttisutcnt == typecnt || ttisutcnt == 0)))
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
sp->leapcnt = leapcnt;
|
|
|
|
sp->timecnt = timecnt;
|
|
|
|
sp->typecnt = typecnt;
|
|
|
|
sp->charcnt = charcnt;
|
|
|
|
|
2022-05-12 13:11:22 +00:00
|
|
|
/* Read transitions, discarding those out of time_t range.
|
|
|
|
But pretend the last transition before TIME_T_MIN
|
|
|
|
occurred at TIME_T_MIN. */
|
2022-04-27 12:39:39 +00:00
|
|
|
timecnt = 0;
|
2020-06-15 14:18:57 +00:00
|
|
|
for (i = 0; i < sp->timecnt; ++i) {
|
2023-10-11 21:54:42 +00:00
|
|
|
int64_t at
|
2022-04-27 12:39:39 +00:00
|
|
|
= stored == 4 ? detzcode(p) : detzcode64(p);
|
2022-05-12 13:11:22 +00:00
|
|
|
sp->types[i] = at <= TIME_T_MAX;
|
2022-04-27 12:39:39 +00:00
|
|
|
if (sp->types[i]) {
|
2022-05-12 13:11:22 +00:00
|
|
|
time_t attime
|
|
|
|
= ((TYPE_SIGNED(time_t) ? at < TIME_T_MIN : at < 0)
|
|
|
|
? TIME_T_MIN : at);
|
2022-04-27 12:39:39 +00:00
|
|
|
if (timecnt && attime <= sp->ats[timecnt - 1]) {
|
|
|
|
if (attime < sp->ats[timecnt - 1])
|
|
|
|
return EINVAL;
|
|
|
|
sp->types[i - 1] = 0;
|
|
|
|
timecnt--;
|
|
|
|
}
|
|
|
|
sp->ats[timecnt++] = attime;
|
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
p += stored;
|
|
|
|
}
|
2022-04-27 12:39:39 +00:00
|
|
|
|
|
|
|
timecnt = 0;
|
2020-06-15 14:18:57 +00:00
|
|
|
for (i = 0; i < sp->timecnt; ++i) {
|
2022-04-27 12:39:39 +00:00
|
|
|
unsigned char typ = *p++;
|
|
|
|
if (sp->typecnt <= typ)
|
|
|
|
return EINVAL;
|
|
|
|
if (sp->types[i])
|
|
|
|
sp->types[timecnt++] = typ;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-04-27 12:39:39 +00:00
|
|
|
sp->timecnt = timecnt;
|
2020-06-15 14:18:57 +00:00
|
|
|
for (i = 0; i < sp->typecnt; ++i) {
|
2022-04-27 12:39:39 +00:00
|
|
|
register struct ttinfo * ttisp;
|
|
|
|
unsigned char isdst, desigidx;
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
ttisp = &sp->ttis[i];
|
2022-04-27 12:39:39 +00:00
|
|
|
ttisp->tt_utoff = detzcode(p);
|
2020-06-15 14:18:57 +00:00
|
|
|
p += 4;
|
2022-04-27 12:39:39 +00:00
|
|
|
isdst = *p++;
|
|
|
|
if (! (isdst < 2))
|
|
|
|
return EINVAL;
|
|
|
|
ttisp->tt_isdst = isdst;
|
|
|
|
desigidx = *p++;
|
|
|
|
if (! (desigidx < sp->charcnt))
|
|
|
|
return EINVAL;
|
|
|
|
ttisp->tt_desigidx = desigidx;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < sp->charcnt; ++i)
|
|
|
|
sp->chars[i] = *p++;
|
2022-04-27 12:39:39 +00:00
|
|
|
/* Ensure '\0'-terminated, and make it safe to call
|
|
|
|
ttunspecified later. */
|
2023-07-06 13:57:28 +00:00
|
|
|
bzero(&sp->chars[i], CHARS_EXTRA);
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-05-12 13:11:22 +00:00
|
|
|
/* Read leap seconds, discarding those out of time_t range. */
|
2022-04-27 12:39:39 +00:00
|
|
|
leapcnt = 0;
|
|
|
|
for (i = 0; i < sp->leapcnt; ++i) {
|
2023-10-11 21:54:42 +00:00
|
|
|
int64_t tr = stored == 4 ? detzcode(p) : detzcode64(p);
|
|
|
|
int32_t corr = detzcode(p + stored);
|
2022-04-27 12:39:39 +00:00
|
|
|
p += stored + 4;
|
|
|
|
|
|
|
|
/* Leap seconds cannot occur before the Epoch,
|
|
|
|
or out of order. */
|
|
|
|
if (tr <= prevtr)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
/* To avoid other botches in this code, each leap second's
|
|
|
|
correction must differ from the previous one's by 1
|
|
|
|
second or less, except that the first correction can be
|
|
|
|
any value; these requirements are more generous than
|
|
|
|
RFC 8536, to allow future RFC extensions. */
|
|
|
|
if (! (i == 0
|
|
|
|
|| (prevcorr < corr
|
|
|
|
? corr == prevcorr + 1
|
|
|
|
: (corr == prevcorr
|
|
|
|
|| corr == prevcorr - 1))))
|
|
|
|
return EINVAL;
|
|
|
|
prevtr = tr;
|
|
|
|
prevcorr = corr;
|
|
|
|
|
2022-05-12 13:11:22 +00:00
|
|
|
if (tr <= TIME_T_MAX) {
|
2022-04-27 12:39:39 +00:00
|
|
|
sp->lsis[leapcnt].ls_trans = tr;
|
|
|
|
sp->lsis[leapcnt].ls_corr = corr;
|
|
|
|
leapcnt++;
|
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-04-27 12:39:39 +00:00
|
|
|
sp->leapcnt = leapcnt;
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
for (i = 0; i < sp->typecnt; ++i) {
|
2022-04-27 12:39:39 +00:00
|
|
|
register struct ttinfo * ttisp;
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
ttisp = &sp->ttis[i];
|
|
|
|
if (ttisstdcnt == 0)
|
2022-04-27 12:39:39 +00:00
|
|
|
ttisp->tt_ttisstd = false;
|
2020-06-15 14:18:57 +00:00
|
|
|
else {
|
2022-04-27 12:39:39 +00:00
|
|
|
if (*p != true && *p != false)
|
|
|
|
return EINVAL;
|
2020-06-15 14:18:57 +00:00
|
|
|
ttisp->tt_ttisstd = *p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < sp->typecnt; ++i) {
|
2022-04-27 12:39:39 +00:00
|
|
|
register struct ttinfo * ttisp;
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
ttisp = &sp->ttis[i];
|
2022-04-27 12:39:39 +00:00
|
|
|
if (ttisutcnt == 0)
|
|
|
|
ttisp->tt_ttisut = false;
|
2020-06-15 14:18:57 +00:00
|
|
|
else {
|
2022-04-27 12:39:39 +00:00
|
|
|
if (*p != true && *p != false)
|
|
|
|
return EINVAL;
|
|
|
|
ttisp->tt_ttisut = *p++;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-27 12:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nread -= p - up->buf;
|
|
|
|
memmove(up->buf, p, nread);
|
|
|
|
|
|
|
|
/* If this is an old file, we're done. */
|
|
|
|
if (!version)
|
|
|
|
break;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
if (doextend && nread > 2 &&
|
2022-04-27 12:39:39 +00:00
|
|
|
up->buf[0] == '\n' && up->buf[nread - 1] == '\n' &&
|
|
|
|
sp->typecnt + 2 <= TZ_MAX_TYPES) {
|
|
|
|
struct state *ts = &lsp->u.st;
|
|
|
|
|
|
|
|
up->buf[nread - 1] = '\0';
|
2022-04-29 07:36:14 +00:00
|
|
|
if (localtime_tzparse(&up->buf[1], ts, sp)) {
|
2022-04-27 12:39:39 +00:00
|
|
|
|
|
|
|
/* Attempt to reuse existing abbreviations.
|
|
|
|
Without this, America/Anchorage would be right on
|
|
|
|
the edge after 2037 when TZ_MAX_CHARS is 50, as
|
|
|
|
sp->charcnt equals 40 (for LMT AST AWT APT AHST
|
|
|
|
AHDT YST AKDT AKST) and ts->charcnt equals 10
|
|
|
|
(for AKST AKDT). Reusing means sp->charcnt can
|
|
|
|
stay 40 in this example. */
|
|
|
|
int gotabbr = 0;
|
|
|
|
int charcnt = sp->charcnt;
|
|
|
|
for (i = 0; i < ts->typecnt; i++) {
|
|
|
|
char *tsabbr = ts->chars + ts->ttis[i].tt_desigidx;
|
|
|
|
int j;
|
|
|
|
for (j = 0; j < charcnt; j++)
|
|
|
|
if (strcmp(sp->chars + j, tsabbr) == 0) {
|
|
|
|
ts->ttis[i].tt_desigidx = j;
|
|
|
|
gotabbr++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (! (j < charcnt)) {
|
|
|
|
int tsabbrlen = strlen(tsabbr);
|
|
|
|
if (j + tsabbrlen < TZ_MAX_CHARS) {
|
|
|
|
strcpy(sp->chars + j, tsabbr);
|
|
|
|
charcnt = j + tsabbrlen + 1;
|
|
|
|
ts->ttis[i].tt_desigidx = j;
|
|
|
|
gotabbr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (gotabbr == ts->typecnt) {
|
|
|
|
sp->charcnt = charcnt;
|
|
|
|
|
|
|
|
/* Ignore any trailing, no-op transitions generated
|
|
|
|
by zic as they don't help here and can run afoul
|
|
|
|
of bugs in zic 2016j or earlier. */
|
|
|
|
while (1 < sp->timecnt
|
|
|
|
&& (sp->types[sp->timecnt - 1]
|
|
|
|
== sp->types[sp->timecnt - 2]))
|
|
|
|
sp->timecnt--;
|
|
|
|
|
|
|
|
for (i = 0;
|
|
|
|
i < ts->timecnt && sp->timecnt < TZ_MAX_TIMES;
|
|
|
|
i++) {
|
2022-05-12 13:11:22 +00:00
|
|
|
time_t t = ts->ats[i];
|
|
|
|
if (increment_overflow_time(&t, leapcorr(sp, t))
|
2022-04-27 12:39:39 +00:00
|
|
|
|| (0 < sp->timecnt
|
|
|
|
&& t <= sp->ats[sp->timecnt - 1]))
|
|
|
|
continue;
|
|
|
|
sp->ats[sp->timecnt] = t;
|
|
|
|
sp->types[sp->timecnt] = (sp->typecnt
|
|
|
|
+ ts->types[i]);
|
|
|
|
sp->timecnt++;
|
|
|
|
}
|
|
|
|
for (i = 0; i < ts->typecnt; i++)
|
|
|
|
sp->ttis[sp->typecnt++] = ts->ttis[i];
|
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-27 12:39:39 +00:00
|
|
|
if (sp->typecnt == 0)
|
|
|
|
return EINVAL;
|
2020-06-15 14:18:57 +00:00
|
|
|
if (sp->timecnt > 1) {
|
2022-05-12 13:11:22 +00:00
|
|
|
if (sp->ats[0] <= TIME_T_MAX - SECSPERREPEAT) {
|
|
|
|
time_t repeatat = sp->ats[0] + SECSPERREPEAT;
|
2022-04-27 12:39:39 +00:00
|
|
|
int repeattype = sp->types[0];
|
|
|
|
for (i = 1; i < sp->timecnt; ++i)
|
|
|
|
if (sp->ats[i] == repeatat
|
2022-04-29 07:36:14 +00:00
|
|
|
&& localtime_typesequiv(sp, sp->types[i], repeattype)) {
|
|
|
|
sp->goback = true;
|
|
|
|
break;
|
2022-04-27 12:39:39 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-12 13:11:22 +00:00
|
|
|
if (TIME_T_MIN + SECSPERREPEAT <= sp->ats[sp->timecnt - 1]) {
|
|
|
|
time_t repeatat = sp->ats[sp->timecnt - 1] - SECSPERREPEAT;
|
2022-04-27 12:39:39 +00:00
|
|
|
int repeattype = sp->types[sp->timecnt - 1];
|
|
|
|
for (i = sp->timecnt - 2; i >= 0; --i)
|
|
|
|
if (sp->ats[i] == repeatat
|
2022-04-29 07:36:14 +00:00
|
|
|
&& localtime_typesequiv(sp, sp->types[i], repeattype)) {
|
|
|
|
sp->goahead = true;
|
|
|
|
break;
|
2022-04-27 12:39:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Infer sp->defaulttype from the data. Although this default
|
|
|
|
type is always zero for data from recent tzdb releases,
|
|
|
|
things are trickier for data from tzdb 2018e or earlier.
|
|
|
|
|
|
|
|
The first set of heuristics work around bugs in 32-bit data
|
|
|
|
generated by tzdb 2013c or earlier. The workaround is for
|
|
|
|
zones like Australia/Macquarie where timestamps before the
|
|
|
|
first transition have a time type that is not the earliest
|
|
|
|
standard-time type. See:
|
|
|
|
https://mm.icann.org/pipermail/tz/2013-May/019368.html */
|
|
|
|
/*
|
|
|
|
** If type 0 does not specify local time, or is unused in transitions,
|
|
|
|
** it's the type to use for early times.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < sp->timecnt; ++i)
|
|
|
|
if (sp->types[i] == 0)
|
|
|
|
break;
|
|
|
|
i = i < sp->timecnt && ! ttunspecified(sp, 0) ? -1 : 0;
|
|
|
|
/*
|
|
|
|
** Absent the above,
|
|
|
|
** if there are transition times
|
|
|
|
** and the first transition is to a daylight time
|
|
|
|
** find the standard type less than and closest to
|
|
|
|
** the type of the first transition.
|
|
|
|
*/
|
|
|
|
if (i < 0 && sp->timecnt > 0 && sp->ttis[sp->types[0]].tt_isdst) {
|
|
|
|
i = sp->types[0];
|
|
|
|
while (--i >= 0)
|
|
|
|
if (!sp->ttis[i].tt_isdst)
|
2020-06-15 14:18:57 +00:00
|
|
|
break;
|
2022-04-27 12:39:39 +00:00
|
|
|
}
|
|
|
|
/* The next heuristics are for data generated by tzdb 2018e or
|
|
|
|
earlier, for zones like EST5EDT where the first transition
|
|
|
|
is to DST. */
|
|
|
|
/*
|
|
|
|
** If no result yet, find the first standard type.
|
|
|
|
** If there is none, punt to type zero.
|
|
|
|
*/
|
|
|
|
if (i < 0) {
|
|
|
|
i = 0;
|
|
|
|
while (sp->ttis[i].tt_isdst)
|
|
|
|
if (++i >= sp->typecnt) {
|
|
|
|
i = 0;
|
2020-06-15 14:18:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-04-27 12:39:39 +00:00
|
|
|
/* A simple 'sp->defaulttype = 0;' would suffice here if we
|
|
|
|
didn't have to worry about 2018e-or-earlier data. Even
|
|
|
|
simpler would be to remove the defaulttype member and just
|
|
|
|
use 0 in its place. */
|
|
|
|
sp->defaulttype = i;
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
static int /* [jart] pthread cancelation safe */
|
2022-11-06 02:49:41 +00:00
|
|
|
localtime_tzloadbody(char const *name, struct state *sp, bool doextend,
|
|
|
|
union local_storage *lsp)
|
|
|
|
{
|
|
|
|
int rc;
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
BLOCK_CANCELATION;
|
2022-11-06 02:49:41 +00:00
|
|
|
rc = localtime_tzloadbody_(name, sp, doextend, lsp);
|
Make improvements
- We now serialize the file descriptor table when spawning / executing
processes on Windows. This means you can now inherit more stuff than
just standard i/o. It's needed by bash, which duplicates the console
to file descriptor #255. We also now do a better job serializing the
environment variables, so you're less likely to encounter E2BIG when
using your bash shell. We also no longer coerce environ to uppercase
- execve() on Windows now remotely controls its parent process to make
them spawn a replacement for itself. Then it'll be able to terminate
immediately once the spawn succeeds, without having to linger around
for the lifetime as a shell process for proxying the exit code. When
process worker thread running in the parent sees the child die, it's
given a handle to the new child, to replace it in the process table.
- execve() and posix_spawn() on Windows will now provide CreateProcess
an explicit handle list. This allows us to remove handle locks which
enables better fork/spawn concurrency, with seriously correct thread
safety. Other codebases like Go use the same technique. On the other
hand fork() still favors the conventional WIN32 inheritence approach
which can be a little bit messy, but is *controlled* by guaranteeing
perfectly clean slates at both the spawning and execution boundaries
- sigset_t is now 64 bits. Having it be 128 bits was a mistake because
there's no reason to use that and it's only supported by FreeBSD. By
using the system word size, signal mask manipulation on Windows goes
very fast. Furthermore @asyncsignalsafe funcs have been rewritten on
Windows to take advantage of signal masking, now that it's much more
pleasant to use.
- All the overlapped i/o code on Windows has been rewritten for pretty
good signal and cancelation safety. We're now able to ensure overlap
data structures are cleaned up so long as you don't longjmp() out of
out of a signal handler that interrupted an i/o operation. Latencies
are also improved thanks to the removal of lots of "busy wait" code.
Waits should be optimal for everything except poll(), which shall be
the last and final demon we slay in the win32 i/o horror show.
- getrusage() on Windows is now able to report RUSAGE_CHILDREN as well
as RUSAGE_SELF, thanks to aggregation in the process manager thread.
2023-10-08 12:36:18 +00:00
|
|
|
ALLOW_CANCELATION;
|
2022-11-06 02:49:41 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2022-04-27 12:39:39 +00:00
|
|
|
/* Load tz data from the file named NAME into *SP. Read extended
|
|
|
|
format if DOEXTEND. Return 0 on success, an errno value on failure. */
|
|
|
|
static int
|
2022-04-29 07:36:14 +00:00
|
|
|
localtime_tzload(char const *name, struct state *sp, bool doextend)
|
2022-04-27 12:39:39 +00:00
|
|
|
{
|
|
|
|
union local_storage *lsp = malloc(sizeof *lsp);
|
|
|
|
if (!lsp) {
|
|
|
|
return HAVE_MALLOC_ERRNO ? errno : ENOMEM;
|
|
|
|
} else {
|
2022-04-29 07:36:14 +00:00
|
|
|
int err = localtime_tzloadbody(name, sp, doextend, lsp);
|
2022-04-27 12:39:39 +00:00
|
|
|
free(lsp);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2022-04-29 07:36:14 +00:00
|
|
|
localtime_typesequiv(const struct state *sp, int a, int b)
|
2022-04-27 12:39:39 +00:00
|
|
|
{
|
|
|
|
register bool result;
|
|
|
|
|
|
|
|
if (sp == NULL ||
|
|
|
|
a < 0 || a >= sp->typecnt ||
|
|
|
|
b < 0 || b >= sp->typecnt)
|
|
|
|
result = false;
|
|
|
|
else {
|
|
|
|
register const struct ttinfo * ap = &sp->ttis[a];
|
|
|
|
register const struct ttinfo * bp = &sp->ttis[b];
|
|
|
|
result = (ap->tt_utoff == bp->tt_utoff
|
|
|
|
&& ap->tt_isdst == bp->tt_isdst
|
|
|
|
&& ap->tt_ttisstd == bp->tt_ttisstd
|
|
|
|
&& ap->tt_ttisut == bp->tt_ttisut
|
|
|
|
&& (strcmp(&sp->chars[ap->tt_desigidx],
|
|
|
|
&sp->chars[bp->tt_desigidx])
|
|
|
|
== 0));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-05-12 13:11:22 +00:00
|
|
|
static const int mon_lengths[2][MONSPERYEAR] = {
|
2020-06-15 14:18:57 +00:00
|
|
|
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
|
|
|
|
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
|
|
|
|
};
|
|
|
|
|
2022-04-27 12:39:39 +00:00
|
|
|
static const int year_lengths[2] = {
|
2020-06-15 14:18:57 +00:00
|
|
|
DAYSPERNYEAR, DAYSPERLYEAR
|
|
|
|
};
|
|
|
|
|
2022-04-27 12:39:39 +00:00
|
|
|
/* Is C an ASCII digit? */
|
|
|
|
static inline bool
|
|
|
|
is_digit(char c)
|
|
|
|
{
|
2022-05-12 13:11:22 +00:00
|
|
|
return '0' <= c && c <= '9';
|
2022-04-27 12:39:39 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
/*
|
2022-04-27 12:39:39 +00:00
|
|
|
** Given a pointer into a timezone string, scan until a character that is not
|
|
|
|
** a valid character in a time zone abbreviation is found.
|
|
|
|
** Return a pointer to that character.
|
2020-06-15 14:18:57 +00:00
|
|
|
*/
|
|
|
|
|
2022-04-27 12:39:39 +00:00
|
|
|
static nosideeffect const char *
|
|
|
|
getzname(register const char *strp)
|
|
|
|
{
|
|
|
|
register char c;
|
|
|
|
|
|
|
|
while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
|
|
|
|
c != '+')
|
|
|
|
++strp;
|
2020-06-15 14:18:57 +00:00
|
|
|
return strp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-04-27 12:39:39 +00:00
|
|
|
** Given a pointer into an extended timezone string, scan until the ending
|
|
|
|
** delimiter of the time zone abbreviation is located.
|
|
|
|
** Return a pointer to the delimiter.
|
2020-06-15 14:18:57 +00:00
|
|
|
**
|
|
|
|
** As with getzname above, the legal character set is actually quite
|
|
|
|
** restricted, with other characters producing undefined results.
|
|
|
|
** We don't do any checking here; checking is done later in common-case code.
|
|
|
|
*/
|
|
|
|
|
2022-04-27 12:39:39 +00:00
|
|
|
static nosideeffect const char *
|
|
|
|
getqzname(register const char *strp, const int delim)
|
|
|
|
{
|
|
|
|
register int c;
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
while ((c = *strp) != '\0' && c != delim)
|
|
|
|
++strp;
|
|
|
|
return strp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-04-27 12:39:39 +00:00
|
|
|
** Given a pointer into a timezone string, extract a number from that string.
|
2020-06-15 14:18:57 +00:00
|
|
|
** Check that the number is within a specified range; if it is not, return
|
|
|
|
** NULL.
|
|
|
|
** Otherwise, return a pointer to the first character not part of the number.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const char *
|
2022-04-27 12:39:39 +00:00
|
|
|
getnum(register const char *strp, int *const nump, const int min, const int max)
|
|
|
|
{
|
|
|
|
register char c;
|
|
|
|
register int num;
|
|
|
|
|
|
|
|
if (strp == NULL || !is_digit(c = *strp))
|
2020-06-15 14:18:57 +00:00
|
|
|
return NULL;
|
|
|
|
num = 0;
|
|
|
|
do {
|
|
|
|
num = num * 10 + (c - '0');
|
|
|
|
if (num > max)
|
|
|
|
return NULL; /* illegal value */
|
|
|
|
c = *++strp;
|
2022-04-27 12:39:39 +00:00
|
|
|
} while (is_digit(c));
|
2020-06-15 14:18:57 +00:00
|
|
|
if (num < min)
|
|
|
|
return NULL; /* illegal value */
|
|
|
|
*nump = num;
|
|
|
|
return strp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-04-27 12:39:39 +00:00
|
|
|
** Given a pointer into a timezone string, extract a number of seconds,
|
2020-06-15 14:18:57 +00:00
|
|
|
** in hh[:mm[:ss]] form, from the string.
|
|
|
|
** If any error occurs, return NULL.
|
|
|
|
** Otherwise, return a pointer to the first character not part of the number
|
|
|
|
** of seconds.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const char *
|
2023-10-11 21:54:42 +00:00
|
|
|
getsecs(register const char *strp, int32_t *const secsp)
|
2022-04-27 12:39:39 +00:00
|
|
|
{
|
|
|
|
int num;
|
2023-10-11 21:54:42 +00:00
|
|
|
int32_t secsperhour = SECSPERHOUR;
|
2022-04-27 12:39:39 +00:00
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
/*
|
2022-04-27 12:39:39 +00:00
|
|
|
** 'HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
|
2020-06-15 14:18:57 +00:00
|
|
|
** "M10.4.6/26", which does not conform to Posix,
|
|
|
|
** but which specifies the equivalent of
|
2022-04-27 12:39:39 +00:00
|
|
|
** "02:00 on the first Sunday on or after 23 Oct".
|
2020-06-15 14:18:57 +00:00
|
|
|
*/
|
|
|
|
strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
|
|
|
|
if (strp == NULL)
|
|
|
|
return NULL;
|
2022-04-27 12:39:39 +00:00
|
|
|
*secsp = num * secsperhour;
|
2020-06-15 14:18:57 +00:00
|
|
|
if (*strp == ':') {
|
|
|
|
++strp;
|
|
|
|
strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
|
|
|
|
if (strp == NULL)
|
|
|
|
return NULL;
|
|
|
|
*secsp += num * SECSPERMIN;
|
|
|
|
if (*strp == ':') {
|
|
|
|
++strp;
|
2022-04-27 12:39:39 +00:00
|
|
|
/* 'SECSPERMIN' allows for leap seconds. */
|
2020-06-15 14:18:57 +00:00
|
|
|
strp = getnum(strp, &num, 0, SECSPERMIN);
|
|
|
|
if (strp == NULL)
|
|
|
|
return NULL;
|
|
|
|
*secsp += num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-04-27 12:39:39 +00:00
|
|
|
** Given a pointer into a timezone string, extract an offset, in
|
2020-06-15 14:18:57 +00:00
|
|
|
** [+-]hh[:mm[:ss]] form, from the string.
|
|
|
|
** If any error occurs, return NULL.
|
|
|
|
** Otherwise, return a pointer to the first character not part of the time.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const char *
|
2023-10-11 21:54:42 +00:00
|
|
|
getoffset(register const char *strp, int32_t *const offsetp)
|
2022-04-27 12:39:39 +00:00
|
|
|
{
|
|
|
|
register bool neg = false;
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
if (*strp == '-') {
|
2022-04-27 12:39:39 +00:00
|
|
|
neg = true;
|
2020-06-15 14:18:57 +00:00
|
|
|
++strp;
|
|
|
|
} else if (*strp == '+')
|
|
|
|
++strp;
|
|
|
|
strp = getsecs(strp, offsetp);
|
|
|
|
if (strp == NULL)
|
|
|
|
return NULL; /* illegal time */
|
|
|
|
if (neg)
|
|
|
|
*offsetp = -*offsetp;
|
|
|
|
return strp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-04-27 12:39:39 +00:00
|
|
|
** Given a pointer into a timezone string, extract a rule in the form
|
2020-06-15 14:18:57 +00:00
|
|
|
** date[/time]. See POSIX section 8 for the format of "date" and "time".
|
|
|
|
** If a valid rule is not found, return NULL.
|
|
|
|
** Otherwise, return a pointer to the first character not part of the rule.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const char *
|
2022-04-27 12:39:39 +00:00
|
|
|
getrule(const char *strp, register struct rule *const rulep)
|
|
|
|
{
|
2020-06-15 14:18:57 +00:00
|
|
|
if (*strp == 'J') {
|
|
|
|
/*
|
|
|
|
** Julian day.
|
|
|
|
*/
|
|
|
|
rulep->r_type = JULIAN_DAY;
|
|
|
|
++strp;
|
|
|
|
strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
|
|
|
|
} else if (*strp == 'M') {
|
|
|
|
/*
|
|
|
|
** Month, week, day.
|
|
|
|
*/
|
|
|
|
rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
|
|
|
|
++strp;
|
|
|
|
strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
|
|
|
|
if (strp == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (*strp++ != '.')
|
|
|
|
return NULL;
|
|
|
|
strp = getnum(strp, &rulep->r_week, 1, 5);
|
|
|
|
if (strp == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (*strp++ != '.')
|
|
|
|
return NULL;
|
|
|
|
strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
|
2022-04-27 12:39:39 +00:00
|
|
|
} else if (is_digit(*strp)) {
|
2020-06-15 14:18:57 +00:00
|
|
|
/*
|
|
|
|
** Day of year.
|
|
|
|
*/
|
|
|
|
rulep->r_type = DAY_OF_YEAR;
|
|
|
|
strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
|
|
|
|
} else return NULL; /* invalid format */
|
|
|
|
if (strp == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (*strp == '/') {
|
|
|
|
/*
|
|
|
|
** Time specified.
|
|
|
|
*/
|
|
|
|
++strp;
|
2022-04-27 12:39:39 +00:00
|
|
|
strp = getoffset(strp, &rulep->r_time);
|
2020-06-15 14:18:57 +00:00
|
|
|
} else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
|
|
|
|
return strp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-04-27 12:39:39 +00:00
|
|
|
** Given a year, a rule, and the offset from UT at the time that rule takes
|
|
|
|
** effect, calculate the year-relative time that rule takes effect.
|
2020-06-15 14:18:57 +00:00
|
|
|
*/
|
|
|
|
|
2023-10-11 21:54:42 +00:00
|
|
|
static int32_t
|
2022-05-12 13:11:22 +00:00
|
|
|
transtime(const int year, register const struct rule *const rulep,
|
2023-10-11 21:54:42 +00:00
|
|
|
const int32_t offset)
|
2022-04-27 12:39:39 +00:00
|
|
|
{
|
2022-05-12 13:11:22 +00:00
|
|
|
register bool leapyear;
|
2023-10-11 21:54:42 +00:00
|
|
|
register int32_t value;
|
2022-05-12 13:11:22 +00:00
|
|
|
register int i;
|
|
|
|
int d, m1, yy0, yy1, yy2, dow;
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-05-12 13:11:22 +00:00
|
|
|
leapyear = isleap(year);
|
2020-06-15 14:18:57 +00:00
|
|
|
switch (rulep->r_type) {
|
|
|
|
|
|
|
|
case JULIAN_DAY:
|
|
|
|
/*
|
|
|
|
** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
|
|
|
|
** years.
|
|
|
|
** In non-leap years, or if the day number is 59 or less, just
|
|
|
|
** add SECSPERDAY times the day number-1 to the time of
|
|
|
|
** January 1, midnight, to get the day.
|
|
|
|
*/
|
2022-04-27 12:39:39 +00:00
|
|
|
value = (rulep->r_day - 1) * SECSPERDAY;
|
2020-06-15 14:18:57 +00:00
|
|
|
if (leapyear && rulep->r_day >= 60)
|
|
|
|
value += SECSPERDAY;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DAY_OF_YEAR:
|
|
|
|
/*
|
|
|
|
** n - day of year.
|
|
|
|
** Just add SECSPERDAY times the day number to the time of
|
|
|
|
** January 1, midnight, to get the day.
|
|
|
|
*/
|
2022-04-27 12:39:39 +00:00
|
|
|
value = rulep->r_day * SECSPERDAY;
|
2020-06-15 14:18:57 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MONTH_NTH_DAY_OF_WEEK:
|
|
|
|
/*
|
|
|
|
** Mm.n.d - nth "dth day" of month m.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Use Zeller's Congruence to get day-of-week of first day of
|
|
|
|
** month.
|
|
|
|
*/
|
|
|
|
m1 = (rulep->r_mon + 9) % 12 + 1;
|
|
|
|
yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
|
2020-10-11 04:18:53 +00:00
|
|
|
yy1 = yy0 / 100;
|
|
|
|
yy2 = yy0 % 100;
|
|
|
|
dow = ((26 * m1 - 2) / 10 +
|
2020-06-15 14:18:57 +00:00
|
|
|
1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
|
|
|
|
if (dow < 0)
|
|
|
|
dow += DAYSPERWEEK;
|
|
|
|
|
|
|
|
/*
|
|
|
|
** "dow" is the day-of-week of the first day of the month. Get
|
|
|
|
** the day-of-month (zero-origin) of the first "dow" day of the
|
|
|
|
** month.
|
|
|
|
*/
|
|
|
|
d = rulep->r_day - dow;
|
|
|
|
if (d < 0)
|
|
|
|
d += DAYSPERWEEK;
|
|
|
|
for (i = 1; i < rulep->r_week; ++i) {
|
|
|
|
if (d + DAYSPERWEEK >=
|
2022-04-27 12:39:39 +00:00
|
|
|
mon_lengths[leapyear][rulep->r_mon - 1])
|
2020-06-15 14:18:57 +00:00
|
|
|
break;
|
|
|
|
d += DAYSPERWEEK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** "d" is the day-of-month (zero-origin) of the day we want.
|
|
|
|
*/
|
2022-04-27 12:39:39 +00:00
|
|
|
value = d * SECSPERDAY;
|
|
|
|
for (i = 0; i < rulep->r_mon - 1; ++i)
|
|
|
|
value += mon_lengths[leapyear][i] * SECSPERDAY;
|
2020-06-15 14:18:57 +00:00
|
|
|
break;
|
2022-04-27 12:39:39 +00:00
|
|
|
|
2022-05-12 13:11:22 +00:00
|
|
|
default: UNREACHABLE();
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-04-27 12:39:39 +00:00
|
|
|
** "value" is the year-relative time of 00:00:00 UT on the day in
|
|
|
|
** question. To get the year-relative time of the specified local
|
2020-06-15 14:18:57 +00:00
|
|
|
** time on that day, add the transition time and the current offset
|
2022-04-27 12:39:39 +00:00
|
|
|
** from UT.
|
2020-06-15 14:18:57 +00:00
|
|
|
*/
|
|
|
|
return value + rulep->r_time + offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Given a POSIX section 8-style TZ string, fill in the rule tables as
|
|
|
|
** appropriate.
|
|
|
|
*/
|
|
|
|
|
2022-05-12 13:11:22 +00:00
|
|
|
static bool
|
2022-04-29 07:36:14 +00:00
|
|
|
localtime_tzparse(const char *name, struct state *sp, struct state *basep)
|
2022-04-27 12:39:39 +00:00
|
|
|
{
|
|
|
|
const char * stdname;
|
|
|
|
const char * dstname;
|
|
|
|
size_t stdlen;
|
|
|
|
size_t dstlen;
|
|
|
|
size_t charcnt;
|
2023-10-11 21:54:42 +00:00
|
|
|
int32_t stdoffset;
|
|
|
|
int32_t dstoffset;
|
2022-04-27 12:39:39 +00:00
|
|
|
register char * cp;
|
|
|
|
register bool load_ok;
|
2022-05-12 13:11:22 +00:00
|
|
|
time_t atlo = TIME_T_MIN, leaplo = TIME_T_MIN;
|
2022-04-27 12:39:39 +00:00
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
stdname = name;
|
2022-04-27 12:39:39 +00:00
|
|
|
if (*name == '<') {
|
|
|
|
name++;
|
|
|
|
stdname = name;
|
|
|
|
name = getqzname(name, '>');
|
|
|
|
if (*name != '>')
|
|
|
|
return false;
|
|
|
|
stdlen = name - stdname;
|
|
|
|
name++;
|
2020-06-15 14:18:57 +00:00
|
|
|
} else {
|
2022-04-27 12:39:39 +00:00
|
|
|
name = getzname(name);
|
|
|
|
stdlen = name - stdname;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-04-27 12:39:39 +00:00
|
|
|
if (!stdlen)
|
|
|
|
return false;
|
|
|
|
name = getoffset(name, &stdoffset);
|
|
|
|
if (name == NULL)
|
|
|
|
return false;
|
|
|
|
charcnt = stdlen + 1;
|
|
|
|
if (sizeof sp->chars < charcnt)
|
|
|
|
return false;
|
|
|
|
if (basep) {
|
|
|
|
if (0 < basep->timecnt)
|
|
|
|
atlo = basep->ats[basep->timecnt - 1];
|
|
|
|
load_ok = false;
|
|
|
|
sp->leapcnt = basep->leapcnt;
|
|
|
|
memcpy(sp->lsis, basep->lsis, sp->leapcnt * sizeof *sp->lsis);
|
|
|
|
} else {
|
2022-04-29 07:36:14 +00:00
|
|
|
load_ok = localtime_tzload(TZDEFRULES, sp, false) == 0;
|
2022-04-27 12:39:39 +00:00
|
|
|
if (!load_ok)
|
|
|
|
sp->leapcnt = 0; /* So, we're off a little. */
|
|
|
|
}
|
|
|
|
if (0 < sp->leapcnt)
|
|
|
|
leaplo = sp->lsis[sp->leapcnt - 1].ls_trans;
|
2020-06-15 14:18:57 +00:00
|
|
|
if (*name != '\0') {
|
|
|
|
if (*name == '<') {
|
|
|
|
dstname = ++name;
|
|
|
|
name = getqzname(name, '>');
|
|
|
|
if (*name != '>')
|
2022-04-27 12:39:39 +00:00
|
|
|
return false;
|
2020-06-15 14:18:57 +00:00
|
|
|
dstlen = name - dstname;
|
|
|
|
name++;
|
|
|
|
} else {
|
|
|
|
dstname = name;
|
|
|
|
name = getzname(name);
|
2022-04-27 12:39:39 +00:00
|
|
|
dstlen = name - dstname; /* length of DST abbr. */
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-04-27 12:39:39 +00:00
|
|
|
if (!dstlen)
|
|
|
|
return false;
|
|
|
|
charcnt += dstlen + 1;
|
|
|
|
if (sizeof sp->chars < charcnt)
|
|
|
|
return false;
|
2020-06-15 14:18:57 +00:00
|
|
|
if (*name != '\0' && *name != ',' && *name != ';') {
|
|
|
|
name = getoffset(name, &dstoffset);
|
|
|
|
if (name == NULL)
|
2022-04-27 12:39:39 +00:00
|
|
|
return false;
|
2020-06-15 14:18:57 +00:00
|
|
|
} else dstoffset = stdoffset - SECSPERHOUR;
|
2022-04-27 12:39:39 +00:00
|
|
|
if (*name == '\0' && !load_ok)
|
2020-06-15 14:18:57 +00:00
|
|
|
name = TZDEFRULESTRING;
|
|
|
|
if (*name == ',' || *name == ';') {
|
2022-05-12 13:11:22 +00:00
|
|
|
struct rule start;
|
|
|
|
struct rule end;
|
|
|
|
register int year;
|
|
|
|
register int timecnt;
|
|
|
|
time_t janfirst;
|
2023-10-11 21:54:42 +00:00
|
|
|
int32_t janoffset = 0;
|
2022-05-12 13:11:22 +00:00
|
|
|
int yearbeg, yearlim;
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
++name;
|
|
|
|
if ((name = getrule(name, &start)) == NULL)
|
2022-04-27 12:39:39 +00:00
|
|
|
return false;
|
2020-06-15 14:18:57 +00:00
|
|
|
if (*name++ != ',')
|
2022-04-27 12:39:39 +00:00
|
|
|
return false;
|
2020-06-15 14:18:57 +00:00
|
|
|
if ((name = getrule(name, &end)) == NULL)
|
2022-04-27 12:39:39 +00:00
|
|
|
return false;
|
2020-06-15 14:18:57 +00:00
|
|
|
if (*name != '\0')
|
2022-04-27 12:39:39 +00:00
|
|
|
return false;
|
2020-06-15 14:18:57 +00:00
|
|
|
sp->typecnt = 2; /* standard time and DST */
|
|
|
|
/*
|
|
|
|
** Two transitions per year, from EPOCH_YEAR forward.
|
|
|
|
*/
|
2022-04-27 12:39:39 +00:00
|
|
|
init_ttinfo(&sp->ttis[0], -stdoffset, false, 0);
|
|
|
|
init_ttinfo(&sp->ttis[1], -dstoffset, true, stdlen + 1);
|
|
|
|
sp->defaulttype = 0;
|
|
|
|
timecnt = 0;
|
2020-06-15 14:18:57 +00:00
|
|
|
janfirst = 0;
|
2022-04-27 12:39:39 +00:00
|
|
|
yearbeg = EPOCH_YEAR;
|
|
|
|
|
|
|
|
do {
|
2023-10-11 21:54:42 +00:00
|
|
|
int32_t yearsecs
|
2022-05-12 13:11:22 +00:00
|
|
|
= year_lengths[isleap(yearbeg - 1)] * SECSPERDAY;
|
2022-04-27 12:39:39 +00:00
|
|
|
yearbeg--;
|
2022-05-12 13:11:22 +00:00
|
|
|
if (increment_overflow_time(&janfirst, -yearsecs)) {
|
2022-04-27 12:39:39 +00:00
|
|
|
janoffset = -yearsecs;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (atlo < janfirst
|
|
|
|
&& EPOCH_YEAR - YEARSPERREPEAT / 2 < yearbeg);
|
|
|
|
|
|
|
|
while (true) {
|
2023-10-11 21:54:42 +00:00
|
|
|
int32_t yearsecs
|
2022-05-12 13:11:22 +00:00
|
|
|
= year_lengths[isleap(yearbeg)] * SECSPERDAY;
|
|
|
|
int yearbeg1 = yearbeg;
|
|
|
|
time_t janfirst1 = janfirst;
|
|
|
|
if (increment_overflow_time(&janfirst1, yearsecs)
|
2022-04-27 12:39:39 +00:00
|
|
|
|| increment_overflow(&yearbeg1, 1)
|
|
|
|
|| atlo <= janfirst1)
|
|
|
|
break;
|
|
|
|
yearbeg = yearbeg1;
|
|
|
|
janfirst = janfirst1;
|
|
|
|
}
|
|
|
|
|
|
|
|
yearlim = yearbeg;
|
|
|
|
if (increment_overflow(&yearlim, YEARSPERREPEAT + 1))
|
|
|
|
yearlim = INT_MAX;
|
|
|
|
for (year = yearbeg; year < yearlim; year++) {
|
2023-10-11 21:54:42 +00:00
|
|
|
int32_t
|
2022-05-12 13:11:22 +00:00
|
|
|
starttime = transtime(year, &start, stdoffset),
|
2022-04-27 12:39:39 +00:00
|
|
|
endtime = transtime(year, &end, dstoffset);
|
2023-10-11 21:54:42 +00:00
|
|
|
int32_t
|
2022-05-12 13:11:22 +00:00
|
|
|
yearsecs = (year_lengths[isleap(year)]
|
2022-04-27 12:39:39 +00:00
|
|
|
* SECSPERDAY);
|
|
|
|
bool reversed = endtime < starttime;
|
|
|
|
if (reversed) {
|
2023-10-11 21:54:42 +00:00
|
|
|
int32_t swap = starttime;
|
2022-04-27 12:39:39 +00:00
|
|
|
starttime = endtime;
|
|
|
|
endtime = swap;
|
|
|
|
}
|
|
|
|
if (reversed
|
|
|
|
|| (starttime < endtime
|
|
|
|
&& endtime - starttime < yearsecs)) {
|
|
|
|
if (TZ_MAX_TIMES - 2 < timecnt)
|
|
|
|
break;
|
|
|
|
sp->ats[timecnt] = janfirst;
|
2022-05-12 13:11:22 +00:00
|
|
|
if (! increment_overflow_time
|
2022-04-27 12:39:39 +00:00
|
|
|
(&sp->ats[timecnt],
|
|
|
|
janoffset + starttime)
|
|
|
|
&& atlo <= sp->ats[timecnt])
|
|
|
|
sp->types[timecnt++] = !reversed;
|
|
|
|
sp->ats[timecnt] = janfirst;
|
2022-05-12 13:11:22 +00:00
|
|
|
if (! increment_overflow_time
|
2022-04-27 12:39:39 +00:00
|
|
|
(&sp->ats[timecnt],
|
|
|
|
janoffset + endtime)
|
|
|
|
&& atlo <= sp->ats[timecnt]) {
|
|
|
|
sp->types[timecnt++] = reversed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (endtime < leaplo) {
|
|
|
|
yearlim = year;
|
|
|
|
if (increment_overflow(&yearlim,
|
|
|
|
YEARSPERREPEAT + 1))
|
|
|
|
yearlim = INT_MAX;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-05-12 13:11:22 +00:00
|
|
|
if (increment_overflow_time
|
2022-04-27 12:39:39 +00:00
|
|
|
(&janfirst, janoffset + yearsecs))
|
2020-06-15 14:18:57 +00:00
|
|
|
break;
|
2022-04-27 12:39:39 +00:00
|
|
|
janoffset = 0;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-04-27 12:39:39 +00:00
|
|
|
sp->timecnt = timecnt;
|
|
|
|
if (! timecnt) {
|
|
|
|
sp->ttis[0] = sp->ttis[1];
|
|
|
|
sp->typecnt = 1; /* Perpetual DST. */
|
|
|
|
} else if (YEARSPERREPEAT < year - yearbeg)
|
|
|
|
sp->goback = sp->goahead = true;
|
2020-06-15 14:18:57 +00:00
|
|
|
} else {
|
2023-10-11 21:54:42 +00:00
|
|
|
register int32_t theirstdoffset;
|
|
|
|
register int32_t theirdstoffset;
|
|
|
|
register int32_t theiroffset;
|
2022-04-27 12:39:39 +00:00
|
|
|
register bool isdst;
|
2020-06-15 14:18:57 +00:00
|
|
|
register int i;
|
|
|
|
register int j;
|
|
|
|
|
|
|
|
if (*name != '\0')
|
2022-04-27 12:39:39 +00:00
|
|
|
return false;
|
2020-06-15 14:18:57 +00:00
|
|
|
/*
|
|
|
|
** Initial values of theirstdoffset and theirdstoffset.
|
|
|
|
*/
|
|
|
|
theirstdoffset = 0;
|
|
|
|
for (i = 0; i < sp->timecnt; ++i) {
|
|
|
|
j = sp->types[i];
|
|
|
|
if (!sp->ttis[j].tt_isdst) {
|
|
|
|
theirstdoffset =
|
2022-04-27 12:39:39 +00:00
|
|
|
- sp->ttis[j].tt_utoff;
|
2020-06-15 14:18:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
theirdstoffset = 0;
|
|
|
|
for (i = 0; i < sp->timecnt; ++i) {
|
|
|
|
j = sp->types[i];
|
|
|
|
if (sp->ttis[j].tt_isdst) {
|
|
|
|
theirdstoffset =
|
2022-04-27 12:39:39 +00:00
|
|
|
- sp->ttis[j].tt_utoff;
|
2020-06-15 14:18:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
** Initially we're assumed to be in standard time.
|
|
|
|
*/
|
2022-04-27 12:39:39 +00:00
|
|
|
isdst = false;
|
2020-06-15 14:18:57 +00:00
|
|
|
/*
|
|
|
|
** Now juggle transition times and types
|
|
|
|
** tracking offsets as you do.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < sp->timecnt; ++i) {
|
|
|
|
j = sp->types[i];
|
|
|
|
sp->types[i] = sp->ttis[j].tt_isdst;
|
2022-04-27 12:39:39 +00:00
|
|
|
if (sp->ttis[j].tt_ttisut) {
|
2020-06-15 14:18:57 +00:00
|
|
|
/* No adjustment to transition time */
|
|
|
|
} else {
|
|
|
|
/*
|
2022-04-27 12:39:39 +00:00
|
|
|
** If daylight saving time is in
|
|
|
|
** effect, and the transition time was
|
|
|
|
** not specified as standard time, add
|
|
|
|
** the daylight saving time offset to
|
|
|
|
** the transition time; otherwise, add
|
|
|
|
** the standard time offset to the
|
|
|
|
** transition time.
|
2020-06-15 14:18:57 +00:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
** Transitions from DST to DDST
|
|
|
|
** will effectively disappear since
|
|
|
|
** POSIX provides for only one DST
|
|
|
|
** offset.
|
|
|
|
*/
|
|
|
|
if (isdst && !sp->ttis[j].tt_ttisstd) {
|
|
|
|
sp->ats[i] += dstoffset -
|
|
|
|
theirdstoffset;
|
|
|
|
} else {
|
|
|
|
sp->ats[i] += stdoffset -
|
|
|
|
theirstdoffset;
|
|
|
|
}
|
|
|
|
}
|
2022-04-27 12:39:39 +00:00
|
|
|
theiroffset = -sp->ttis[j].tt_utoff;
|
2020-06-15 14:18:57 +00:00
|
|
|
if (sp->ttis[j].tt_isdst)
|
|
|
|
theirdstoffset = theiroffset;
|
|
|
|
else theirstdoffset = theiroffset;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
** Finally, fill in ttis.
|
|
|
|
*/
|
2022-04-27 12:39:39 +00:00
|
|
|
init_ttinfo(&sp->ttis[0], -stdoffset, false, 0);
|
|
|
|
init_ttinfo(&sp->ttis[1], -dstoffset, true, stdlen + 1);
|
2020-06-15 14:18:57 +00:00
|
|
|
sp->typecnt = 2;
|
2022-04-27 12:39:39 +00:00
|
|
|
sp->defaulttype = 0;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dstlen = 0;
|
|
|
|
sp->typecnt = 1; /* only standard time */
|
|
|
|
sp->timecnt = 0;
|
2022-04-27 12:39:39 +00:00
|
|
|
init_ttinfo(&sp->ttis[0], -stdoffset, false, 0);
|
|
|
|
sp->defaulttype = 0;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-04-27 12:39:39 +00:00
|
|
|
sp->charcnt = charcnt;
|
2020-06-15 14:18:57 +00:00
|
|
|
cp = sp->chars;
|
2022-04-27 12:39:39 +00:00
|
|
|
memcpy(cp, stdname, stdlen);
|
2020-06-15 14:18:57 +00:00
|
|
|
cp += stdlen;
|
|
|
|
*cp++ = '\0';
|
|
|
|
if (dstlen != 0) {
|
2022-04-27 12:39:39 +00:00
|
|
|
memcpy(cp, dstname, dstlen);
|
2020-06-15 14:18:57 +00:00
|
|
|
*(cp + dstlen) = '\0';
|
|
|
|
}
|
2022-04-27 12:39:39 +00:00
|
|
|
return true;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-04-27 12:39:39 +00:00
|
|
|
gmtload(struct state *const sp)
|
|
|
|
{
|
2022-04-29 07:36:14 +00:00
|
|
|
if (localtime_tzload(gmt, sp, true) != 0)
|
|
|
|
localtime_tzparse("GMT0", sp, NULL);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-27 12:39:39 +00:00
|
|
|
/* Initialize *SP to a value appropriate for the TZ setting NAME.
|
|
|
|
Return 0 on success, an errno value on failure. */
|
|
|
|
static int
|
|
|
|
zoneinit(struct state *sp, char const *name)
|
|
|
|
{
|
|
|
|
if (name && ! name[0]) {
|
|
|
|
/*
|
|
|
|
** User wants it fast rather than right.
|
|
|
|
*/
|
|
|
|
sp->leapcnt = 0; /* so, we're off a little */
|
|
|
|
sp->timecnt = 0;
|
|
|
|
sp->typecnt = 0;
|
|
|
|
sp->charcnt = 0;
|
|
|
|
sp->goback = sp->goahead = false;
|
|
|
|
init_ttinfo(&sp->ttis[0], 0, false, 0);
|
|
|
|
strcpy(sp->chars, gmt);
|
|
|
|
sp->defaulttype = 0;
|
|
|
|
return 0;
|
|
|
|
} else {
|
2022-04-29 07:36:14 +00:00
|
|
|
int err = localtime_tzload(name, sp, true);
|
|
|
|
if (err != 0 && name && name[0] != ':' &&
|
|
|
|
localtime_tzparse(name, sp, NULL))
|
2022-04-27 12:39:39 +00:00
|
|
|
err = 0;
|
|
|
|
if (err == 0)
|
|
|
|
scrub_abbrs(sp);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 20:20:08 +00:00
|
|
|
static void
|
|
|
|
FreeLocaltime(void *p) {
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
2022-04-27 12:39:39 +00:00
|
|
|
static void
|
2022-04-29 07:36:14 +00:00
|
|
|
localtime_tzset_unlocked(void)
|
2020-06-15 14:18:57 +00:00
|
|
|
{
|
2022-04-27 12:39:39 +00:00
|
|
|
char const *name = getenv("TZ");
|
|
|
|
struct state *sp = lclptr;
|
|
|
|
int lcl = name ? strlen(name) < sizeof lcl_TZname : -1;
|
|
|
|
if (lcl < 0
|
|
|
|
? lcl_is_set < 0
|
|
|
|
: 0 < lcl_is_set && strcmp(lcl_TZname, name) == 0)
|
2020-06-15 14:18:57 +00:00
|
|
|
return;
|
2022-05-16 20:20:08 +00:00
|
|
|
if (!sp) {
|
2022-04-27 12:39:39 +00:00
|
|
|
lclptr = sp = malloc(sizeof *lclptr);
|
2022-05-16 20:20:08 +00:00
|
|
|
__cxa_atexit(FreeLocaltime, sp, 0);
|
|
|
|
}
|
2022-04-27 12:39:39 +00:00
|
|
|
if (sp) {
|
|
|
|
if (zoneinit(sp, name) != 0)
|
|
|
|
zoneinit(sp, "");
|
|
|
|
if (0 < lcl)
|
|
|
|
strcpy(lcl_TZname, name);
|
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
settzname();
|
2022-04-27 12:39:39 +00:00
|
|
|
lcl_is_set = lcl;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-07 04:39:00 +00:00
|
|
|
tzset(void)
|
2020-06-15 14:18:57 +00:00
|
|
|
{
|
2022-08-13 21:18:02 +00:00
|
|
|
localtime_lock();
|
2022-04-29 07:36:14 +00:00
|
|
|
localtime_tzset_unlocked();
|
2022-06-12 18:47:20 +00:00
|
|
|
localtime_unlock();
|
2022-04-27 12:39:39 +00:00
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2023-06-09 06:44:03 +00:00
|
|
|
static void
|
|
|
|
gmtcheck(void)
|
|
|
|
{
|
|
|
|
static bool gmt_is_set;
|
|
|
|
localtime_lock();
|
|
|
|
if (! gmt_is_set) {
|
|
|
|
gmtptr = malloc(sizeof *gmtptr);
|
|
|
|
if (gmtptr)
|
|
|
|
gmtload(gmtptr);
|
|
|
|
gmt_is_set = true;
|
|
|
|
}
|
|
|
|
localtime_unlock();
|
|
|
|
}
|
|
|
|
|
2022-05-16 20:20:08 +00:00
|
|
|
static void
|
|
|
|
FreeGmt(void *p) {
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
2022-04-27 12:39:39 +00:00
|
|
|
static void
|
2022-04-29 07:36:14 +00:00
|
|
|
localtime_gmtcheck(void)
|
2022-04-27 12:39:39 +00:00
|
|
|
{
|
|
|
|
static bool gmt_is_set;
|
2022-08-13 21:18:02 +00:00
|
|
|
localtime_lock();
|
2022-04-27 12:39:39 +00:00
|
|
|
if (! gmt_is_set) {
|
|
|
|
gmtptr = malloc(sizeof *gmtptr);
|
2022-05-16 20:20:08 +00:00
|
|
|
__cxa_atexit(FreeGmt, gmtptr, 0);
|
2022-04-27 12:39:39 +00:00
|
|
|
if (gmtptr)
|
|
|
|
gmtload(gmtptr);
|
|
|
|
gmt_is_set = true;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-06-12 18:47:20 +00:00
|
|
|
localtime_unlock();
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** The easy way to behave "as if no library function calls" localtime
|
2022-04-27 12:39:39 +00:00
|
|
|
** is to not call it, so we drop its guts into "localsub", which can be
|
|
|
|
** freely called. (And no, the PANS doesn't require the above behavior,
|
2020-06-15 14:18:57 +00:00
|
|
|
** but it *is* desirable.)
|
|
|
|
**
|
2022-04-27 12:39:39 +00:00
|
|
|
** If successful and SETNAME is nonzero,
|
|
|
|
** set the applicable parts of tzname, timezone and altzone;
|
|
|
|
** however, it's OK to omit this step if the timezone is POSIX-compatible,
|
|
|
|
** since in that case tzset should have already done this step correctly.
|
2023-10-11 21:54:42 +00:00
|
|
|
** SETNAME's type is int32_t for compatibility with gmtsub,
|
2022-04-27 12:39:39 +00:00
|
|
|
** but it is actually a boolean and its value should be 0 or 1.
|
2020-06-15 14:18:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*ARGSUSED*/
|
|
|
|
static struct tm *
|
2023-10-11 21:54:42 +00:00
|
|
|
localsub(struct state const *sp, time_t const *timep, int32_t setname,
|
2022-04-27 12:39:39 +00:00
|
|
|
struct tm *const tmp)
|
|
|
|
{
|
2020-06-15 14:18:57 +00:00
|
|
|
register const struct ttinfo * ttisp;
|
|
|
|
register int i;
|
|
|
|
register struct tm * result;
|
2022-05-12 13:11:22 +00:00
|
|
|
const time_t t = *timep;
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-04-27 12:39:39 +00:00
|
|
|
if (sp == NULL) {
|
|
|
|
/* Don't bother to set tzname etc.; tzset has already done it. */
|
|
|
|
return gmtsub(gmtptr, timep, 0, tmp);
|
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
if ((sp->goback && t < sp->ats[0]) ||
|
|
|
|
(sp->goahead && t > sp->ats[sp->timecnt - 1])) {
|
2022-05-12 13:11:22 +00:00
|
|
|
time_t newt;
|
|
|
|
register time_t seconds;
|
|
|
|
register time_t years;
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
if (t < sp->ats[0])
|
|
|
|
seconds = sp->ats[0] - t;
|
|
|
|
else seconds = t - sp->ats[sp->timecnt - 1];
|
|
|
|
--seconds;
|
2022-04-27 12:39:39 +00:00
|
|
|
|
|
|
|
/* Beware integer overflow, as SECONDS might
|
2022-05-12 13:11:22 +00:00
|
|
|
be close to the maximum time_t. */
|
2022-04-27 12:39:39 +00:00
|
|
|
years = seconds / SECSPERREPEAT * YEARSPERREPEAT;
|
|
|
|
seconds = years * AVGSECSPERYEAR;
|
|
|
|
years += YEARSPERREPEAT;
|
2020-06-15 14:18:57 +00:00
|
|
|
if (t < sp->ats[0])
|
2022-04-27 12:39:39 +00:00
|
|
|
newt = t + seconds + SECSPERREPEAT;
|
|
|
|
else
|
|
|
|
newt = t - seconds - SECSPERREPEAT;
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
if (newt < sp->ats[0] ||
|
|
|
|
newt > sp->ats[sp->timecnt - 1])
|
|
|
|
return NULL; /* "cannot happen" */
|
2022-04-27 12:39:39 +00:00
|
|
|
result = localsub(sp, &newt, setname, tmp);
|
|
|
|
if (result) {
|
2023-10-11 21:54:42 +00:00
|
|
|
register int64_t newy;
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-04-27 12:39:39 +00:00
|
|
|
newy = result->tm_year;
|
2020-06-15 14:18:57 +00:00
|
|
|
if (t < sp->ats[0])
|
2022-04-27 12:39:39 +00:00
|
|
|
newy -= years;
|
|
|
|
else newy += years;
|
|
|
|
if (! (INT_MIN <= newy && newy <= INT_MAX))
|
2020-06-15 14:18:57 +00:00
|
|
|
return NULL;
|
2022-04-27 12:39:39 +00:00
|
|
|
result->tm_year = newy;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (sp->timecnt == 0 || t < sp->ats[0]) {
|
2022-04-27 12:39:39 +00:00
|
|
|
i = sp->defaulttype;
|
2020-06-15 14:18:57 +00:00
|
|
|
} else {
|
|
|
|
register int lo = 1;
|
|
|
|
register int hi = sp->timecnt;
|
|
|
|
|
|
|
|
while (lo < hi) {
|
|
|
|
register int mid = (lo + hi) >> 1;
|
|
|
|
|
|
|
|
if (t < sp->ats[mid])
|
|
|
|
hi = mid;
|
|
|
|
else lo = mid + 1;
|
|
|
|
}
|
2022-04-27 12:39:39 +00:00
|
|
|
i = sp->types[lo - 1];
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
ttisp = &sp->ttis[i];
|
|
|
|
/*
|
|
|
|
** To get (wrong) behavior that's compatible with System V Release 2.0
|
|
|
|
** you'd replace the statement below with
|
2022-04-27 12:39:39 +00:00
|
|
|
** t += ttisp->tt_utoff;
|
2020-06-15 14:18:57 +00:00
|
|
|
** timesub(&t, 0L, sp, tmp);
|
|
|
|
*/
|
2022-04-29 07:36:14 +00:00
|
|
|
result = localtime_timesub(&t, ttisp->tt_utoff, sp, tmp);
|
2022-04-27 12:39:39 +00:00
|
|
|
if (result) {
|
|
|
|
result->tm_isdst = ttisp->tt_isdst;
|
|
|
|
result->tm_zone = (char *) &sp->chars[ttisp->tt_desigidx];
|
|
|
|
if (setname)
|
|
|
|
update_tzname_etc(sp, ttisp);
|
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-04-27 12:39:39 +00:00
|
|
|
static struct tm *
|
2022-05-12 13:11:22 +00:00
|
|
|
localtime_tzset(time_t const *timep, struct tm *tmp, bool setname)
|
2022-04-27 12:39:39 +00:00
|
|
|
{
|
2022-08-13 21:18:02 +00:00
|
|
|
localtime_lock();
|
2022-04-27 12:39:39 +00:00
|
|
|
if (setname || !lcl_is_set)
|
2022-04-29 07:36:14 +00:00
|
|
|
localtime_tzset_unlocked();
|
2022-04-27 12:39:39 +00:00
|
|
|
tmp = localsub(lclptr, timep, setname, tmp);
|
2022-06-12 18:47:20 +00:00
|
|
|
localtime_unlock();
|
2022-04-27 12:39:39 +00:00
|
|
|
return tmp;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-27 12:39:39 +00:00
|
|
|
struct tm *
|
2022-05-12 13:11:22 +00:00
|
|
|
localtime(const time_t *timep)
|
2022-04-27 12:39:39 +00:00
|
|
|
{
|
|
|
|
return localtime_tzset(timep, &tm, true);
|
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
struct tm *
|
2022-05-12 13:11:22 +00:00
|
|
|
localtime_r(const time_t *timep, struct tm *tmp)
|
2022-04-27 12:39:39 +00:00
|
|
|
{
|
|
|
|
return localtime_tzset(timep, tmp, false);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** gmtsub is to gmtime as localsub is to localtime.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct tm *
|
2023-10-11 21:54:42 +00:00
|
|
|
gmtsub(struct state const *sp, time_t const *timep, int32_t offset,
|
2022-04-27 12:39:39 +00:00
|
|
|
struct tm *tmp)
|
|
|
|
{
|
2020-06-15 14:18:57 +00:00
|
|
|
register struct tm * result;
|
2022-04-27 12:39:39 +00:00
|
|
|
|
2022-04-29 07:36:14 +00:00
|
|
|
result = localtime_timesub(timep, offset, gmtptr, tmp);
|
2020-06-15 14:18:57 +00:00
|
|
|
/*
|
|
|
|
** Could get fancy here and deliver something such as
|
2022-04-27 12:39:39 +00:00
|
|
|
** "+xx" or "-xx" if offset is non-zero,
|
2020-06-15 14:18:57 +00:00
|
|
|
** but this is no time for a treasure hunt.
|
|
|
|
*/
|
2022-04-27 12:39:39 +00:00
|
|
|
tmp->tm_zone = ((char *)
|
|
|
|
(offset ? wildabbr : gmtptr ? gmtptr->chars : gmt));
|
2020-06-15 14:18:57 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Re-entrant version of gmtime.
|
|
|
|
*/
|
|
|
|
|
2023-10-03 02:25:19 +00:00
|
|
|
/**
|
|
|
|
* Converts UNIX timestamp to broken-down representation.
|
|
|
|
*/
|
2020-06-15 14:18:57 +00:00
|
|
|
struct tm *
|
2022-05-12 13:11:22 +00:00
|
|
|
gmtime_r(const time_t *timep, struct tm *tmp)
|
2022-04-27 12:39:39 +00:00
|
|
|
{
|
2022-04-29 07:36:14 +00:00
|
|
|
localtime_gmtcheck();
|
2022-04-27 12:39:39 +00:00
|
|
|
return gmtsub(gmtptr, timep, 0, tmp);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 02:25:19 +00:00
|
|
|
/**
|
|
|
|
* Converts UNIX timestamp to broken-down representation.
|
|
|
|
* @threadunsafe (see gmtime_r)
|
|
|
|
*/
|
2020-06-15 14:18:57 +00:00
|
|
|
struct tm *
|
2022-05-12 13:11:22 +00:00
|
|
|
gmtime(const time_t *timep)
|
2022-04-27 12:39:39 +00:00
|
|
|
{
|
|
|
|
return gmtime_r(timep, &tm);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Return the number of leap years through the end of the given year
|
|
|
|
** where, to make the math easy, the answer for year zero is defined as zero.
|
|
|
|
*/
|
|
|
|
|
2022-05-12 13:11:22 +00:00
|
|
|
static time_t
|
|
|
|
leaps_thru_end_of_nonneg(time_t y)
|
2022-04-27 12:39:39 +00:00
|
|
|
{
|
|
|
|
return y / 4 - y / 100 + y / 400;
|
|
|
|
}
|
|
|
|
|
2022-05-12 13:11:22 +00:00
|
|
|
static time_t
|
|
|
|
leaps_thru_end_of(time_t y)
|
2022-04-27 12:39:39 +00:00
|
|
|
{
|
|
|
|
return (y < 0
|
|
|
|
? -1 - leaps_thru_end_of_nonneg(-1 - y)
|
|
|
|
: leaps_thru_end_of_nonneg(y));
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct tm *
|
2023-10-11 21:54:42 +00:00
|
|
|
localtime_timesub(const time_t *timep, int32_t offset,
|
2022-04-29 07:36:14 +00:00
|
|
|
const struct state *sp, struct tm *tmp)
|
2022-04-27 12:39:39 +00:00
|
|
|
{
|
|
|
|
register const struct lsinfo * lp;
|
2022-05-12 13:11:22 +00:00
|
|
|
register time_t tdays;
|
|
|
|
register const int * ip;
|
2023-10-11 21:54:42 +00:00
|
|
|
register int32_t corr;
|
2022-05-12 13:11:22 +00:00
|
|
|
register int i;
|
2023-10-11 21:54:42 +00:00
|
|
|
int32_t idays, rem, dayoff, dayrem;
|
2022-05-12 13:11:22 +00:00
|
|
|
time_t y;
|
2022-04-27 12:39:39 +00:00
|
|
|
|
|
|
|
/* If less than SECSPERMIN, the number of seconds since the
|
|
|
|
most recent positive leap second; otherwise, do not add 1
|
|
|
|
to localtime tm_sec because of leap seconds. */
|
2022-05-12 13:11:22 +00:00
|
|
|
time_t secs_since_posleap = SECSPERMIN;
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
corr = 0;
|
|
|
|
i = (sp == NULL) ? 0 : sp->leapcnt;
|
|
|
|
while (--i >= 0) {
|
|
|
|
lp = &sp->lsis[i];
|
|
|
|
if (*timep >= lp->ls_trans) {
|
|
|
|
corr = lp->ls_corr;
|
2022-04-27 12:39:39 +00:00
|
|
|
if ((i == 0 ? 0 : lp[-1].ls_corr) < corr)
|
|
|
|
secs_since_posleap = *timep - lp->ls_trans;
|
2020-06-15 14:18:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-04-27 12:39:39 +00:00
|
|
|
|
|
|
|
/* Calculate the year, avoiding integer overflow even if
|
2022-05-12 13:11:22 +00:00
|
|
|
time_t is unsigned. */
|
2020-06-15 14:18:57 +00:00
|
|
|
tdays = *timep / SECSPERDAY;
|
2022-04-27 12:39:39 +00:00
|
|
|
rem = *timep % SECSPERDAY;
|
|
|
|
rem += offset % SECSPERDAY - corr % SECSPERDAY + 3 * SECSPERDAY;
|
|
|
|
dayoff = offset / SECSPERDAY - corr / SECSPERDAY + rem / SECSPERDAY - 3;
|
|
|
|
rem %= SECSPERDAY;
|
|
|
|
/* y = (EPOCH_YEAR
|
|
|
|
+ floor((tdays + dayoff) / DAYSPERREPEAT) * YEARSPERREPEAT),
|
|
|
|
sans overflow. But calculate against 1570 (EPOCH_YEAR -
|
|
|
|
YEARSPERREPEAT) instead of against 1970 so that things work
|
2022-05-12 13:11:22 +00:00
|
|
|
for localtime values before 1970 when time_t is unsigned. */
|
2022-04-27 12:39:39 +00:00
|
|
|
dayrem = tdays % DAYSPERREPEAT;
|
|
|
|
dayrem += dayoff % DAYSPERREPEAT;
|
|
|
|
y = (EPOCH_YEAR - YEARSPERREPEAT
|
|
|
|
+ ((1 + dayoff / DAYSPERREPEAT + dayrem / DAYSPERREPEAT
|
|
|
|
- ((dayrem % DAYSPERREPEAT) < 0)
|
|
|
|
+ tdays / DAYSPERREPEAT)
|
|
|
|
* YEARSPERREPEAT));
|
|
|
|
/* idays = (tdays + dayoff) mod DAYSPERREPEAT, sans overflow. */
|
|
|
|
idays = tdays % DAYSPERREPEAT;
|
|
|
|
idays += dayoff % DAYSPERREPEAT + 2 * DAYSPERREPEAT;
|
|
|
|
idays %= DAYSPERREPEAT;
|
|
|
|
/* Increase Y and decrease IDAYS until IDAYS is in range for Y. */
|
2022-05-12 13:11:22 +00:00
|
|
|
while (year_lengths[isleap(y)] <= idays) {
|
|
|
|
int tdelta = idays / DAYSPERLYEAR;
|
2023-10-11 21:54:42 +00:00
|
|
|
int32_t ydelta = tdelta + !tdelta;
|
2022-05-12 13:11:22 +00:00
|
|
|
time_t newy = y + ydelta;
|
|
|
|
register int leapdays;
|
2020-06-15 14:18:57 +00:00
|
|
|
leapdays = leaps_thru_end_of(newy - 1) -
|
|
|
|
leaps_thru_end_of(y - 1);
|
2022-04-27 12:39:39 +00:00
|
|
|
idays -= ydelta * DAYSPERNYEAR;
|
|
|
|
idays -= leapdays;
|
2020-06-15 14:18:57 +00:00
|
|
|
y = newy;
|
|
|
|
}
|
|
|
|
|
2022-05-12 13:11:22 +00:00
|
|
|
if (!TYPE_SIGNED(time_t) && y < TM_YEAR_BASE) {
|
|
|
|
int signed_y = y;
|
|
|
|
tmp->tm_year = signed_y - TM_YEAR_BASE;
|
|
|
|
} else if ((!TYPE_SIGNED(time_t) || INT_MIN + TM_YEAR_BASE <= y)
|
|
|
|
&& y - TM_YEAR_BASE <= INT_MAX)
|
|
|
|
tmp->tm_year = y - TM_YEAR_BASE;
|
|
|
|
else {
|
|
|
|
errno = EOVERFLOW;
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
tmp->tm_yday = idays;
|
|
|
|
/*
|
|
|
|
** The "extra" mods below avoid overflow problems.
|
|
|
|
*/
|
2022-04-27 12:39:39 +00:00
|
|
|
tmp->tm_wday = (TM_WDAY_BASE
|
|
|
|
+ ((tmp->tm_year % DAYSPERWEEK)
|
|
|
|
* (DAYSPERNYEAR % DAYSPERWEEK))
|
|
|
|
+ leaps_thru_end_of(y - 1)
|
|
|
|
- leaps_thru_end_of(TM_YEAR_BASE - 1)
|
|
|
|
+ idays);
|
2020-06-15 14:18:57 +00:00
|
|
|
tmp->tm_wday %= DAYSPERWEEK;
|
|
|
|
if (tmp->tm_wday < 0)
|
|
|
|
tmp->tm_wday += DAYSPERWEEK;
|
2022-04-27 12:39:39 +00:00
|
|
|
tmp->tm_hour = rem / SECSPERHOUR;
|
2020-06-15 14:18:57 +00:00
|
|
|
rem %= SECSPERHOUR;
|
2022-04-27 12:39:39 +00:00
|
|
|
tmp->tm_min = rem / SECSPERMIN;
|
|
|
|
tmp->tm_sec = rem % SECSPERMIN;
|
|
|
|
|
|
|
|
/* Use "... ??:??:60" at the end of the localtime minute containing
|
|
|
|
the second just before the positive leap second. */
|
|
|
|
tmp->tm_sec += secs_since_posleap <= tmp->tm_sec;
|
|
|
|
|
2022-05-12 13:11:22 +00:00
|
|
|
ip = mon_lengths[isleap(y)];
|
2022-04-27 12:39:39 +00:00
|
|
|
for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
|
|
|
|
idays -= ip[tmp->tm_mon];
|
|
|
|
tmp->tm_mday = idays + 1;
|
2020-06-15 14:18:57 +00:00
|
|
|
tmp->tm_isdst = 0;
|
2022-04-27 12:39:39 +00:00
|
|
|
tmp->tm_gmtoff = offset;
|
2020-06-15 14:18:57 +00:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** 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.
|
2022-05-12 13:11:22 +00:00
|
|
|
** It does a binary search of the time_t space. Since time_t's are
|
2020-06-15 14:18:57 +00:00
|
|
|
** just 32 bits, its a max of 32 iterations (even at 64 bits it
|
|
|
|
** would still be very reasonable).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef WRONG
|
|
|
|
#define WRONG (-1)
|
|
|
|
#endif /* !defined WRONG */
|
|
|
|
|
2022-05-12 13:11:22 +00:00
|
|
|
/*
|
|
|
|
** Normalize logic courtesy Paul Eggert.
|
|
|
|
*/
|
|
|
|
|
2022-06-13 02:33:42 +00:00
|
|
|
forceinline bool
|
2022-05-12 13:11:22 +00:00
|
|
|
increment_overflow(int *ip, int j)
|
|
|
|
{
|
|
|
|
#if defined(__GNUC__) && __GNUC__ >= 6
|
|
|
|
int i = *ip;
|
|
|
|
if (__builtin_add_overflow(i, j, &i)) return true;
|
|
|
|
*ip = i;
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
register int const i = *ip;
|
|
|
|
/*
|
|
|
|
** If i >= 0 there can only be overflow if i + j > INT_MAX
|
|
|
|
** or if j > INT_MAX - i; given i >= 0, INT_MAX - i cannot overflow.
|
|
|
|
** If i < 0 there can only be overflow if i + j < INT_MIN
|
|
|
|
** or if j < INT_MIN - i; given i < 0, INT_MIN - i cannot overflow.
|
|
|
|
*/
|
|
|
|
if ((i >= 0) ? (j > INT_MAX - i) : (j < INT_MIN - i))
|
|
|
|
return true;
|
|
|
|
*ip += j;
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-06-13 02:33:42 +00:00
|
|
|
forceinline bool
|
2023-10-11 21:54:42 +00:00
|
|
|
increment_overflow32(int32_t *const lp, int const m)
|
2022-05-12 13:11:22 +00:00
|
|
|
{
|
|
|
|
#if defined(__GNUC__) && __GNUC__ >= 6
|
2023-10-11 21:54:42 +00:00
|
|
|
int32_t i = *lp;
|
2022-05-12 13:11:22 +00:00
|
|
|
if (__builtin_add_overflow(i, m, &i)) return true;
|
|
|
|
*lp = i;
|
|
|
|
return false;
|
|
|
|
#else
|
2023-10-11 21:54:42 +00:00
|
|
|
register int32_t const l = *lp;
|
|
|
|
if ((l >= 0) ? (m > INT32_MAX - l) : (m < INT32_MIN - l))
|
2022-05-12 13:11:22 +00:00
|
|
|
return true;
|
|
|
|
*lp += m;
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-06-13 02:33:42 +00:00
|
|
|
forceinline bool
|
2023-10-11 21:54:42 +00:00
|
|
|
increment_overflow_time(time_t *tp, int32_t j)
|
2022-05-12 13:11:22 +00:00
|
|
|
{
|
|
|
|
#if defined(__GNUC__) && __GNUC__ >= 6
|
|
|
|
time_t i = *tp;
|
|
|
|
if (__builtin_add_overflow(i, j, &i)) return true;
|
|
|
|
*tp = i;
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
/*
|
|
|
|
** This is like
|
|
|
|
** 'if (! (TIME_T_MIN <= *tp + j && *tp + j <= TIME_T_MAX)) ...',
|
|
|
|
** except that it does the right thing even if *tp + j would overflow.
|
|
|
|
*/
|
|
|
|
if (! (j < 0
|
|
|
|
? (TYPE_SIGNED(time_t) ? TIME_T_MIN - j <= *tp : -1 - j < *tp)
|
|
|
|
: *tp <= TIME_T_MAX - j))
|
|
|
|
return true;
|
|
|
|
*tp += j;
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-04-27 12:39:39 +00:00
|
|
|
static bool
|
2022-05-12 13:11:22 +00:00
|
|
|
normalize_overflow(int *const tensptr, int *const unitsptr, const int base)
|
2022-04-27 12:39:39 +00:00
|
|
|
{
|
2022-05-12 13:11:22 +00:00
|
|
|
register int tensdelta;
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
tensdelta = (*unitsptr >= 0) ?
|
|
|
|
(*unitsptr / base) :
|
|
|
|
(-1 - (-1 - *unitsptr) / base);
|
|
|
|
*unitsptr -= tensdelta * base;
|
|
|
|
return increment_overflow(tensptr, tensdelta);
|
|
|
|
}
|
|
|
|
|
2022-05-12 13:11:22 +00:00
|
|
|
static bool
|
2023-10-11 21:54:42 +00:00
|
|
|
normalize_overflow32(int32_t *tensptr, int *unitsptr, int base)
|
2022-05-12 13:11:22 +00:00
|
|
|
{
|
|
|
|
register int tensdelta;
|
|
|
|
|
|
|
|
tensdelta = (*unitsptr >= 0) ?
|
|
|
|
(*unitsptr / base) :
|
|
|
|
(-1 - (-1 - *unitsptr) / base);
|
|
|
|
*unitsptr -= tensdelta * base;
|
|
|
|
return increment_overflow32(tensptr, tensdelta);
|
|
|
|
}
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
static int
|
2022-05-12 13:11:22 +00:00
|
|
|
tmcomp(register const struct tm *const atmp,
|
|
|
|
register const struct tm *const btmp)
|
2022-04-27 12:39:39 +00:00
|
|
|
{
|
2022-05-12 13:11:22 +00:00
|
|
|
register int result;
|
|
|
|
|
2022-04-27 12:39:39 +00:00
|
|
|
if (atmp->tm_year != btmp->tm_year)
|
|
|
|
return atmp->tm_year < btmp->tm_year ? -1 : 1;
|
2022-05-12 13:11:22 +00:00
|
|
|
if ((result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
|
|
|
|
(result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
|
|
|
|
(result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
|
|
|
|
(result = (atmp->tm_min - btmp->tm_min)) == 0)
|
|
|
|
result = atmp->tm_sec - btmp->tm_sec;
|
2020-06-15 14:18:57 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-05-12 13:11:22 +00:00
|
|
|
static time_t
|
2022-04-29 07:36:14 +00:00
|
|
|
localtime_time2sub(
|
|
|
|
struct tm *const tmp,
|
2022-05-12 13:11:22 +00:00
|
|
|
struct tm *(*funcp)(struct state const *, time_t const *,
|
2023-10-11 21:54:42 +00:00
|
|
|
int32_t, struct tm *),
|
2022-04-29 07:36:14 +00:00
|
|
|
struct state const *sp,
|
2023-10-11 21:54:42 +00:00
|
|
|
const int32_t offset,
|
2022-04-29 07:36:14 +00:00
|
|
|
bool *okayp,
|
|
|
|
bool do_norm_secs)
|
2022-04-27 12:39:39 +00:00
|
|
|
{
|
2022-05-12 13:11:22 +00:00
|
|
|
register int dir;
|
|
|
|
register int i, j;
|
|
|
|
register int saved_seconds;
|
2023-10-11 21:54:42 +00:00
|
|
|
register int32_t li;
|
2022-05-12 13:11:22 +00:00
|
|
|
register time_t lo;
|
|
|
|
register time_t hi;
|
2023-10-11 21:54:42 +00:00
|
|
|
int32_t y;
|
2022-05-12 13:11:22 +00:00
|
|
|
time_t newt;
|
|
|
|
time_t t;
|
|
|
|
struct tm yourtm, mytm;
|
2022-04-27 12:39:39 +00:00
|
|
|
|
|
|
|
*okayp = false;
|
2020-06-15 14:18:57 +00:00
|
|
|
yourtm = *tmp;
|
|
|
|
if (do_norm_secs) {
|
|
|
|
if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
|
|
|
|
SECSPERMIN))
|
|
|
|
return WRONG;
|
|
|
|
}
|
|
|
|
if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
|
|
|
|
return WRONG;
|
|
|
|
if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
|
|
|
|
return WRONG;
|
|
|
|
y = yourtm.tm_year;
|
2022-05-12 13:11:22 +00:00
|
|
|
if (normalize_overflow32(&y, &yourtm.tm_mon, MONSPERYEAR))
|
2020-06-15 14:18:57 +00:00
|
|
|
return WRONG;
|
|
|
|
/*
|
|
|
|
** Turn y into an actual year number for now.
|
|
|
|
** It is converted back to an offset from TM_YEAR_BASE later.
|
|
|
|
*/
|
2022-05-12 13:11:22 +00:00
|
|
|
if (increment_overflow32(&y, TM_YEAR_BASE))
|
2020-06-15 14:18:57 +00:00
|
|
|
return WRONG;
|
|
|
|
while (yourtm.tm_mday <= 0) {
|
2022-05-12 13:11:22 +00:00
|
|
|
if (increment_overflow32(&y, -1))
|
2020-06-15 14:18:57 +00:00
|
|
|
return WRONG;
|
|
|
|
li = y + (1 < yourtm.tm_mon);
|
2022-05-12 13:11:22 +00:00
|
|
|
yourtm.tm_mday += year_lengths[isleap(li)];
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
while (yourtm.tm_mday > DAYSPERLYEAR) {
|
|
|
|
li = y + (1 < yourtm.tm_mon);
|
2022-05-12 13:11:22 +00:00
|
|
|
yourtm.tm_mday -= year_lengths[isleap(li)];
|
|
|
|
if (increment_overflow32(&y, 1))
|
2020-06-15 14:18:57 +00:00
|
|
|
return WRONG;
|
|
|
|
}
|
|
|
|
for ( ; ; ) {
|
2022-05-12 13:11:22 +00:00
|
|
|
i = mon_lengths[isleap(y)][yourtm.tm_mon];
|
2020-06-15 14:18:57 +00:00
|
|
|
if (yourtm.tm_mday <= i)
|
|
|
|
break;
|
|
|
|
yourtm.tm_mday -= i;
|
|
|
|
if (++yourtm.tm_mon >= MONSPERYEAR) {
|
|
|
|
yourtm.tm_mon = 0;
|
2022-05-12 13:11:22 +00:00
|
|
|
if (increment_overflow32(&y, 1))
|
2020-06-15 14:18:57 +00:00
|
|
|
return WRONG;
|
|
|
|
}
|
|
|
|
}
|
2022-05-12 13:11:22 +00:00
|
|
|
if (increment_overflow32(&y, -TM_YEAR_BASE))
|
2020-06-15 14:18:57 +00:00
|
|
|
return WRONG;
|
2022-04-27 12:39:39 +00:00
|
|
|
if (! (INT_MIN <= y && y <= INT_MAX))
|
2020-06-15 14:18:57 +00:00
|
|
|
return WRONG;
|
2022-04-27 12:39:39 +00:00
|
|
|
yourtm.tm_year = y;
|
2020-06-15 14:18:57 +00:00
|
|
|
if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
|
|
|
|
saved_seconds = 0;
|
|
|
|
else if (y + TM_YEAR_BASE < EPOCH_YEAR) {
|
|
|
|
/*
|
|
|
|
** We can't set tm_sec to 0, because that might push the
|
|
|
|
** time below the minimum representable time.
|
|
|
|
** Set tm_sec to 59 instead.
|
|
|
|
** This assumes that the minimum representable time is
|
|
|
|
** not in the same minute that a leap second was deleted from,
|
|
|
|
** which is a safer assumption than using 58 would be.
|
|
|
|
*/
|
|
|
|
if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
|
|
|
|
return WRONG;
|
|
|
|
saved_seconds = yourtm.tm_sec;
|
|
|
|
yourtm.tm_sec = SECSPERMIN - 1;
|
|
|
|
} else {
|
|
|
|
saved_seconds = yourtm.tm_sec;
|
|
|
|
yourtm.tm_sec = 0;
|
|
|
|
}
|
|
|
|
/*
|
2022-05-12 13:11:22 +00:00
|
|
|
** Do a binary search (this works whatever time_t's type is).
|
2020-06-15 14:18:57 +00:00
|
|
|
*/
|
2022-05-12 13:11:22 +00:00
|
|
|
lo = TIME_T_MIN;
|
|
|
|
hi = TIME_T_MAX;
|
2020-06-15 14:18:57 +00:00
|
|
|
for ( ; ; ) {
|
|
|
|
t = lo / 2 + hi / 2;
|
|
|
|
if (t < lo)
|
|
|
|
t = lo;
|
|
|
|
else if (t > hi)
|
|
|
|
t = hi;
|
2022-04-27 12:39:39 +00:00
|
|
|
if (! funcp(sp, &t, offset, &mytm)) {
|
2020-06-15 14:18:57 +00:00
|
|
|
/*
|
|
|
|
** Assume that t is too extreme to be represented in
|
|
|
|
** a struct tm; arrange things so that it is less
|
|
|
|
** extreme on the next pass.
|
|
|
|
*/
|
|
|
|
dir = (t > 0) ? 1 : -1;
|
|
|
|
} else dir = tmcomp(&mytm, &yourtm);
|
|
|
|
if (dir != 0) {
|
|
|
|
if (t == lo) {
|
2022-05-12 13:11:22 +00:00
|
|
|
if (t == TIME_T_MAX)
|
2020-06-15 14:18:57 +00:00
|
|
|
return WRONG;
|
2022-04-27 12:39:39 +00:00
|
|
|
++t;
|
2020-06-15 14:18:57 +00:00
|
|
|
++lo;
|
|
|
|
} else if (t == hi) {
|
2022-05-12 13:11:22 +00:00
|
|
|
if (t == TIME_T_MIN)
|
2020-06-15 14:18:57 +00:00
|
|
|
return WRONG;
|
2022-04-27 12:39:39 +00:00
|
|
|
--t;
|
2020-06-15 14:18:57 +00:00
|
|
|
--hi;
|
|
|
|
}
|
|
|
|
if (lo > hi)
|
|
|
|
return WRONG;
|
|
|
|
if (dir > 0)
|
|
|
|
hi = t;
|
|
|
|
else lo = t;
|
|
|
|
continue;
|
|
|
|
}
|
2022-04-27 12:39:39 +00:00
|
|
|
#if defined TM_GMTOFF && ! UNINIT_TRAP
|
|
|
|
if (mytm.TM_GMTOFF != yourtm.TM_GMTOFF
|
|
|
|
&& (yourtm.TM_GMTOFF < 0
|
|
|
|
? (-SECSPERDAY <= yourtm.TM_GMTOFF
|
|
|
|
&& (mytm.TM_GMTOFF <=
|
2023-10-11 21:54:42 +00:00
|
|
|
(SMALLEST(INT32_MAX, LONG_MAX)
|
2022-04-27 12:39:39 +00:00
|
|
|
+ yourtm.TM_GMTOFF)))
|
|
|
|
: (yourtm.TM_GMTOFF <= SECSPERDAY
|
2023-10-11 21:54:42 +00:00
|
|
|
&& ((BIGGEST(INT32_MIN, LONG_MIN)
|
2022-04-27 12:39:39 +00:00
|
|
|
+ yourtm.TM_GMTOFF)
|
|
|
|
<= mytm.TM_GMTOFF)))) {
|
|
|
|
/* MYTM matches YOURTM except with the wrong UT offset.
|
|
|
|
YOURTM.TM_GMTOFF is plausible, so try it instead.
|
|
|
|
It's OK if YOURTM.TM_GMTOFF contains uninitialized data,
|
|
|
|
since the guess gets checked. */
|
2022-05-12 13:11:22 +00:00
|
|
|
time_t altt = t;
|
2023-10-11 21:54:42 +00:00
|
|
|
int32_t diff = mytm.TM_GMTOFF - yourtm.TM_GMTOFF;
|
2022-05-12 13:11:22 +00:00
|
|
|
if (!increment_overflow_time(&altt, diff)) {
|
2022-04-27 12:39:39 +00:00
|
|
|
struct tm alttm;
|
|
|
|
if (funcp(sp, &altt, offset, &alttm)
|
|
|
|
&& alttm.tm_isdst == mytm.tm_isdst
|
|
|
|
&& alttm.TM_GMTOFF == yourtm.TM_GMTOFF
|
|
|
|
&& tmcomp(&alttm, &yourtm) == 0) {
|
|
|
|
t = altt;
|
|
|
|
mytm = alttm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2020-06-15 14:18:57 +00:00
|
|
|
if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
|
|
|
|
break;
|
|
|
|
/*
|
|
|
|
** Right time, wrong type.
|
|
|
|
** Hunt for right time, right type.
|
|
|
|
** It's okay to guess wrong since the guess
|
|
|
|
** gets checked.
|
|
|
|
*/
|
|
|
|
if (sp == NULL)
|
|
|
|
return WRONG;
|
|
|
|
for (i = sp->typecnt - 1; i >= 0; --i) {
|
|
|
|
if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
|
|
|
|
continue;
|
|
|
|
for (j = sp->typecnt - 1; j >= 0; --j) {
|
|
|
|
if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
|
|
|
|
continue;
|
2022-04-27 12:39:39 +00:00
|
|
|
if (ttunspecified(sp, j))
|
|
|
|
continue;
|
|
|
|
newt = (t + sp->ttis[j].tt_utoff
|
|
|
|
- sp->ttis[i].tt_utoff);
|
|
|
|
if (! funcp(sp, &newt, offset, &mytm))
|
2020-06-15 14:18:57 +00:00
|
|
|
continue;
|
|
|
|
if (tmcomp(&mytm, &yourtm) != 0)
|
|
|
|
continue;
|
|
|
|
if (mytm.tm_isdst != yourtm.tm_isdst)
|
|
|
|
continue;
|
|
|
|
/*
|
|
|
|
** We have a match.
|
|
|
|
*/
|
|
|
|
t = newt;
|
|
|
|
goto label;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return WRONG;
|
|
|
|
}
|
|
|
|
label:
|
|
|
|
newt = t + saved_seconds;
|
|
|
|
if ((newt < t) != (saved_seconds < 0))
|
|
|
|
return WRONG;
|
|
|
|
t = newt;
|
2022-04-27 12:39:39 +00:00
|
|
|
if (funcp(sp, &t, offset, tmp))
|
|
|
|
*okayp = true;
|
2020-06-15 14:18:57 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2022-05-12 13:11:22 +00:00
|
|
|
static time_t
|
2022-04-29 07:36:14 +00:00
|
|
|
localtime_time2(
|
|
|
|
struct tm * const tmp,
|
2022-05-12 13:11:22 +00:00
|
|
|
struct tm *(*funcp)(struct state const *, time_t const *,
|
2023-10-11 21:54:42 +00:00
|
|
|
int32_t, struct tm *),
|
2022-04-29 07:36:14 +00:00
|
|
|
struct state const *sp,
|
2023-10-11 21:54:42 +00:00
|
|
|
const int32_t offset,
|
2022-04-29 07:36:14 +00:00
|
|
|
bool *okayp)
|
2022-04-27 12:39:39 +00:00
|
|
|
{
|
2022-05-12 13:11:22 +00:00
|
|
|
time_t t;
|
2022-04-27 12:39:39 +00:00
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
/*
|
|
|
|
** First try without normalization of seconds
|
|
|
|
** (in case tm_sec contains a value associated with a leap second).
|
|
|
|
** If that fails, try with normalization of seconds.
|
|
|
|
*/
|
2022-04-29 07:36:14 +00:00
|
|
|
t = localtime_time2sub(tmp, funcp, sp, offset, okayp, false);
|
|
|
|
return *okayp ? t : localtime_time2sub(tmp,funcp,sp,offset,okayp,true);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 13:11:22 +00:00
|
|
|
static time_t
|
2022-04-29 07:36:14 +00:00
|
|
|
localtime_time1(
|
|
|
|
struct tm *const tmp,
|
2022-05-12 13:11:22 +00:00
|
|
|
struct tm *(*funcp)(struct state const *, time_t const *,
|
2023-10-11 21:54:42 +00:00
|
|
|
int32_t, struct tm *),
|
2022-04-29 07:36:14 +00:00
|
|
|
struct state const *sp,
|
2023-10-11 21:54:42 +00:00
|
|
|
const int32_t offset)
|
2022-04-27 12:39:39 +00:00
|
|
|
{
|
2022-05-12 13:11:22 +00:00
|
|
|
register time_t t;
|
2020-06-15 14:18:57 +00:00
|
|
|
register int samei, otheri;
|
|
|
|
register int sameind, otherind;
|
|
|
|
register int i;
|
|
|
|
register int nseen;
|
2022-04-27 12:39:39 +00:00
|
|
|
char seen[TZ_MAX_TYPES];
|
|
|
|
unsigned char types[TZ_MAX_TYPES];
|
|
|
|
bool okay;
|
|
|
|
|
|
|
|
if (tmp == NULL) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return WRONG;
|
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
if (tmp->tm_isdst > 1)
|
|
|
|
tmp->tm_isdst = 1;
|
2022-04-29 07:36:14 +00:00
|
|
|
t = localtime_time2(tmp, funcp, sp, offset, &okay);
|
2020-06-15 14:18:57 +00:00
|
|
|
if (okay)
|
|
|
|
return t;
|
|
|
|
if (tmp->tm_isdst < 0)
|
2022-04-27 12:39:39 +00:00
|
|
|
#ifdef PCTS
|
|
|
|
/*
|
|
|
|
** POSIX Conformance Test Suite code courtesy Grant Sullivan.
|
|
|
|
*/
|
2020-06-15 14:18:57 +00:00
|
|
|
tmp->tm_isdst = 0; /* reset to std and try again */
|
2022-04-27 12:39:39 +00:00
|
|
|
#else
|
2020-06-15 14:18:57 +00:00
|
|
|
return t;
|
|
|
|
#endif /* !defined PCTS */
|
|
|
|
/*
|
|
|
|
** We're supposed to assume that somebody took a time of one type
|
|
|
|
** and did some math on it that yielded a "struct tm" that's bad.
|
|
|
|
** We try to divine the type they started from and adjust to the
|
|
|
|
** type they need.
|
|
|
|
*/
|
|
|
|
if (sp == NULL)
|
|
|
|
return WRONG;
|
|
|
|
for (i = 0; i < sp->typecnt; ++i)
|
2022-04-27 12:39:39 +00:00
|
|
|
seen[i] = false;
|
2020-06-15 14:18:57 +00:00
|
|
|
nseen = 0;
|
|
|
|
for (i = sp->timecnt - 1; i >= 0; --i)
|
2022-04-27 12:39:39 +00:00
|
|
|
if (!seen[sp->types[i]] && !ttunspecified(sp, sp->types[i])) {
|
|
|
|
seen[sp->types[i]] = true;
|
2020-06-15 14:18:57 +00:00
|
|
|
types[nseen++] = sp->types[i];
|
|
|
|
}
|
|
|
|
for (sameind = 0; sameind < nseen; ++sameind) {
|
|
|
|
samei = types[sameind];
|
|
|
|
if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
|
|
|
|
continue;
|
|
|
|
for (otherind = 0; otherind < nseen; ++otherind) {
|
|
|
|
otheri = types[otherind];
|
|
|
|
if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
|
|
|
|
continue;
|
2022-04-27 12:39:39 +00:00
|
|
|
tmp->tm_sec += (sp->ttis[otheri].tt_utoff
|
|
|
|
- sp->ttis[samei].tt_utoff);
|
2020-06-15 14:18:57 +00:00
|
|
|
tmp->tm_isdst = !tmp->tm_isdst;
|
2022-04-29 07:36:14 +00:00
|
|
|
t = localtime_time2(tmp, funcp, sp, offset, &okay);
|
2020-06-15 14:18:57 +00:00
|
|
|
if (okay)
|
|
|
|
return t;
|
2022-04-27 12:39:39 +00:00
|
|
|
tmp->tm_sec -= (sp->ttis[otheri].tt_utoff
|
|
|
|
- sp->ttis[samei].tt_utoff);
|
2020-06-15 14:18:57 +00:00
|
|
|
tmp->tm_isdst = !tmp->tm_isdst;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return WRONG;
|
|
|
|
}
|
|
|
|
|
2022-05-12 13:11:22 +00:00
|
|
|
static time_t
|
|
|
|
mktime_tzname(struct state *sp, struct tm *tmp, bool setname)
|
2022-04-27 12:39:39 +00:00
|
|
|
{
|
2022-05-12 13:11:22 +00:00
|
|
|
if (sp)
|
2022-04-29 07:36:14 +00:00
|
|
|
return localtime_time1(tmp, localsub, sp, setname);
|
2022-05-12 13:11:22 +00:00
|
|
|
else {
|
2022-04-29 07:36:14 +00:00
|
|
|
localtime_gmtcheck();
|
|
|
|
return localtime_time1(tmp, gmtsub, gmtptr, 0);
|
2022-04-27 12:39:39 +00:00
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 13:11:22 +00:00
|
|
|
time_t
|
2022-04-27 12:39:39 +00:00
|
|
|
mktime(struct tm *tmp)
|
|
|
|
{
|
2022-05-12 13:11:22 +00:00
|
|
|
time_t t;
|
2022-08-13 21:18:02 +00:00
|
|
|
localtime_lock();
|
2022-04-29 07:36:14 +00:00
|
|
|
localtime_tzset_unlocked();
|
2022-05-12 13:11:22 +00:00
|
|
|
t = mktime_tzname(lclptr, tmp, true);
|
2022-06-12 18:47:20 +00:00
|
|
|
localtime_unlock();
|
2022-04-27 12:39:39 +00:00
|
|
|
return t;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
2023-06-09 06:44:03 +00:00
|
|
|
time_t
|
|
|
|
timelocal(struct tm *tmp)
|
|
|
|
{
|
|
|
|
if (tmp != NULL)
|
|
|
|
tmp->tm_isdst = -1; /* in case it wasn't initialized */
|
|
|
|
return mktime(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
time_t
|
|
|
|
timegm(struct tm *tmp)
|
|
|
|
{
|
|
|
|
return timeoff(tmp, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
time_t
|
|
|
|
timeoff(struct tm *tmp, long offset)
|
|
|
|
{
|
|
|
|
if (tmp)
|
|
|
|
tmp->tm_isdst = 0;
|
|
|
|
gmtcheck();
|
|
|
|
return localtime_time1(tmp, gmtsub, gmtptr, offset);
|
|
|
|
}
|
|
|
|
|
2023-10-11 21:54:42 +00:00
|
|
|
static int32_t
|
2022-05-12 13:11:22 +00:00
|
|
|
leapcorr(struct state const *sp, time_t t)
|
2022-04-27 12:39:39 +00:00
|
|
|
{
|
|
|
|
register struct lsinfo const * lp;
|
2020-06-15 14:18:57 +00:00
|
|
|
register int i;
|
|
|
|
|
|
|
|
i = sp->leapcnt;
|
|
|
|
while (--i >= 0) {
|
|
|
|
lp = &sp->lsis[i];
|
2022-04-27 12:39:39 +00:00
|
|
|
if (t >= lp->ls_trans)
|
2020-06-15 14:18:57 +00:00
|
|
|
return lp->ls_corr;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|