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

Tobias Burnus burnus@net-b.de
Wed Nov 29 19:55:00 GMT 2006

:ADDPATCH fortran:

As found (and fixed) by Mat Cross:

In libgfortran's date_and_time(...), we currently have:

  lt = time (NULL);
      local_time = *localtime (&lt);
      UTC_time = *gmtime (&lt);
      values[6] = local_time.tm_sec;
      values[7] = 0;
      gettimeofday (&tp)
      values[7] = tp.tv_usec / 1000;

Example for the border case:

First call to date_and_time(...)
     lt = time() // assume time 19.0s
     values[7] = tp.tv_usec / 1000; // assume time 19.4s
gives 19.4s

Second call to date_and_time(...)
     lt = time() // assume time 19.8s
     values[7] = tp.tv_usec / 1000; // assume time 20.1s
gives 19.1s
Thus we produced a time machine!

The suggested fix is use:
   lt = time (NULL);
      gettimeofday (&tp)
      lt = tp.tv_sec
      values[7] = tp.tv_usec / 1000;

The change by Mat Cross is therefore:
- Moving the  gettimeofday() block up in the file
- Using the result of gettimeofday() for lt and value[7], instead of
using time() for lt and gettimeofday() for value[7]
 date_and_time.c |   61
 1 file changed, 36 insertions(+), 25 deletions(-)

As it was mostly moving things around and changing
   value[7] = 0;  // at one place
     lt = tp.tv_sec; // at another place
#   else
      value[7] = 0;
I think it is copyright-wise ok, isn't it?

Ok for the trunk and after a week or so for 4.2 and 4.1 (which are

Build and regression tested on the trunk with x86_64-unknown-linux-gnu.

The PR contains also an example, but I failed to trigger the error.
If you think it makes sense, we can also add the example to our test suite.


2006-11-29  Mat Cross  <mathewc@nag.co.uk>

    PR fortran/30015
    * intrinsics/date_and_time.c (date_and_time): Fix case where time
can go backwards.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: date_and_time.diff
Type: text/x-patch
Size: 2363 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20061129/f9a2dc69/attachment.bin>

More information about the Gcc-patches mailing list