Index: trunk/src/emx/src/lib/libc.def
===================================================================
--- trunk/src/emx/src/lib/libc.def	(revision 902)
+++ trunk/src/emx/src/lib/libc.def	(revision 903)
@@ -299,5 +299,5 @@
     "__std_globfree" @335
     "__gmt2loc" @336
-    "__gmtime" @337
+    "__std_gmtime_r" @337
     "__hcalloc" @338
     "__heap_walk" @339
@@ -321,5 +321,5 @@
     "__lmalloc" @357
     "__loc2gmt" @358
-    "__localtime" @359
+    "__std_localtime_r" @359
     "__lrealloc" @360
     "__makepath" @361
@@ -824,2 +824,4 @@
     "_toascii" @860
     "_tolower" @861
+    "__std_asctime_r" @862
+    "__std_ctime_r" @863
Index: trunk/src/emx/src/lib/time/asctime.c
===================================================================
--- trunk/src/emx/src/lib/time/asctime.c	(revision 902)
+++ trunk/src/emx/src/lib/time/asctime.c	(revision 903)
@@ -14,6 +14,9 @@
 {
   struct _thread *tp = _thread();
-#define result (tp->_th_asctime_buf)
+  return asctime_r(t, tp->_th_asctime_buf);
+}
 
+char *_STD(asctime_r)(const struct tm *t, char *result)
+{
   memcpy (result+0, wdays+t->tm_wday*3, 3);
   result[3] = ' ';
@@ -41,2 +44,3 @@
   return result;
 }
+
Index: trunk/src/emx/src/lib/time/ctime.c
===================================================================
--- trunk/src/emx/src/lib/time/ctime.c	(revision 902)
+++ trunk/src/emx/src/lib/time/ctime.c	(revision 903)
@@ -1,16 +1,24 @@
-/* ctime.c (emx+gcc) -- Copyright (c) 1990-1999 by Eberhard Mattes */
-
+/* ctime.c (emx+gcc) -- Copyright (c) 1990-1999 by Eberhard Mattes
+                     -- Copyright (c) 2003 by Knut Stange Osmundsen */
 #include "libc-alias.h"
 #include <time.h>
 #include <emx/time.h>
 
-char *_STD(ctime) (const time_t *t)
+char *_STD(ctime)(const time_t *t)
 {
-  struct tm tmp, *x;
+    struct tm tmp, *pTm;
 
-  x = _localtime (&tmp, t);
-  if (x == NULL)
+    pTm = localtime_r(t, &tmp);
+    if (pTm != NULL)
+        return asctime(pTm);
     return NULL;
-  else
-    return asctime (x);
 }
+
+char *_STD(ctime_r)(const time_t * t, char *buf)
+{
+    struct tm tmp, *pTm;
+    pTm = localtime_r(t, &tmp);
+    if (pTm != NULL)
+        return asctime_r(pTm, buf);
+    return NULL;
+}
Index: trunk/src/emx/src/lib/time/gmtime.c
===================================================================
--- trunk/src/emx/src/lib/time/gmtime.c	(revision 902)
+++ trunk/src/emx/src/lib/time/gmtime.c	(revision 903)
@@ -7,56 +7,67 @@
 #include <emx/time.h>
 
