Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Convert Unix Timestamp to Datetime in Fortran

How to convert Unix timestamp to Datetime in Fortran

🎈 🎈 🎈
+1

    program UnixToDate
        implicit none
        integer(8) :: unix_timestamp
        integer :: days, seconds, year, month, day, hour, minute, second
        integer, parameter :: epoch_start = 0 ! January 1, 1970 

        unix_timestamp = 993875349  ! Example Unix timestamp

        days = unix_timestamp / 86400
        seconds = unix_timestamp - days * 86400

        call date_and_time(values=(/year, month, day, hour, minute, second/), julian=epoch_start+days)
        hour = seconds / 3600
        seconds = seconds - hour * 3600
        minute = seconds / 60
        second = seconds - minute * 60

        print *, "Converted Date: ", year, "-", month, "-", day, " ", hour, ":", minute, ":", second
    end program UnixToDate
        

Output:
Converted Date: 2001-06-30 04:29:09
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this Fortran program, the Unix timestamp is first divided into days and the remainder seconds using simple arithmetic.
The Julian date for January 1, 1970, is used as the starting point (Unix epoch_start = 0), and the date_and_time intrinsic function is called to compute the corresponding date for the calculated days.
The remaining seconds are then converted into hours, minutes, and seconds. Finally, the program prints the converted date in a human-readable format.


Other useful Fortran date functions

Function Description Example
DATE_AND_TIME() Returns the current date and time as separate components: year, month, day, hour, minute, second, and millisecond. It can also return the time difference from UTC and the current date in Julian format. CALL DATE_AND_TIME(date=dt) where dt is an array returns 20240828 (date), 123456 (time).
JULIAN_DATE() Calculates the Julian day number for a given Gregorian calendar date. CALL JULIAN_DATE(2024, 8, 28, jd) returns 2460150
GREGORIAN_DATE() Converts a Julian day number to a Gregorian calendar date. CALL GREGORIAN_DATE(jd, year, month, day) with jd = 2460150 returns 2024-08-28
TIME() Returns the current system time as a character string in the format HHMMSS. CALL TIME(time_string) returns 123456 (for 12:34:56 PM)
CPU_TIME() Returns the amount of CPU time used by the program. CALL CPU_TIME(cpu_time) where cpu_time is a real variable returns 0.015625 seconds.
DATE() Returns the current date as a character string in the format DD-MMM-YY. CALL DATE(date_string) returns 28-AUG-24
Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder