From a12eb6b9292bc9603e6e5c879748ab357c748c4a Mon Sep 17 00:00:00 2001 From: "Albert ARIBAUD (3ADEV)" Date: Fri, 8 Sep 2017 00:41:30 +0200 Subject: Y2038: add functions which have struct tm arguments This consists in the following implementation additions: ([file] 32-bit implementation -> [file] 64-bit addition) time/ctime.c ctime() -> __ctime64() time/ctime_r.c ctime_r() -> __ctime64_r() time/gmtime.c gmtime() -> __gmtime64() time/gmtime.c gmtime_r() -> __gmtime64_r() time/localtime.c localtime() -> __localtime64() time/localtime.c localtime_r() -> __localtime64_r() which require the following internal function additions time/offtime.c __offtime() -> __offtime64() time/tzset.c __tz_convert() -> __tz64_convert() and internal function (32-bit-time-compatible) changes time/tzfile.c __tzfile_compute() time/tzset.c tzset_internal() time/tzset.c compute_change() --- include/sys/time.h | 1 + include/time.h | 34 ++++++++++++++++++++++++----- time/Versions | 3 +++ time/ctime.c | 10 +++++++++ time/ctime_r.c | 9 ++++++++ time/gmtime.c | 15 +++++++++++++ time/localtime.c | 17 +++++++++++++++ time/offtime.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ time/tzfile.c | 4 ++-- time/tzset.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 10 files changed, 209 insertions(+), 12 deletions(-) diff --git a/include/sys/time.h b/include/sys/time.h index 98f6b6b2cc..fa8e33fef8 100644 --- a/include/sys/time.h +++ b/include/sys/time.h @@ -39,5 +39,6 @@ extern int __utimes (const char *__file, const struct timeval __tvp[2]) attribute_hidden; extern int __futimes (int fd, const struct timeval tvp[2]) attribute_hidden; +extern struct tm * __localtime64 (const __time64_t *__timer); # endif #endif diff --git a/include/time.h b/include/time.h index 7eda265a66..894dcdb0b3 100644 --- a/include/time.h +++ b/include/time.h @@ -9,6 +9,8 @@ extern __typeof (strftime_l) __strftime_l; libc_hidden_proto (__strftime_l) extern __typeof (strptime_l) __strptime_l; +extern struct tm *__localtime64 (const __time64_t *__timer); + libc_hidden_proto (time) libc_hidden_proto (asctime) libc_hidden_proto (mktime) @@ -17,6 +19,8 @@ libc_hidden_proto (localtime) libc_hidden_proto (strftime) libc_hidden_proto (strptime) +libc_hidden_proto (__localtime64) + extern __typeof (clock_getres) __clock_getres; extern __typeof (clock_gettime) __clock_gettime; libc_hidden_proto (__clock_gettime) @@ -40,14 +44,14 @@ extern int __use_tzfile attribute_hidden; extern void __tzfile_read (const char *file, size_t extra, char **extrap) attribute_hidden; -extern void __tzfile_compute (time_t timer, int use_localtime, +extern void __tzfile_compute (__time64_t timer, int use_localtime, long int *leap_correct, int *leap_hit, struct tm *tp) attribute_hidden; extern void __tzfile_default (const char *std, const char *dst, long int stdoff, long int dstoff) attribute_hidden; extern void __tzset_parse_tz (const char *tz) attribute_hidden; -extern void __tz_compute (time_t timer, struct tm *tm, int use_localtime) +extern void __tz_compute (__time64_t timer, struct tm *tm, int use_localtime) __THROW attribute_hidden; /* Subroutine of `mktime'. Return the `time_t' representation of TP and @@ -60,15 +64,21 @@ extern time_t __mktime_internal (struct tm *__tp, extern struct tm *__localtime_r (const time_t *__timer, struct tm *__tp) attribute_hidden; -extern struct tm *__gmtime_r (const time_t *__restrict __timer, +extern struct tm *__localtime64_r (const __time64_t *__timer, + struct tm *__tp) attribute_hidden; + +extern struct tm *__gmtime_r (const __time_t *__restrict __timer, struct tm *__restrict __tp); libc_hidden_proto (__gmtime_r) +extern struct tm *__gmtime64_r (const __time64_t *__restrict __timer, + struct tm *__restrict __tp); + /* Compute the `struct tm' representation of *T, offset OFFSET seconds east of UTC, and store year, yday, mon, mday, wday, hour, min, sec into *TP. Return nonzero if successful. */ -extern int __offtime (const time_t *__timer, +extern int __offtime (const __time_t *__timer, long int __offset, struct tm *__tp) attribute_hidden; @@ -76,9 +86,21 @@ extern char *__asctime_r (const struct tm *__tp, char *__buf) attribute_hidden; extern void __tzset (void) attribute_hidden; +extern int __offtime64 (const __time64_t *__timer, + long int __offset, + struct tm *__tp) + attribute_hidden; + +extern char *__asctime_r (const struct tm *__tp, char *__buf); +extern void __tzset (void); + /* Prototype for the internal function to get information based on TZ. */ -extern struct tm *__tz_convert (const time_t *timer, int use_localtime, - struct tm *tp) attribute_hidden; +extern struct tm *__tz_convert (const __time_t *timer, int use_localtime, + struct tm *tp) attribute_hidden; + +extern struct tm *__tz_convert64 (const __time64_t *timer, + int use_localtime, struct tm *tp) + attribute_hidden; extern int __nanosleep (const struct timespec *__requested_time, struct timespec *__remaining); diff --git a/time/Versions b/time/Versions index 57314b98c8..0ad2749f2c 100644 --- a/time/Versions +++ b/time/Versions @@ -67,5 +67,8 @@ libc { } GLIBC_2.27 { __difftime64; + __ctime64; __ctime64_r; + __gmtime64; __gmtime64_r; + __localtime64; __localtime64_r; } } diff --git a/time/ctime.c b/time/ctime.c index 1222614f29..fbfb61fcd6 100644 --- a/time/ctime.c +++ b/time/ctime.c @@ -26,3 +26,13 @@ ctime (const time_t *t) In particular, ctime and asctime must yield the same pointer. */ return asctime (localtime (t)); } + +/* Return a string as returned by asctime which + is the representation of *T in that form. */ +char * +__ctime64 (const __time64_t *t) +{ + /* Apply the same rule as ctime: + make ctime64 (t) is equivalent to asctime (localtime64 (t)). */ + return asctime (__localtime64 (t)); +} diff --git a/time/ctime_r.c b/time/ctime_r.c index c111146d76..742a967fdd 100644 --- a/time/ctime_r.c +++ b/time/ctime_r.c @@ -27,3 +27,12 @@ ctime_r (const time_t *t, char *buf) struct tm tm; return __asctime_r (__localtime_r (t, &tm), buf); } + +/* Return a string as returned by asctime which is the representation + of *T in that form. Reentrant Y2038-proof version. */ +char * +__ctime64_r (const __time64_t *t, char *buf) +{ + struct tm tm; + return __asctime_r (__localtime64_r (t, &tm), buf); +} diff --git a/time/gmtime.c b/time/gmtime.c index dc33b3e68a..6d6878a1ea 100644 --- a/time/gmtime.c +++ b/time/gmtime.c @@ -35,3 +35,18 @@ gmtime (const time_t *t) { return __tz_convert (t, 0, &_tmbuf); } + +/* Return the `struct tm' representation of 64-bit-time *T + in UTC, using *TP to store the result. */ +struct tm * +__gmtime64_r (const __time64_t *t, struct tm *tp) +{ + return __tz_convert64 (t, 0, tp); +} + +/* Return the `struct tm' representation of 64-bit-time *T in UTC. */ +struct tm * +__gmtime64 (const __time64_t *t) +{ + return __tz_convert64 (t, 0, &_tmbuf); +} diff --git a/time/localtime.c b/time/localtime.c index 8684a8a971..63dd125638 100644 --- a/time/localtime.c +++ b/time/localtime.c @@ -39,3 +39,20 @@ localtime (const time_t *t) return __tz_convert (t, 1, &_tmbuf); } libc_hidden_def (localtime) + +/* 64-bit-time versions */ + +/* Return the `struct tm' representation of *T in local time, + using *TP to store the result. */ +struct tm * +__localtime64_r (const __time64_t *t, struct tm *tp) +{ + return __tz_convert64 (t, 1, tp); +} + +/* Return the `struct tm' representation of *T in local time. */ +struct tm * +__localtime64 (const __time64_t *t) +{ + return __tz_convert64 (t, 1, &_tmbuf); +} diff --git a/time/offtime.c b/time/offtime.c index 04c48389fc..650ddcce38 100644 --- a/time/offtime.c +++ b/time/offtime.c @@ -84,3 +84,67 @@ __offtime (const time_t *t, long int offset, struct tm *tp) tp->tm_mday = days + 1; return 1; } + +/* Compute the `struct tm' representation of 64-bit-time *T, + offset OFFSET seconds east of UTC, + and store year, yday, mon, mday, wday, hour, min, sec into *TP. + Return nonzero if successful. */ +int +__offtime64 (const __time64_t *t, long int offset, struct tm *tp) +{ + __time64_t days, rem, y; + const unsigned short int *ip; + + days = *t / SECS_PER_DAY; + rem = *t % SECS_PER_DAY; + rem += offset; + while (rem < 0) + { + rem += SECS_PER_DAY; + --days; + } + while (rem >= SECS_PER_DAY) + { + rem -= SECS_PER_DAY; + ++days; + } + tp->tm_hour = rem / SECS_PER_HOUR; + rem %= SECS_PER_HOUR; + tp->tm_min = rem / 60; + tp->tm_sec = rem % 60; + /* January 1, 1970 was a Thursday. */ + tp->tm_wday = (4 + days) % 7; + if (tp->tm_wday < 0) + tp->tm_wday += 7; + y = 1970; + +#define DIV(a, b) ((a) / (b) - ((a) % (b) < 0)) +#define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400)) + + while (days < 0 || days >= (__isleap (y) ? 366 : 365)) + { + /* Guess a corrected year, assuming 365 days per year. */ + __time64_t yg = y + days / 365 - (days % 365 < 0); + + /* Adjust DAYS and Y to match the guessed year. */ + days -= ((yg - y) * 365 + + LEAPS_THRU_END_OF (yg - 1) + - LEAPS_THRU_END_OF (y - 1)); + y = yg; + } + tp->tm_year = y - 1900; + if (tp->tm_year != y - 1900) + { + /* The year cannot be represented due to overflow. */ + __set_errno (EOVERFLOW); + return 0; + } + tp->tm_yday = days; + ip = __mon_yday[__isleap(y)]; + for (y = 11; days < (long int) ip[y]; --y) + continue; + days -= ip[y]; + tp->tm_mon = y; + tp->tm_mday = days + 1; + return 1; +} diff --git a/time/tzfile.c b/time/tzfile.c index 1e59fd5abc..8ec9834d28 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -630,7 +630,7 @@ __tzfile_default (const char *std, const char *dst, } void -__tzfile_compute (time_t timer, int use_localtime, +__tzfile_compute (__time64_t timer, int use_localtime, long int *leap_correct, int *leap_hit, struct tm *tp) { @@ -685,7 +685,7 @@ __tzfile_compute (time_t timer, int use_localtime, /* Convert to broken down structure. If this fails do not use the string. */ - if (__glibc_unlikely (! __offtime (&timer, 0, tp))) + if (__glibc_unlikely (! __offtime64 (&timer, 0, tp))) goto use_last; /* Use the rules from the TZ string to compute the change. */ diff --git a/time/tzset.c b/time/tzset.c index b51786704a..106966f4dd 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -55,7 +55,7 @@ typedef struct /* We cache the computed time of change for a given year so we don't have to recompute it. */ - time_t change; /* When to change to this zone. */ + __time64_t change; /* When to change to this zone. */ int computed_for; /* Year above is computed for. */ } tz_rule; @@ -416,7 +416,7 @@ tzset_internal (int always) tz_rules[0].name = tz_rules[1].name = "UTC"; if (J0 != 0) tz_rules[0].type = tz_rules[1].type = J0; - tz_rules[0].change = tz_rules[1].change = (time_t) -1; + tz_rules[0].change = tz_rules[1].change = (__time64_t) -1; update_vars (); return; } @@ -514,9 +514,10 @@ compute_change (tz_rule *rule, int year) /* Figure out the correct timezone for TM and set `__tzname', - `__timezone', and `__daylight' accordingly. */ + `__timezone', and `__daylight' accordingly. + NOTE: this takes a __time64_t value, so passing a __time_t value is OK. */ void -__tz_compute (time_t timer, struct tm *tm, int use_localtime) +__tz_compute (__time64_t timer, struct tm *tm, int use_localtime) { compute_change (&tz_rules[0], 1900 + tm->tm_year); compute_change (&tz_rules[1], 1900 + tm->tm_year); @@ -617,6 +618,61 @@ __tz_convert (const time_t *timer, int use_localtime, struct tm *tp) } +/* Return the `struct tm' representation of *TIMER in the local timezone. + Use local time if USE_LOCALTIME is nonzero, UTC otherwise. */ +struct tm * +__tz_convert64 (const __time64_t *timer, int use_localtime, struct tm *tp) +{ + long int leap_correction; + int leap_extra_secs; + + if (timer == NULL) + { + __set_errno (EINVAL); + return NULL; + } + + __libc_lock_lock (tzset_lock); + + /* Update internal database according to current TZ setting. + POSIX.1 8.3.7.2 says that localtime_r is not required to set tzname. + This is a good idea since this allows at least a bit more parallelism. */ + tzset_internal (tp == &_tmbuf && use_localtime); + + if (__use_tzfile) + __tzfile_compute (*timer, use_localtime, &leap_correction, + &leap_extra_secs, tp); + else + { + if (! __offtime64 (timer, 0, tp)) + tp = NULL; + else + __tz_compute (*timer, tp, use_localtime); + leap_correction = 0L; + leap_extra_secs = 0; + } + + __libc_lock_unlock (tzset_lock); + + if (tp) + { + if (! use_localtime) + { + tp->tm_isdst = 0; + tp->tm_zone = "GMT"; + tp->tm_gmtoff = 0L; + } + + if (__offtime64 (timer, tp->tm_gmtoff - leap_correction, tp)) + tp->tm_sec += leap_extra_secs; + else + tp = NULL; + } + + return tp; +} + + libc_freeres_fn (free_mem) { while (tzstring_list != NULL) -- cgit v1.2.1