Saman Moghaddampoor
Saman Moghaddampoor

Reputation: 57

How to get the current time in millisecond in Fortran?

I want to get the current system time in milliseconds in Fortran. I can't use system_clock, because I can't figure out how to get the current time from it.

Upvotes: 1

Views: 5619

Answers (3)

mrankovic
mrankovic

Reputation: 1

You can use ITIME(), a function embedded in Fortran. It returns a real number in units of milliseconds. You just need to call it twice and subtract the two values in order to calculate the time interval.

Upvotes: -1

0bijan mortazavi
0bijan mortazavi

Reputation: 368

I think your question is: "how can I call date_and_time subroutin and access it to calculate ms?" Am I right? Alexander's answer was true.also you can use this code:

  program time
  integer :: values(8)
  call date_and_time(values=values)
  print *, values(5),":",values(6),":",values(7),":",values(8)
  end program time

Upvotes: 2

Alexander Vogt
Alexander Vogt

Reputation: 18098

This illustrates how to get the time since midnight in milliseconds using date_and_time:

program time
  integer :: values(8)
  real    :: rTime

  ! Get the values
  call date_and_time(values=values)

  ! From https://gcc.gnu.org/onlinedocs/gfortran/DATE_005fAND_005fTIME.html
  ! values(5) ... The hour of the day
  ! values(6) ... The minutes of the hour
  ! values(7) ... The seconds of the minute
  ! values(8) ... The milliseconds of the second 

  ! Calculate time since midnight
  rTime = ( values(5) )*60.         ! Hours to minutes
  rTime = ( rTime + values(6) )*60. ! Minutes to seconds
  rTime = ( rTime + values(7) )*1e3 ! Seconds to milliseconds
  rTime = rTime + values(8)         ! Add milliseconds

  ! Time in seconds 
  print *, 'Time (ms) since midnight', rTime
end program

Upvotes: 11

Related Questions