This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [patch,fortran] Fix corner case where consecutive calls to date_and_time can return times out of sequence (PR30015)


Hi Thomas,

Thomas Koenig wrote:
> If I understand the system calls correctly, gettimeofday() returns
> both the seconds and the milliseconds since epoch.  That's all we
> need to fill out the fields of DATE_AND_TIME
>
> If gettimeofday succeeds, why is there a need to call time() at all?
>   

Because I missed this when I copied the patch from the PR. Yesterday,
during a concert I asked myself the same question. (I should go more
often into concerts, especially if they are given for free in the
conservatoire ;-)

Short description of the bug:
- Currently, libgfortran uses  "time()" for the
seconds/minutes/hours/... and gettimeofday for the milliseconds.
- This patch uses gettimeofday for both and removes thus a corner case,
where a second call could return an earlier time than the first one.

Patch based on the one by Mat Cross, see PR.


Regression tested on x86_64-unknown-linux-gnu. Ok for the trunk and
after a week for 4.2 (and 4.1?).


Tobias


2006-12-09  Mat Cross  <mathewc@nag.co.uk>
            Tobias Burnus  <burnus@net-b.de>

	PR fortran/30015
	* intrinsics/date_and_time.c (date_and_time): Fix case where time
	  can go backwards.

Index: libgfortran/intrinsics/date_and_time.c
===================================================================
--- libgfortran/intrinsics/date_and_time.c	(Revision 119704)
+++ libgfortran/intrinsics/date_and_time.c	(Arbeitskopie)
@@ -154,7 +154,43 @@
   struct tm local_time;
   struct tm UTC_time;
 
+#if HAVE_GETTIMEOFDAY
+  {
+    struct timeval tp;
+
+#if GETTIMEOFDAY_ONE_ARGUMENT
+    if (!gettimeofday (&tp))
+#else
+
+#if HAVE_STRUCT_TIMEZONE
+    struct timezone tzp;
+
+    /* Some systems such as HP-UX, do have struct timezone, but
+       gettimeofday takes void* as the 2nd arg.  However, the
+       effect of passing anything other than a null pointer is
+       unspecified on HP-UX.  Configure checks if gettimeofday
+       actually fails with a non-NULL arg and pretends that
+       struct timezone is missing if it does fail.  */
+    if (!gettimeofday (&tp, &tzp))
+#else
+    if (!gettimeofday (&tp, (void *) 0))
+#endif /* HAVE_STRUCT_TIMEZONE  */
+
+#endif /* GETTIMEOFDAY_ONE_ARGUMENT  */
+      {
+         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 */
 
   if (lt != (time_t) -1)
     {
@@ -171,32 +207,7 @@
       values[4] = local_time.tm_hour;
       values[5] = local_time.tm_min;
       values[6] = local_time.tm_sec;
-      values[7] = 0;
 
-#if HAVE_GETTIMEOFDAY
-      {
-	struct timeval tp;
-#  if GETTIMEOFDAY_ONE_ARGUMENT
-	if (!gettimeofday (&tp))
-#  else
-#    if HAVE_STRUCT_TIMEZONE
-	struct timezone tzp;
-
-      /* Some systems such as HP-UX, do have struct timezone, but
-	 gettimeofday takes void* as the 2nd arg.  However, the
-	 effect of passing anything other than a null pointer is
-	 unspecified on HP-UX.  Configure checks if gettimeofday
-	 actually fails with a non-NULL arg and pretends that
-	 struct timezone is missing if it does fail.  */
-	if (!gettimeofday (&tp, &tzp))
-#    else
-	if (!gettimeofday (&tp, (void *) 0))
-#    endif /* HAVE_STRUCT_TIMEZONE  */
-#  endif /* GETTIMEOFDAY_ONE_ARGUMENT  */
-	values[7] = tp.tv_usec / 1000;
-      }
-#endif /* HAVE_GETTIMEOFDAY */
-
 #if HAVE_SNPRINTF
       if (__date)
 	snprintf (date, DATE_LEN + 1, "%04d%02d%02d",

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]