Epoch in Co-array Fortran
A common requirement in programs is getting the number of seconds, milliseconds, or nanoseconds since the Unix epoch. Here’s how to do it in Co-array Fortran.
In this Co-array Fortran program:
We use the
system_clock
subroutine to get the current time. This subroutine returns the number of clock ticks since an arbitrary time in the past.We calculate the number of seconds by dividing the count by the count rate. This gives us the time in seconds since the arbitrary starting point.
We then calculate milliseconds and nanoseconds based on the seconds value.
We print out these values to show the elapsed time since the arbitrary starting point (which is not necessarily the Unix epoch, but serves a similar purpose in this example).
Finally, we demonstrate how to convert the seconds back to the original count value.
To compile and run the program:
Note that the actual output will vary depending on when you run the program and the specifics of your system’s clock.
In Co-array Fortran, we don’t have a built-in way to get the exact Unix epoch time as in some other languages. The system_clock
function provides a similar functionality, giving us a consistent time reference from an arbitrary starting point. For many applications, this relative time measurement is sufficient.
Next, we’ll look at another time-related task: time formatting and parsing, which can be implemented using additional modules or custom subroutines in Fortran.