Fix mktime so that it does not write over localtime's returned value.
* localtime.c (__localtime_r): New function, with extra arg
specifying where to store result.
(localtime): Use it.
(_tmbuf): New var.
* gmtime.c (__gmtime_r, gmtime, _tmbuf): Likewise.
* mktime.c (__mktime_internal): Conversion function is now
__localtime_r style, not localtime style.
(mktime): Pass __localtime_r, not localtime.
* timegm.c (timegm): Pass __gmtime_r, not gmtime.
* offtime.c (__offtime): New arg specifying where to store result.
* time.h (__mktime_internal, __offtime): Adjust decls accordingly.
(__gmtime_r, __localtime_r): New decls.
#include <stddef.h>
#include <time.h>
+/* Defined in localtime.c. */
+extern struct tm _tmbuf;
+
/* Return the `struct tm' representation of *T in UTC. */
struct tm *
DEFUN(gmtime, (t), CONST time_t *t)
{
- struct tm *tp = __offtime (t, 0L);
+ return __gmtime_r (t, &_tmbuf);
+}
+
+/* Return the `struct tm' representation of *T in UTC,
+ using *TP to store the result. */
+struct tm *
+DEFUN(__gmtime_r, (t, tp),
+ CONST time_t *t AND struct tm *tp)
+{
+ __offtime (t, 0L, tp);
tp->tm_isdst = 0;
tp->tm_gmtoff = 0L;
-/* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
+/* Copyright (C) 1991, 1992, 1993, 1995 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
#include <errno.h>
#include <time.h>
+/* The C Standard says that localtime and gmtime return the same pointer. */
+struct tm _tmbuf;
/* Return the `struct tm' representation of *TIMER in the local timezone. */
struct tm *
localtime (timer)
const time_t *timer;
{
+ return __localtime_r (timer, &_tmbuf);
+}
+
+struct tm *
+__localtime_r (timer, tp)
+ const time_t *timer;
+ struct tm *tp;
+{
extern int __use_tzfile;
extern int __tz_compute __P ((time_t timer, struct tm *tp));
extern int __tzfile_compute __P ((time_t timer,
long int *leap_correct, int *leap_hit));
- register struct tm *tp;
long int leap_correction;
int leap_extra_secs;
}
else
{
- tp = gmtime (timer);
- if (tp == NULL)
+ struct tm *gmtp = __gmtime_r (timer, tp);
+ if (gmtp == NULL)
return NULL;
- if (! __tz_compute (*timer, tp))
+ if (! __tz_compute (*timer, gmtp))
return NULL;
leap_correction = 0L;
leap_extra_secs = 0;
}
- tp = __offtime (timer, __timezone - leap_correction);
+ __offtime (timer, __timezone - leap_correction, tp);
tp->tm_sec += leap_extra_secs;
tp->tm_isdst = __daylight;
tp->tm_gmtoff = __timezone;
time_t
__mktime_internal (timeptr, producer)
struct tm *timeptr;
- struct tm *(*producer) __P ((const time_t *));
+ struct tm *(*producer) __P ((const time_t *, struct tm *));
{
struct tm our_tm; /* our working space */
struct tm *me = &our_tm; /* a pointer to the above */
{
struct tm *guess_tm;
+ struct tm guess_struct;
time_t guess = 0;
time_t distance = 0;
time_t last_distance = 0;
times_through_search++;
- guess_tm = (*producer) (&guess);
+ guess_tm = (*producer) (&guess, &guess_struct);
#ifdef DEBUG
if (debugging_enabled)
#endif
struct tm *timeptr;
{
- return __mktime_internal (timeptr, localtime);
+ return __mktime_internal (timeptr, __localtime_r);
}
#ifdef weak_alias
#define SECS_PER_HOUR (60 * 60)
#define SECS_PER_DAY (SECS_PER_HOUR * 24)
-/* Returns the `struct tm' representation of *T,
- offset OFFSET seconds east of UCT. */
-struct tm *
-DEFUN(__offtime, (t, offset), CONST time_t *t AND long int offset)
+/* 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. */
+void
+DEFUN(__offtime, (t, offset, tp),
+ CONST time_t *t AND long int offset AND struct tm *tp)
{
- static struct tm tbuf;
register long int days, rem;
register int y;
register CONST unsigned short int *ip;
- if (t == NULL)
- return NULL;
-
days = *t / SECS_PER_DAY;
rem = *t % SECS_PER_DAY;
rem += offset;
rem -= SECS_PER_DAY;
++days;
}
- tbuf.tm_hour = rem / SECS_PER_HOUR;
+ tp->tm_hour = rem / SECS_PER_HOUR;
rem %= SECS_PER_HOUR;
- tbuf.tm_min = rem / 60;
- tbuf.tm_sec = rem % 60;
+ tp->tm_min = rem / 60;
+ tp->tm_sec = rem % 60;
/* January 1, 1970 was a Thursday. */
- tbuf.tm_wday = (4 + days) % 7;
- if (tbuf.tm_wday < 0)
- tbuf.tm_wday += 7;
+ tp->tm_wday = (4 + days) % 7;
+ if (tp->tm_wday < 0)
+ tp->tm_wday += 7;
y = 1970;
while (days >= (rem = __isleap(y) ? 366 : 365))
{
--y;
days += __isleap(y) ? 366 : 365;
}
- tbuf.tm_year = y - 1900;
- tbuf.tm_yday = days;
+ tp->tm_year = y - 1900;
+ tp->tm_yday = days;
ip = __mon_lengths[__isleap(y)];
for (y = 0; days >= ip[y]; ++y)
days -= ip[y];
- tbuf.tm_mon = y;
- tbuf.tm_mday = days + 1;
- tbuf.tm_isdst = -1;
-
- return &tbuf;
+ tp->tm_mon = y;
+ tp->tm_mday = days + 1;
+ tp->tm_isdst = -1;
}
normalize TP, given that a `struct tm *' maps to a `time_t' as performed
by FUNC. */
extern time_t __mktime_internal __P ((struct tm *__tp,
- struct tm *(*__func) (const time_t *)));
+ struct tm *(*__func) (const time_t *,
+ struct tm *)));
/* Format TP into S according to FORMAT.
in Universal Coordinated Time (aka Greenwich Mean Time). */
extern struct tm *gmtime __P ((__const time_t *__timer));
+/* Return the `struct tm' representation of *TIMER in UTC,
+ using *TP to store the result. */
+extern struct tm *__gmtime_r __P ((__const time_t *__timer,
+ struct tm *__tp));
+
/* Return the `struct tm' representation
of *TIMER in the local timezone. */
extern struct tm *localtime __P ((__const time_t *__timer));
-/* Return the `struct tm' representation of *TIMER,
- offset OFFSET seconds east of Universal Coordinated Time. */
-extern struct tm *__offtime __P ((__const time_t *__timer,
- long int __offset));
+/* Return the `struct tm' representation of *TIMER in local time,
+ using *TP to store the result. */
+extern struct tm *__localtime_r __P ((__const time_t *__timer,
+ struct tm *__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. */
+extern void __offtime __P ((__const time_t *__timer,
+ long int __offset,
+ struct tm *__TP));
/* Return a string of the form "Day Mon dd hh:mm:ss yyyy\n"
that is the representation of TP in this format. */
struct tm *const tmp;
{
tmp->tm_isdst = 0;
- return __mktime_internal (tmp, gmtime);
+ return __mktime_internal (tmp, __gmtime_r);
}