diff options
author | Albert ARIBAUD (3ADEV) <albert.aribaud@3adev.fr> | 2017-03-29 12:01:45 +0200 |
---|---|---|
committer | Albert ARIBAUD (3ADEV) <albert.aribaud@3adev.fr> | 2017-04-20 16:14:08 +0200 |
commit | 0747e2cb1a2e8f0793ff4c6b52ce8f0a7c82c795 (patch) | |
tree | 381aeeb68b94c6bc4d5916b56a0fe9670fac0aee | |
parent | 85eb0089bef8d5ffda9d6e98c76c7baf44129706 (diff) | |
download | glibc-0747e2cb1a2e8f0793ff4c6b52ce8f0a7c82c795.tar.gz |
Add 64-bit 'broken-up time' APIs (see detail in commit msg)
This consists in the following API additions:
([file] 32-bit function -> [file] 64-bit variant)
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 additions
time/bits/types/time_t.h time_t -> time64_t
time/offtime.c __offtime() -> __offtime64()
time/tzset.c __tz_convert() -> __tz64_convert()
and modifications to internal functions:
time/tzfile.c __tzfile_compute()
time/tzset.c tzset_internal()
time/tzset.c compute_change()
-rw-r--r-- | include/time.h | 24 | ||||
-rw-r--r-- | sysdeps/unix/sysv/linux/arm/Versions | 4 | ||||
-rw-r--r-- | time/ctime.c | 10 | ||||
-rw-r--r-- | time/ctime_r.c | 9 | ||||
-rw-r--r-- | time/gmtime.c | 15 | ||||
-rw-r--r-- | time/localtime.c | 17 | ||||
-rw-r--r-- | time/offtime.c | 64 | ||||
-rw-r--r-- | time/time.h | 64 | ||||
-rw-r--r-- | time/tzfile.c | 4 | ||||
-rw-r--r-- | time/tzset.c | 64 |
10 files changed, 260 insertions, 15 deletions
diff --git a/include/time.h b/include/time.h index b4e2f6a63e..f873443604 100644 --- a/include/time.h +++ b/include/time.h @@ -13,6 +13,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) @@ -68,13 +70,13 @@ extern int __use_tzfile attribute_hidden; extern void __tzfile_read (const char *file, size_t extra, char **extrap); -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); extern void __tzfile_default (const char *std, const char *dst, long int stdoff, long int dstoff); extern void __tzset_parse_tz (const char *tz); -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 internal_function; /* Subroutine of `mktime'. Return the `time_t' representation of TP and @@ -87,15 +89,25 @@ 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); + +extern int __offtime64 (const __time64_t *__timer, long int __offset, struct tm *__tp); @@ -103,7 +115,9 @@ 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); +extern struct tm *__tz_convert (const __time_t *timer, int use_localtime, struct tm *tp); + +extern struct tm *__tz_convert64 (const __time64_t *timer, int use_localtime, struct tm *tp); /* Return the maximum length of a timezone name. This is what `sysconf (_SC_TZNAME_MAX)' does. */ diff --git a/sysdeps/unix/sysv/linux/arm/Versions b/sysdeps/unix/sysv/linux/arm/Versions index ac557d5eb8..735e97c7f3 100644 --- a/sysdeps/unix/sysv/linux/arm/Versions +++ b/sysdeps/unix/sysv/linux/arm/Versions @@ -20,5 +20,9 @@ libc { __vdso_clock_gettime64; # Y2038-proof difftime is actually __difftime64 __difftime64; + # Y2038-proof 'broken-up time' APIs + __ctime64; __ctime64_r; + __gmtime64; __gmtime64_r; + __localtime64; __localtime64_r; } } diff --git a/time/ctime.c b/time/ctime.c index 451d7b9fa9..d6282fa455 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 0fcf9d8e8e..4ef8100f48 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 b3b5d0d4d7..04333bf9cd 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 8ec40cf333..875a3b1e3a 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 b540b2f372..e660f69be4 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/time.h b/time/time.h index 1274525570..fdd9d5efcf 100644 --- a/time/time.h +++ b/time/time.h @@ -266,37 +266,84 @@ extern char *strptime_l (const char *__restrict __s, __BEGIN_NAMESPACE_STD + /* Return the `struct tm' representation of *TIMER in Universal Coordinated Time (aka Greenwich Mean Time). */ +#ifdef __USE_TIME_BITS64 +# if defined(__REDIRECT) +extern struct tm * __REDIRECT (gmtime, (const time_t *__timer), + __gmtime64) __THROW; +# else +# define gmtime __gmtime64 +# endif +#endif extern struct tm *gmtime (const time_t *__timer) __THROW; /* Return the `struct tm' representation of *TIMER in the local timezone. */ +#ifdef __USE_TIME_BITS64 +# if defined(__REDIRECT) +extern struct tm * __REDIRECT (localtime, (const time_t *__timer), + __localtime64) __THROW; +# else +# define localtime __localtime64 +# endif +#endif extern struct tm *localtime (const time_t *__timer) __THROW; + __END_NAMESPACE_STD -# ifdef __USE_POSIX +#ifdef __USE_POSIX + /* Return the `struct tm' representation of *TIMER in UTC, using *TP to store the result. */ +#ifdef __USE_TIME_BITS64 +# if defined(__REDIRECT) +extern struct tm * __REDIRECT (gmtime_r, (const time_t *__restrict + __timer, struct tm *__restrict __tp), __gmtime64_r) __THROW; +# else +# define gmtime_r __gmtime64_r +# endif +#endif extern struct tm *gmtime_r (const time_t *__restrict __timer, struct tm *__restrict __tp) __THROW; /* Return the `struct tm' representation of *TIMER in local time, using *TP to store the result. */ +#ifdef __USE_TIME_BITS64 +# if defined(__REDIRECT) +extern struct tm * __REDIRECT (localtime_r, (const time_t *__restrict + __timer, struct tm *__restrict __tp), __localtime64_r) __THROW; +# else +# define localtime_r __localtime64_r +# endif +#endif extern struct tm *localtime_r (const time_t *__restrict __timer, struct tm *__restrict __tp) __THROW; -# endif /* POSIX */ + +#endif /* POSIX */ __BEGIN_NAMESPACE_STD + /* Return a string of the form "Day Mon dd hh:mm:ss yyyy\n" that is the representation of TP in this format. */ extern char *asctime (const struct tm *__tp) __THROW; /* Equivalent to `asctime (localtime (timer))'. */ +#ifdef __USE_TIME_BITS64 +# if defined(__REDIRECT) +extern char * __REDIRECT (ctime, (const time_t *__timer), + __ctime64) __THROW; +# else +# define ctime __ctime64 +# endif +#endif extern char *ctime (const time_t *__timer) __THROW; + __END_NAMESPACE_STD -# ifdef __USE_POSIX +#ifdef __USE_POSIX + /* Reentrant versions of the above functions. */ /* Return in BUF a string of the form "Day Mon dd hh:mm:ss yyyy\n" @@ -305,9 +352,18 @@ extern char *asctime_r (const struct tm *__restrict __tp, char *__restrict __buf) __THROW; /* Equivalent to `asctime_r (localtime_r (timer, *TMP*), buf)'. */ +#ifdef __USE_TIME_BITS64 +# if defined(__REDIRECT) +extern char * __REDIRECT (ctime_r, (const time_t *__restrict __timer, + char *__restrict __buf), __ctime64_r) __THROW; +# else +# define ctime_r __ctime64_r +# endif +#endif extern char *ctime_r (const time_t *__restrict __timer, char *__restrict __buf) __THROW; -# endif /* POSIX */ + +#endif /* POSIX */ /* Defined in localtime.c. */ diff --git a/time/tzfile.c b/time/tzfile.c index 9049878399..0872eea169 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -637,7 +637,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) { @@ -692,7 +692,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 f65116ce24..f6145da7a1 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -61,7 +61,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; @@ -451,7 +451,7 @@ tzset_internal (int always, int explicit) 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; } @@ -550,10 +550,11 @@ 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 internal_function -__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); @@ -654,6 +655,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, 1); + + 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) |