On 2024-10-01 05:14, Florian Weimer wrote:
> +#ifdef _LIBC
> +# include <tzset.h>
> +#endif
>
> #include <errno.h>
> #include <limits.h>
> @@ -256,11 +259,15 @@ tm_diff (long_int year, long_int yday, int hour, int min, int sec,
> static struct tm *
> convert_time (bool local, long_int t, struct tm *tm)
> {
> +#ifdef _LIBC
> + return __tz_convert (t, local, tm);
> +#else
> __time64_t x = t;
> if (local)
> return __localtime64_r (&x, tm);
> else
> return __gmtime64_r (&x, tm);
> +#endif
Come to think of it, the !_LIBC code needn't fiddle with types like
__time64_t or symbols like __localtime64_r because they're not relevant
in Gnulib.
Please migrate the "#ifdef _LIBC" stuff higher, e.g., something like
this near the top.:
#ifdef _LIBC
# include <tzset.h>
#else
static struct tm *
__tz_convert (time_t t, bool local, struct tm *tm)
{
return (local ? localtime_r : gmtime_r) (&t, tm);
}
#endif
and then change the rest of the file to use __tz_convert instead of
convert_time. That way, we can remove convert_time and the glibc code
will be more uniform about calling __tz_convert.
@@ -49,24 +49,6 @@ extern const unsigned short int __mon_yday[2][13] attribute_hidden;
/* Defined in localtime.c. */
extern struct tm _tmbuf attribute_hidden;
-/* Defined in tzset.c. */
-extern char *__tzstring (const char *string) attribute_hidden;
-
-extern int __use_tzfile attribute_hidden;
-
-extern void __tzfile_read (const char *file, size_t extra,
- char **extrap) attribute_hidden;
-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,
- int stdoff, int dstoff)
- attribute_hidden;
-extern void __tzset_parse_tz (const char *tz) attribute_hidden;
-extern void __tz_compute (__time64_t timer, struct tm *tm, int use_localtime)
- __THROW attribute_hidden;
-
-
#if __TIMESIZE == 64
# define __itimerspec64 itimerspec
#else
@@ -275,11 +257,6 @@ extern int __offtime (__time64_t __timer,
extern char *__asctime_r (const struct tm *__tp, char *__buf)
attribute_hidden;
-extern void __tzset (void) attribute_hidden;
-
-/* Prototype for the internal function to get information based on TZ. */
-extern struct tm *__tz_convert (__time64_t timer, int use_localtime,
- struct tm *tp) attribute_hidden;
extern int __nanosleep (const struct timespec *__requested_time,
struct timespec *__remaining);
@@ -17,13 +17,17 @@
<https://www.gnu.org/licenses/>. */
#include <time.h>
+#include <tzset.h>
/* Return the `struct tm' representation of *T in UTC,
using *TP to store the result. */
struct tm *
__gmtime64_r (const __time64_t *t, struct tm *tp)
{
- return __tz_convert (*t, 0, tp);
+ __libc_lock_lock (__tzset_lock);
+ struct tm *result = __tz_convert (*t, 0, tp);
+ __libc_lock_unlock (__tzset_lock);
+ return result;
}
/* Provide a 32-bit variant if needed. */
@@ -48,7 +52,7 @@ weak_alias (__gmtime_r, gmtime_r)
struct tm *
__gmtime64 (const __time64_t *t)
{
- return __tz_convert (*t, 0, &_tmbuf);
+ return __gmtime64_r (t, &_tmbuf);
}
/* Provide a 32-bit variant if needed. */
@@ -17,6 +17,7 @@
<https://www.gnu.org/licenses/>. */
#include <time.h>
+#include <tzset.h>
/* C89 says that localtime and gmtime return the same pointer.
Although C99 and later relax this to let localtime and gmtime
@@ -30,7 +31,10 @@ struct tm _tmbuf;
struct tm *
__localtime64_r (const __time64_t *t, struct tm *tp)
{
- return __tz_convert (*t, 1, tp);
+ __libc_lock_lock (__tzset_lock);
+ struct tm *result = __tz_convert (*t, 1, tp);
+ __libc_lock_unlock (__tzset_lock);
+ return result;
}
/* Provide a 32-bit variant if needed. */
@@ -54,7 +58,7 @@ weak_alias (__localtime_r, localtime_r)
struct tm *
__localtime64 (const __time64_t *t)
{
- return __tz_convert (*t, 1, &_tmbuf);
+ return __localtime64_r (t, &_tmbuf);
}
libc_hidden_def (__localtime64)
@@ -73,6 +73,8 @@ typedef int mktime_offset_t;
/* Subroutine of mktime. Return the time_t representation of TP and
normalize TP, given that a struct tm * maps to a time_t. If
LOCAL, the mapping is performed by localtime_r, otherwise by gmtime_r.
- Record next guess for localtime-gmtime offset in *OFFSET. */
+ Record next guess for localtime-gmtime offset in *OFFSET.
+
+ If _LIBC, the caller must lock __tzset_lock. */
extern __time64_t __mktime_internal (struct tm *tp, bool local,
mktime_offset_t *offset) attribute_hidden;
@@ -42,6 +42,9 @@
#endif
#include <time.h>
+#ifdef _LIBC
+# include <tzset.h>
+#endif
#include <errno.h>
#include <limits.h>
@@ -256,11 +259,15 @@ tm_diff (long_int year, long_int yday, int hour, int min, int sec,
static struct tm *
convert_time (bool local, long_int t, struct tm *tm)
{
+#ifdef _LIBC
+ return __tz_convert (t, local, tm);
+#else
__time64_t x = t;
if (local)
return __localtime64_r (&x, tm);
else
return __gmtime64_r (&x, tm);
+#endif
}
/* Convert *T to a broken down time in *TP (as if by localtime if
@@ -318,7 +325,9 @@ ranged_convert (bool local, long_int *t, struct tm *tp)
If *OFFSET's guess is correct, only one reverse mapping call is
needed. If successful, set *TP to the canonicalized struct tm;
otherwise leave *TP alone, return ((time_t) -1) and set errno.
- This function is external because it is used also by timegm.c. */
+ This function is external because it is used also by timegm.c.
+
+ If _LIBC, the caller must lock __tzset_lock. */
__time64_t
__mktime_internal (struct tm *tp, bool local, mktime_offset_t *offset)
{
@@ -530,7 +539,19 @@ __mktime_internal (struct tm *tp, bool local, mktime_offset_t *offset)
#endif /* _LIBC || NEED_MKTIME_WORKING || NEED_MKTIME_INTERNAL */
-#if defined _LIBC || NEED_MKTIME_WORKING || NEED_MKTIME_WINDOWS
+#ifdef _LIBC
+__time64_t
+__mktime64 (struct tm *tp)
+{
+ __libc_lock_lock (__tzset_lock);
+ __tzset_unlocked ();
+ static mktime_offset_t localtime_offset;
+ __time64_t result = __mktime_internal (tp, true, &localtime_offset);
+ __libc_lock_unlock (__tzset_lock);
+ return result;
+}
+#else /* !_LIBC */
+# if defined NEED_MKTIME_WORKING || NEED_MKTIME_WINDOWS
/* Convert *TP to a __time64_t value. */
__time64_t
@@ -541,15 +562,16 @@ __mktime64 (struct tm *tp)
be set as if the tzset() function had been called. */
__tzset ();
-# if defined _LIBC || NEED_MKTIME_WORKING
+# ifdef NEED_MKTIME_WORKING
static mktime_offset_t localtime_offset;
return __mktime_internal (tp, true, &localtime_offset);
-# else
-# undef mktime
+# else
+# undef mktime
return mktime (tp);
-# endif
+# endif
}
-#endif /* _LIBC || NEED_MKTIME_WORKING || NEED_MKTIME_WINDOWS */
+# endif /* NEED_MKTIME_WORKING || NEED_MKTIME_WINDOWS */
+#endif /* !_LIBC */
#if defined _LIBC && __TIMESIZE != 64
@@ -33,6 +33,7 @@
# define MULTIBYTE_IS_FORMAT_SAFE 1
# define STDC_HEADERS 1
# include "../locale/localeinfo.h"
+# include <tzset.h>
#endif
#if defined emacs && !defined HAVE_BCOPY
@@ -24,6 +24,10 @@
#include <time.h>
#include <errno.h>
+#ifdef _LIBC
+# include <tzset.h>
+#endif
+
#include "mktime-internal.h"
__time64_t
@@ -31,7 +35,14 @@ __timegm64 (struct tm *tmp)
{
static mktime_offset_t gmtime_offset;
tmp->tm_isdst = 0;
+#ifdef _LIBC
+ __libc_lock_lock (__tzset_lock);
+ __time64_t result = __mktime_internal (tmp, false, &gmtime_offset);
+ __libc_lock_unlock (__tzset_lock);
+ return result;
+#else
return __mktime_internal (tmp, false, &gmtime_offset);
+#endif
}
#if defined _LIBC && __TIMESIZE != 64
@@ -27,6 +27,7 @@
#include <stdint.h>
#include <alloc_buffer.h>
#include <set-freeres.h>
+#include <tzset.h>
#include <timezone/tzfile.h>
@@ -16,13 +16,13 @@
<https://www.gnu.org/licenses/>. */
#include <ctype.h>
-#include <libc-lock.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
+#include <tzset.h>
#include <timezone/tzfile.h>
@@ -37,7 +37,7 @@ weak_alias (__daylight, daylight)
weak_alias (__timezone, timezone)
/* This locks all the state variables in tzfile.c and this file. */
-__libc_lock_define_initialized (static, tzset_lock)
+__libc_lock_define_initialized (, __tzset_lock)
/* This structure contains all the information about a
timezone given in the POSIX standard TZ envariable. */
@@ -544,9 +544,8 @@ __tz_compute (__time64_t timer, struct tm *tm, int use_localtime)
#undef tzset
void
-__tzset (void)
+__tzset_unlocked (void)
{
- __libc_lock_lock (tzset_lock);
tzset_internal (1);
@@ -557,8 +556,16 @@ __tzset (void)
__tzname[1] = (char *) tz_rules[1].name;
}
- __libc_lock_unlock (tzset_lock);
}
+
+void
+__tzset (void)
+{
+ __libc_lock_lock (__tzset_lock);
+ __tzset_unlocked ();
+ __libc_lock_unlock (__tzset_lock);
+}
+
weak_alias (__tzset, tzset)
/* Return the `struct tm' representation of TIMER in the local timezone.
@@ -569,7 +576,6 @@ __tz_convert (__time64_t timer, int use_localtime, struct tm *tp)
long int leap_correction;
int leap_extra_secs;
- __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.
@@ -589,8 +595,6 @@ __tz_convert (__time64_t timer, int use_localtime, struct tm *tp)
leap_extra_secs = 0;
}
- __libc_lock_unlock (tzset_lock);
-
if (tp)
{
if (! use_localtime)
new file mode 100644
@@ -0,0 +1,34 @@
+#ifndef _TZSET_H
+#define _TZSET_H
+
+#include <time.h>
+#include <libc-lock.h>
+
+/* Defined in tzset.c. */
+extern char *__tzstring (const char *string) attribute_hidden;
+
+extern int __use_tzfile attribute_hidden;
+
+extern void __tzfile_read (const char *file, size_t extra,
+ char **extrap) attribute_hidden;
+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,
+ int stdoff, int dstoff)
+ attribute_hidden;
+extern void __tzset_parse_tz (const char *tz) attribute_hidden;
+extern void __tz_compute (__time64_t timer, struct tm *tm, int use_localtime)
+ __THROW attribute_hidden;
+
+__libc_lock_define (extern, __tzset_lock attribute_hidden);
+
+extern void __tzset (void) attribute_hidden;
+extern void __tzset_unlocked (void) attribute_hidden;
+
+/* Prototype for the internal function to get information based on TZ.
+ Caller needs to lock __tzset_lock. */
+extern struct tm *__tz_convert (__time64_t timer, int use_localtime,
+ struct tm *tp) attribute_hidden;
+
+#endif /* _TZSET_H */