-/* mkstd.awk: NOUNDERSCORE(gmtime) */
-struct tm *_gmtime (struct tm *dst, const time_t *t)
+struct tm *_STD(gmtime_r)(const time_t *t, struct tm *dst)
 {
-  time_t t0;
-  _uldiv_t q;
+    int days;
+    int rem;
 
-  t0 = *t;
+    /* calc days relative to Epoch. */
+    rem = *t;
+    days = rem / (60*60*24);
+    rem %= (60*60*24);
+    while (rem < 0)
+    {
+        rem += (60*60*24);
+        --days;
+    }
 
-  q = _uldiv (t0, 60); dst->tm_sec = q.rem; t0 = q.quot;
-  q = _uldiv (t0, 60); dst->tm_min = q.rem; t0 = q.quot;
-  q = _uldiv (t0, 24); dst->tm_hour = q.rem; t0 = q.quot;
-  dst->tm_wday = (t0+4) % 7;  /* 01-Jan-1970 was Thursday, ie, 4 */
+    dst->tm_hour= rem / (60*60);
+    rem %= (60*60);
+    dst->tm_min = rem / 60;
+    dst->tm_sec = rem % 60;
 
-  {
-    int lo, hi, i;
+    dst->tm_wday = (days + 4) % 7;    /* 01-Jan-1970 was Thursday, i.e. 4 */
+    if (dst->tm_wday < 0)
+        dst->tm_wday += 7;
 
-    /* Find an i such that _year_day[i] <= t0 < _year_day[i+1]. */
+    {
+        int lo, hi, i;
 
-    lo = 0; hi = _YEARS - 1;
-    for (;;)
-      {
-        i = (lo + hi) / 2;
-        if (_year_day[i] > (int)t0)
-          hi = i - 1;
-        else if (_year_day[i+1] <= (int)t0)
-          lo = i + 1;
-        else
-          break;
-      }
-    dst->tm_year = i + 70;
-    t0 -= _year_day[i];
-    dst->tm_yday = t0;
-  }
+        /* Find an i such that _year_day[i] <= days < _year_day[i+1]. */
 
-  {
-    int i;
-    const unsigned short *p;
+        lo = 0; hi = _YEARS - 1;
+        for (;;)
+        {
+            i = (lo + hi) / 2;
+            if (_year_day[i] > days)
+                hi = i - 1;
+            else if (_year_day[i+1] <= days)
+                lo = i + 1;
+            else
+                break;
+        }
+        dst->tm_year = i;
+        days -= _year_day[i];
+        dst->tm_yday = days;
+    }
 
-    p = (_leap_year (dst->tm_year + 1900)
-         ? _month_day_leap : _month_day_non_leap);
-    for (i = 0; (int)t0 >= p[i+1]; ++i)
-      ;
-    dst->tm_mon = i;
-    dst->tm_mday = t0 - p[i] + 1;
-  }
-  dst->tm_isdst = -1;
-  return dst;
+    {
+        int i;
+        const unsigned short *p;
+
+        p = (_leap_year (dst->tm_year + 1900)
+             ? _month_day_leap : _month_day_non_leap);
+        for (i = 0; (int)days >= p[i+1]; ++i)
+            ;
+        dst->tm_mon = i;
+        dst->tm_mday = days - p[i] + 1;
+    }
+    dst->tm_isdst = -1;
+    return dst;
 }
 
-struct tm *_STD(gmtime) (const time_t *t)
+struct tm *_STD(gmtime)(const time_t *t)
 {
-  struct _thread *tp = _thread ();
-  return _gmtime (&tp->_th_gmtime_buf, t);
+    struct _thread *tp = _thread ();
+    return gmtime_r (t, &tp->_th_gmtime_buf);
 }
Index: trunk/src/emx/src/lib/time/gmtloc.c
===================================================================
--- trunk/src/emx/src/lib/time/gmtloc.c	(revision 902)
+++ trunk/src/emx/src/lib/time/gmtloc.c	(revision 903)
@@ -6,10 +6,12 @@
 #include <emx/time.h>
 
-/* Return true iff adding A to T does not overflow time_t. */
-
+/* Return true iff adding A to T does not overflow or underflow time_t.
+   (Correct but not fast!!!) */
 #define ADD_OK(t,a) \
-    ((a) < 0 ? (t) >= (time_t)(-(a)) \
-   : (a) > 0 ? (t) <= TIME_T_MAX - (a) \
-   : 1)
+    ( (t) == 0 || (a) == 0 \
+     || ((a) > 0 && (t) > 0 && (a) + (t) > (a)) \
+     || ((a) > 0 && (t) < 0 && (a) + (t) < (a)) \
+     || ((a) < 0 && (t) < 0 && (a) + (t) < (a)) \
+     || ((a) < 0 && (t) > 0 && (a) + (t) > (a)) )
 
 struct _dstswitch
@@ -19,5 +21,5 @@
 };
 
-static struct _dstswitch _dstsw[2*_YEARS+2] = {{0,0}, {TIME_T_MAX, 0}};
+static struct _dstswitch _dstsw[2*_YEARS+2] = {{TIME_T_MIN, 0}, {TIME_T_MAX, 0}};
 static int _dstsw_count = 2;
 
