Time Formatting Parsing in Fortran

Fortran supports time formatting and parsing via built-in functions and modules.

program time_formatting_parsing
    use, intrinsic :: iso_fortran_env
    use, intrinsic :: iso_c_binding
    implicit none

    character(len=30) :: time_string
    integer :: date_time(8)
    integer :: year, month, day, hour, minute, second
    real :: millisecond

    ! Get current time
    call date_and_time(VALUES=date_time)
    year = date_time(1)
    month = date_time(2)
    day = date_time(3)
    hour = date_time(5)
    minute = date_time(6)
    second = date_time(7)
    millisecond = date_time(8) / 1000.0

    ! Format time according to ISO 8601 (similar to RFC3339)
    write(time_string, '(I4.4,"-",I2.2,"-",I2.2,"T",I2.2,":",I2.2,":",I2.2,".",F3.3,"Z")') &
        year, month, day, hour, minute, second, millisecond
    print *, "Formatted time: ", trim(time_string)

    ! Parse a time string
    call parse_time("2012-11-01T22:08:41", year, month, day, hour, minute, second)
    print *, "Parsed time: ", year, month, day, hour, minute, second

    ! Custom time formatting
    write(time_string, '(I2.2,":",I2.2,"PM")') mod(hour, 12), minute
    print *, "Custom format (HH:MMPM): ", trim(time_string)

    write(time_string, '(I4.4,"-",I2.2,"-",I2.2,"T",I2.2,":",I2.2,":",I2.2,".",F3.3,"+00:00")') &
        year, month, day, hour, minute, second, millisecond
    print *, "ISO 8601 with milliseconds: ", trim(time_string)

contains

    subroutine parse_time(time_str, year, month, day, hour, minute, second)
        character(len=*), intent(in) :: time_str
        integer, intent(out) :: year, month, day, hour, minute, second
        read(time_str, '(I4,1X,I2,1X,I2,1X,I2,1X,I2,1X,I2)') year, month, day, hour, minute, second
    end subroutine parse_time

end program time_formatting_parsing

In this Fortran example, we demonstrate time formatting and parsing using built-in functions and custom routines. Here’s a breakdown of the code:

  1. We use the date_and_time intrinsic subroutine to get the current date and time.

  2. We format the time according to ISO 8601 (similar to RFC3339) using Fortran’s formatted write statement.

  3. For time parsing, we create a custom subroutine parse_time that reads a formatted time string and extracts its components.

  4. We demonstrate custom time formatting using Fortran’s formatted write statements.

  5. For purely numeric representations, we use Fortran’s formatted write statements with the extracted components of the time value.

Note that Fortran doesn’t have built-in constants for time formats like RFC3339. Instead, we manually construct the format strings. Also, Fortran’s time handling capabilities are more limited compared to some other languages, so some advanced features may require additional libraries or more complex custom routines.

To run this program, save it as time_formatting_parsing.f90 and compile it using a Fortran compiler:

$ gfortran time_formatting_parsing.f90 -o time_formatting_parsing
$ ./time_formatting_parsing

This will output the formatted and parsed times according to the program.