Fortran Programming Guide

Time and Date Functions

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

The following time functions are not supported directly in the Sun Fortran libraries, but you can write subroutines to duplicate their functions:

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

Table 7-1 Sun 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 stringdate(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. 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 f77 command line, in which case you also get these VMS versions instead of the standard f77 versions.

Table 7-2 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 90 routine date_and_time(3F) is available for both FORTRAN 77 and Fortran 90 programs, and should be used instead. See the Fortran Library Reference Manual for details.


The error condition subroutine errsns is not provided, because it is totally specific to the VMS operating system.

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, 1000 
            q = atan( q ) 
30      continue 
      timediff = dtime( timearray ) 
      print*, "atan(q) 1000 times took: ", timediff ," seconds"
      end 

Running this program produces the following results:


demo% TimeTest
       Hello, Time Now Is: Mon Feb 12 11:53:54 1996
 See how long 'sleep 4' takes, in seconds
 Elapsed time for sleep 4 was:   5 seconds
 atan(q) 1000 times took:     2.26550E-03 seconds
demo%