Sun Studio 12: Fortran Programming Guide

7.7 Time and Date Functions

Library functions that return the time of day or elapsed CPU time vary from system to system.

The time functions supported in the Fortran library are listed in the following table:

Table 7–3 Fortran Time Functions

Name  

Function  

Man Page  

time

Returns the number of seconds elapsed since January, 1, 1970 

time(3F)

date

Returns date as a character string 

date(3F)

fdate

Returns the current time and date as a character string 

fdate(3F)

idate

Returns the current month, day, and year in an integer array 

idate(3F)

itime

Returns the current hour, minute, and second in an integer array 

itime(3F)

ctime

Converts the time returned by the time function to a character string

ctime(3F)

ltime

Converts the time returned by the time function to the local time

ltime(3F)

gmtime

Converts the time returned by the time function to Greenwich time

gmtime(3F)

etime

Single processor: Returns elapsed user and system time for program execution Multiple processors: Returns the wall clock time

etime(3F)

dtime

Returns the elapsed user and system time since last call to dtime

dtime(3F)

date_and_time

Returns date and time in character and numeric form 

date_and_time(3F)

For details, see Fortran Library Reference Manual or the individual man pages for these functions. Here is a simple example of the use of these time functions (TestTim.f):


      subroutine startclock
      common / myclock / mytime
      integer mytime, time
      mytime = time()
      return
      end
      function wallclock()
      integer wallclock
      common / myclock / mytime
      integer mytime, time, newtime
      newtime = time()
      wallclock = newtime - mytime
      mytime = newtime
      return
      end
      integer wallclock, elapsed
      character*24 greeting
      real dtime, timediff, timearray(2)
c      print a heading
      call fdate( greeting )
      print*,  "      Hello, Time Now Is: ",  greeting
      print*,      "See how long ’sleep 4’ takes, in seconds"
      call startclock
      call system( ’sleep 4’ )
      elapsed = wallclock()
      print*, "Elapsed time for sleep 4 was: ", elapsed," seconds"
c      now test the cpu time for some trivial computing
      timediff = dtime( timearray )
      q = 0.01
      do 30 i = 1, 100000
            q = atan( q )
30      continue
      timediff = dtime( timearray )
      print*, "atan(q) 100000 times took: ", timediff ," seconds"
      end

Running this program produces the following results:


demo% TimeTest
       Hello, Time Now Is: Thu Feb  8 15:33:36 2001
 See how long ’sleep 4’ takes, in seconds
 Elapsed time for sleep 4 was:  4  seconds
 atan(q) 100000 times took:  0.01  seconds
demo%

The routines listed in the following table provide compatibility with VMS Fortran system routines idate and time. To use these routines, you must include the -lV77 option on the f95 command line, in which case you also get these VMS versions instead of the standard f95 versions.

Table 7–4 Summary: Nonstandard VMS Fortran System Routines

Name  

Definition  

Calling Sequence  

Argument Type  

idate

Date as day, month, year 

call idate( d, m, y )

integer

time

Current time as hhmmss

call time( t )

character*8


Note –

The date(3F) routine and the VMS version of idate(3F) cannot be Year 2000 safe because they return 2-digit values for the year. Programs that compute time duration by subtracting dates returned by these routines will compute erroneous results after December 31, 1999. The Fortran 95 routine date_and_time(3F) should be used instead. See the Fortran Library Reference Manual for details.