OSDN Git Service

Backport from mainline
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / date_and_time.c
index 2ee8e5c..3c38636 100644 (file)
@@ -1,5 +1,5 @@
 /* Implementation of the DATE_AND_TIME intrinsic.
-   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010
+   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2011
    Free Software Foundation, Inc.
    Contributed by Steven Bosscher.
 
@@ -29,45 +29,13 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 #include <assert.h>
 #include <stdlib.h>
 
-#undef HAVE_NO_DATE_TIME
-#if TIME_WITH_SYS_TIME
-#  include <sys/time.h>
-#  include <time.h>
-#else
-#  if HAVE_SYS_TIME_H
-#    include <sys/time.h>
-#  else
-#    ifdef HAVE_TIME_H
-#      include <time.h>
-#    else
-#      define HAVE_NO_DATE_TIME
-#    endif  /* HAVE_TIME_H  */
-#  endif  /* HAVE_SYS_TIME_H  */
-#endif  /* TIME_WITH_SYS_TIME  */
-
-#ifndef abs
-#define abs(x) ((x)>=0 ? (x) : -(x))
-#endif
-
-
-/* If the re-entrant versions of localtime and gmtime are not
-   available, provide fallback implementations.  On some targets where
-   the _r versions are not available, localtime and gmtime use
-   thread-local storage so they are threadsafe.  */
+#include "time_1.h"
 
-#ifndef HAVE_LOCALTIME_R
-/* If _POSIX is defined localtime_r gets defined by mingw-w64 headers.  */
-#ifdef localtime_r
-#undef localtime_r
-#endif
 
-static struct tm *
-localtime_r (const time_t * timep, struct tm * result)
-{
-  *result = *localtime (timep);
-  return result;
-}
-#endif
+/* If the re-entrant version of gmtime is not available, provide a
+   fallback implementation.  On some targets where the _r version is
+   not available, gmtime uses thread-local storage so it's
+   threadsafe.  */
 
 #ifndef HAVE_GMTIME_R
 /* If _POSIX is defined gmtime_r gets defined by mingw-w64 headers.  */
@@ -171,33 +139,16 @@ date_and_time (char *__date, char *__time, char *__zone,
   char zone[ZONE_LEN + 1];
   GFC_INTEGER_4 values[VALUES_SIZE];
 
-#ifndef HAVE_NO_DATE_TIME
   time_t lt;
   struct tm local_time;
   struct tm UTC_time;
 
-#if HAVE_GETTIMEOFDAY
-  {
-    struct timeval tp;
-
-    if (!gettimeofday (&tp, NULL))
-      {
-         lt = tp.tv_sec;
-         values[7] = tp.tv_usec / 1000;
-      }
-    else
-      {
-         lt = time (NULL);
-         values[7] = 0;
-      }
-  }
-#else
-  lt = time (NULL);
-  values[7] = 0;
-#endif /* HAVE_GETTIMEOFDAY */
+  long usecs;
 
-  if (lt != (time_t) -1)
+  if (!gf_gettime (&lt, &usecs))
     {
+      values[7] = usecs / 1000;
+
       localtime_r (&lt, &local_time);
       gmtime_r (&lt, &UTC_time);
 
@@ -212,7 +163,6 @@ date_and_time (char *__date, char *__time, char *__zone,
       values[5] = local_time.tm_min;
       values[6] = local_time.tm_sec;
 
-#if HAVE_SNPRINTF
       if (__date)
        snprintf (date, DATE_LEN + 1, "%04d%02d%02d",
                  values[0], values[1], values[2]);
@@ -223,18 +173,6 @@ date_and_time (char *__date, char *__time, char *__zone,
       if (__zone)
        snprintf (zone, ZONE_LEN + 1, "%+03d%02d",
                  values[3] / 60, abs (values[3] % 60));
-#else
-      if (__date)
-       sprintf (date, "%04d%02d%02d", values[0], values[1], values[2]);
-
-      if (__time)
-       sprintf (timec, "%02d%02d%02d.%03d",
-                values[4], values[5], values[6], values[7]);
-
-      if (__zone)
-       sprintf (zone, "%+03d%02d",
-                values[3] / 60, abs (values[3] % 60));
-#endif
     }
   else
     {
@@ -250,21 +188,6 @@ date_and_time (char *__date, char *__time, char *__zone,
       for (i = 0; i < VALUES_SIZE; i++)
        values[i] = - GFC_INTEGER_4_HUGE;
     }   
-#else /* if defined HAVE_NO_DATE_TIME  */
-  /* We really have *nothing* to return, so return blanks and HUGE(0).  */
-      
-  memset (date, ' ', DATE_LEN);
-  date[DATE_LEN] = '\0';
-
-  memset (timec, ' ', TIME_LEN);
-  timec[TIME_LEN] = '\0';
-
-  memset (zone, ' ', ZONE_LEN);
-  zone[ZONE_LEN] = '\0';
-
-  for (i = 0; i < VALUES_SIZE; i++)
-    values[i] = - GFC_INTEGER_4_HUGE;
-#endif  /* HAVE_NO_DATE_TIME  */
 
   /* Copy the values into the arguments.  */
   if (__values)
@@ -349,7 +272,7 @@ secnds (GFC_REAL_4 *x)
   /* Make the INTEGER*4 array for passing to date_and_time.  */
   gfc_array_i4 *avalues = internal_malloc_size (sizeof (gfc_array_i4));
   avalues->data = &values[0];
-  GFC_DESCRIPTOR_DTYPE (avalues) = ((GFC_DTYPE_REAL << GFC_DTYPE_TYPE_SHIFT)
+  GFC_DESCRIPTOR_DTYPE (avalues) = ((BT_REAL << GFC_DTYPE_TYPE_SHIFT)
                                        & GFC_DTYPE_TYPE_MASK) +
                                    (4 << GFC_DTYPE_SIZE_SHIFT);
 
@@ -378,7 +301,6 @@ secnds (GFC_REAL_4 *x)
 static void
 itime0 (int x[3])
 {
-#ifndef HAVE_NO_DATE_TIME
   time_t lt;
   struct tm local_time;
 
@@ -392,9 +314,6 @@ itime0 (int x[3])
       x[1] = local_time.tm_min;
       x[2] = local_time.tm_sec;
     }
-#else
-  x[0] = x[1] = x[2] = -1;
-#endif
 }
 
 extern void itime_i4 (gfc_array_i4 *);
@@ -460,7 +379,6 @@ itime_i8 (gfc_array_i8 *__values)
 static void
 idate0 (int x[3])
 {
-#ifndef HAVE_NO_DATE_TIME
   time_t lt;
   struct tm local_time;
 
@@ -474,9 +392,6 @@ idate0 (int x[3])
       x[1] = 1 + local_time.tm_mon;
       x[2] = 1900 + local_time.tm_year;
     }
-#else
-  x[0] = x[1] = x[2] = -1;
-#endif
 }
 
 extern void idate_i4 (gfc_array_i4 *);