Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Datetime to Unix timestamp in Fortran

🎈 🎈 🎈
+1

    program DateToUnixTimestamp
        implicit none
        integer :: year, month, day, hour, minute, second
        integer(8) :: unix_timestamp
        integer(8) :: days_since_epoch, seconds_since_midnight
        integer, parameter :: epoch_start = 719163  ! Julian day number for 1970-01-01

        ! Example date and time
        year = 2024
        month = 8
        day = 28
        hour = 12
        minute = 34
        second = 56

        ! Calculate the number of days since the Unix epoch
        days_since_epoch = julday(year, month, day) - epoch_start

        ! Calculate the number of seconds since midnight
        seconds_since_midnight = hour * 3600 + minute * 60 + second

        ! Calculate the Unix timestamp
        unix_timestamp = days_since_epoch * 86400 + seconds_since_midnight

        print *, "Unix Timestamp: ", unix_timestamp
    end program DateToUnixTimestamp

    ! Function to calculate Julian day number for a given Gregorian date
    integer function julday(year, month, day)
        implicit none
        integer, intent(in) :: year, month, day
        integer :: a, y, m

        a = (14 - month) / 12
        y = year + 4800 - a
        m = month + 12 * a - 3  

        julday = day + ((153 * m + 2) / 5) + 365 * y + (y / 4) - (y / 100) + (y / 400) - 32045
    end function julday        
        

Output:
Unix Timestamp: 1724848496
Example only. There may be multiple ways to perform this operation.

Code Explanation

We can convert a specified date and time to a Unix timestamp using Fortran by first calculating the Julian day number for the given date using the julday function. We then compute the difference in days between the given date and the Unix epoch start date (January 1, 1970), convert this difference into seconds, and add the number of seconds corresponding to the specified time of day.
The result is the Unix timestamp, which represents the total number of seconds that have passed since the Unix epoch.


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