@@ -65,5 +67,5 @@
   if (!_tzi.dst)
     {
-      _dstsw[0].time = 0;
+      _dstsw[0].time = TIME_T_MIN;
       _dstsw[0].shift = 0;
       _dstsw[1].time = TIME_T_MAX;
@@ -74,7 +76,7 @@
 
   i = 0;
-  for (y = 0; _year_day[y] != USHRT_MAX; ++y)
-    {
-      month_table = (_leap_year (y + 1970)
+  for (y = 2; _year_day[y] != SHRT_MAX; ++y) /* 1900, 1901, 1902 underflows second count. */
+    {
+      month_table = (_leap_year (y + 1900)
                      ? _month_day_leap : _month_day_non_leap);
       d_year = _year_day[y];
@@ -87,5 +89,5 @@
         t_start += t_year;
       else
-        t_start = TIME_T_MAX;
+        t_start = t_year >= 0 ? TIME_T_MAX : TIME_T_MIN;
 
       d_end = switch_day (_tzi.em, _tzi.ew, _tzi.ed, ywday, month_table);
@@ -94,5 +96,5 @@
         t_end += t_year;
       else
-        t_end = TIME_T_MAX;
+        t_end = t_year >= 0 ? TIME_T_MAX : TIME_T_MIN;
 
       if (d_start < d_end || (d_start == d_end && _tzi.st <= _tzi.et))
@@ -102,6 +104,6 @@
           if (i == 0)
             {
-              _dstsw[i].time = 0;
-              _dstsw[i].shift = 0;
+              _dstsw[0].time = TIME_T_MIN;
+              _dstsw[0].shift = 0;
               ++i;
             }
@@ -119,6 +121,6 @@
           if (i == 0)
             {
-              _dstsw[i].time = 0;
-              _dstsw[i].shift = _tzi.shift;
+              _dstsw[0].time = TIME_T_MIN;
+              _dstsw[0].shift = _tzi.shift;
               ++i;
             }
@@ -136,5 +138,5 @@
   _dstsw_count = i;
 
-  assert (_dstsw_count == sizeof (_dstsw) / sizeof (_dstsw[0]));
+  assert (_dstsw_count == sizeof (_dstsw) / sizeof (_dstsw[0]) - 4);
 }
 
Index: trunk/src/emx/src/lib/time/localtim.c
===================================================================
--- trunk/src/emx/src/lib/time/localtim.c	(revision 902)
+++ trunk/src/emx/src/lib/time/localtim.c	(revision 903)
@@ -6,22 +6,21 @@
 #include <emx/time.h>
 
-struct tm *_STD(localtime) (const time_t *t)
+struct tm *_STD(localtime)(const time_t *t)
 {
-  struct _thread *tp = _thread ();
-  return _localtime (&tp->_th_gmtime_buf, t);
+    struct _thread *tp = _thread();
+    return localtime_r(t, &tp->_th_gmtime_buf);
 }
 
-/* mkstd.awk: NOUNDERSCORE(localtime) */
-struct tm *_localtime (struct tm *dst, const time_t *t)
+struct tm *_STD(localtime_r)(const time_t *t, struct tm *dst)
 {
-  time_t lt;
-  int isdst;
-  struct tm *p;
+    time_t lt;
+    int isdst;
+    struct tm *p;
 
-  if (!_tzset_flag) tzset ();
-  lt = *t;
-  isdst = _gmt2loc (&lt);
-  p = _gmtime (dst, &lt);
-  p->tm_isdst = isdst;
-  return p;
+    if (!_tzset_flag) tzset();
+    lt = *t;
+    isdst = _gmt2loc(&lt);
+    p = gmtime_r(&lt, dst);
+    p->tm_isdst = isdst;
+    return p;
 }
Index: trunk/src/emx/src/lib/time/mktime.c
===================================================================
--- trunk/src/emx/src/lib/time/mktime.c	(revision 902)
+++ trunk/src/emx/src/lib/time/mktime.c	(revision 903)
@@ -43,5 +43,5 @@
     return (time_t)-1;
 
-  if (_gmtime (&tmp, &t2) == NULL)
+  if (gmtime_r (&t2, &tmp) == NULL)
     return (time_t)-1;
   *t = tmp;
Index: trunk/src/emx/src/lib/time/strptime.c
===================================================================
--- trunk/src/emx/src/lib/time/strptime.c	(revision 902)
+++ trunk/src/emx/src/lib/time/strptime.c	(revision 903)
@@ -8,5 +8,5 @@
     Parse a time specification in the input string pointed to by buffer
     according to the format string pointed to by format, updating the
-    structure pointed to by t. 
+    structure pointed to by t.
 
     Whitespace (any number of spaces) in the format string matches whitespace
@@ -353,5 +353,5 @@
     {
       /* Compute day of the year given week number and weekday */
-      int dow = (4 + _year_day [tm->tm_year - 70]) % 7;
+      int dow = (4 + _year_day [tm->tm_year]) % 7;
       if (mask & MASK_WEEKM)
         dow--;
@@ -378,5 +378,5 @@
     else
     {
-      int absday = _year_day [tm->tm_year - 70] + tm->tm_yday;
+      int absday = _year_day [tm->tm_year] + tm->tm_yday;
       /* 1st January 1970 was Thursday (4) */
       tm->tm_wday = ((4 + absday) % 7);
Index: trunk/src/emx/src/lib/time/time.smak
===================================================================
--- trunk/src/emx/src/lib/time/time.smak	(revision 902)
+++ trunk/src/emx/src/lib/time/time.smak	(revision 903)
@@ -1,1 +1,1 @@
-libc.TSRC += $(wildcard src/lib/time/*.c)
+libc.TSRC += $(filter-out %_gen.c, $(wildcard src/lib/time/*.c))
Index: trunk/src/emx/src/lib/time/timetabs.c
===================================================================
--- trunk/src/emx/src/lib/time/timetabs.c	(revision 902)
+++ trunk/src/emx/src/lib/time/timetabs.c	(revision 903)
@@ -4,24 +4,25 @@
 #include <emx/time.h>
 
-/* Day number, relative to 01-Jan-1970, of 01-Jan for the years 1970
-   through 2106 */
-
-unsigned short const _year_day[_YEARS+1] =
+/* Day number, relative to 01-Jan-1970, of 01-Jan for the years 1900
+   through 2059 */
+signed short const _year_day[_YEARS+1] =
 {
-  0, 365, 730, 1096, 1461, 1826, 2191, 2557, 2922, 3287, 3652, 4018,
-  4383, 4748, 5113, 5479, 5844, 6209, 6574, 6940, 7305, 7670, 8035,
-  8401, 8766, 9131, 9496, 9862, 10227, 10592, 10957, 11323, 11688,
-  12053, 12418, 12784, 13149, 13514, 13879, 14245, 14610, 14975,
-  15340, 15706, 16071, 16436, 16801, 17167, 17532, 17897, 18262,
-  18628, 18993, 19358, 19723, 20089, 20454, 20819, 21184, 21550,
-  21915, 22280, 22645, 23011, 23376, 23741, 24106, 24472, 24837,
-  25202, 25567, 25933, 26298, 26663, 27028, 27394, 27759, 28124,
-  28489, 28855, 29220, 29585, 29950, 30316, 30681, 31046, 31411,
-  31777, 32142, 32507, 32872, 33238, 33603, 33968, 34333, 34699,
-  35064, 35429, 35794, 36160, 36525, 36890, 37255, 37621, 37986,
-  38351, 38716, 39082, 39447, 39812, 40177, 40543, 40908, 41273,
-  41638, 42004, 42369, 42734, 43099, 43465, 43830, 44195, 44560,
-  44926, 45291, 45656, 46021, 46387, 46752, 47117, 47482, 47847,
-  48212, 48577, 48942, 49308, 49673, USHRT_MAX
+   -25567,-25202,-24837,-24472,-24107,-23741,-23376,-23011,-22646,-22280,  /* 1900 - 1909 */
+   -21915,-21550,-21185,-20819,-20454,-20089,-19724,-19358,-18993,-18628,  /* 1910 - 1919 */
+   -18263,-17897,-17532,-17167,-16802,-16436,-16071,-15706,-15341,-14975,  /* 1920 - 1929 */
+   -14610,-14245,-13880,-13514,-13149,-12784,-12419,-12053,-11688,-11323,  /* 1930 - 1939 */
+   -10958,-10592,-10227, -9862, -9497, -9131, -8766, -8401, -8036, -7670,  /* 1940 - 1949 */
+    -7305, -6940, -6575, -6209, -5844, -5479, -5114, -4748, -4383, -4018,  /* 1950 - 1959 */
+    -3653, -3287, -2922, -2557, -2192, -1826, -1461, -1096,  -731,  -365,  /* 1960 - 1969 */
+        0,   365,   730,  1096,  1461,  1826,  2191,  2557,  2922,  3287,  /* 1970 - 1979 */
+     3652,  4018,  4383,  4748,  5113,  5479,  5844,  6209,  6574,  6940,  /* 1980 - 1989 */
+     7305,  7670,  8035,  8401,  8766,  9131,  9496,  9862, 10227, 10592,  /* 1990 - 1999 */
+    10957, 11323, 11688, 12053, 12418, 12784, 13149, 13514, 13879, 14245,  /* 2000 - 2009 */
+    14610, 14975, 15340, 15706, 16071, 16436, 16801, 17167, 17532, 17897,  /* 2010 - 2019 */
+    18262, 18628, 18993, 19358, 19723, 20089, 20454, 20819, 21184, 21550,  /* 2020 - 2029 */
+    21915, 22280, 22645, 23011, 23376, 23741, 24106, 24472, 24837, 25202,  /* 2030 - 2039 */
+    25567, 25933, 26298, 26663, 27028, 27394, 27759, 28124, 28489, 28855,  /* 2040 - 2049 */
+    29220, 29585, 29950, 30316, 30681, 31046, 31411, 31777, 32142, 32507,  /* 2050 - 2059 */
+    SHRT_MAX
 };
 
@@ -30,7 +31,7 @@
    December. */
 
-unsigned short const _month_day_non_leap[] = 
+unsigned short const _month_day_non_leap[] =
   {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, USHRT_MAX};
 
-unsigned short const _month_day_leap[] = 
+unsigned short const _month_day_leap[] =
   {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, USHRT_MAX};
Index: trunk/src/emx/src/lib/time/timetabs_gen.c
===================================================================
--- trunk/src/emx/src/lib/time/timetabs_gen.c	(revision 903)
+++ trunk/src/emx/src/lib/time/timetabs_gen.c	(revision 903)
@@ -0,0 +1,72 @@
+/* $Id$ */
+/** @file
+ *
+ * Generate year table.
+ *
+ * Copyright (c) 2003 knut st. osmundsen <bird-srcspam@anduin.net>
+ *
+ *
+ * This file is part of Innotek LIBC.
+ *
+ * Innotek LIBC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Innotek LIBC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Innotek LIBC; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#include <stdio.h>
+#include <emx/time.h>
+
+int main()
+{
+    int i;
+    int iDays;
+    int aiDays[_YEARS];
+
+    /*
+     * Calc table.
+     */
+    for (i = 1970, iDays = 0; i >= 1900;)
+    {
+        aiDays[i - 1900] = iDays;
+        /* prev */
+        i--;
+        iDays -= 365;
+        if ((i % 4) == 0 && ((i % 100) || !(i % 1000)))
+            iDays--;
+    }
+    for (i = 1970, iDays = 0; i < (_YEARS + 1900); i++)
+    {
+        aiDays[i - 1900] = iDays;
+        /* next */
+        iDays += 365;
+        if ((i % 4) == 0 && ((i % 100) || !(i % 1000)))
+            iDays++;
+    }
+
+    /*
+     * Array
+     */
+    for (i = 1900; i < (_YEARS + 1900); i++)
+    {
+        iDays = aiDays[i - 1900];
+        if (!(i % 10))
+            printf("   %6d,", iDays);
+        else
+            printf("%6d,", iDays);
+        if (!((i + 1) % 10))
+            printf("  /* %d - %d */\n", i - 9, i);
+    }
+
+    return 0;
+}
Index: trunk/src/emx/src/lib/time/tzset.c
===================================================================
--- trunk/src/emx/src/lib/time/tzset.c	(revision 902)
+++ trunk/src/emx/src/lib/time/tzset.c	(revision 903)
@@ -12,7 +12,7 @@
 int _STD(daylight) = 0;
 long _STD(timezone) = 0;
-char *_STD(tzname)[2] = {"GMT", ""};
-
-struct _tzinfo _tzi = {"GMT", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+char *_STD(tzname)[2] = {"UCT", ""};
+
+struct _tzinfo _tzi = {"UCT", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 
 
@@ -184,5 +184,5 @@
   p = getenv ("TZ");
   if (p == NULL || *p == 0)
-    p = "GMT";                  /* Our best approximation :-) */
+    p = "UCT";                  /* Our best approximation :-) */
 
   if (!copy_tzname (ntz.tzname, &p))
@@ -227,5 +227,9 @@
           return;
         }
-    }
+      _STD(daylight) = 1;
+    }
+  else
+    _STD(daylight) = 0;
+
 
   /* TODO: Make this thread-safe! */
@@ -237,5 +241,5 @@
   __ftime (&tb);
   t_loc = tb.time;
-  _STD(daylight) = _loc2gmt (&tb.time, -1);
+/*  _STD(daylight) = _loc2gmt (&tb.time, -1); */
   _STD(timezone) = _tzi.tz;
 
