format_datetime Function

public pure function format_datetime(dt) result(str)

Format a datetime_type as an ISO 8601 string.

Arguments

Type IntentOptional Attributes Name
type(datetime_type), intent(in) :: dt

Return Value character(len=:), allocatable


Source Code

    pure function format_datetime(dt) result(str)
        !! version: experimental
        !!
        !! Format a datetime_type as an ISO 8601 string.
        type(datetime_type), intent(in) :: dt
        character(:), allocatable :: str
        integer :: off_h, off_m

        str = to_string(dt%year, '(I4.4)') // '-' // &
              to_string(dt%month, '(I2.2)') // '-' // &
              to_string(dt%day, '(I2.2)') // 'T' // &
              to_string(dt%hour, '(I2.2)') // ':' // &
              to_string(dt%minute, '(I2.2)') // ':' // &
              to_string(dt%second, '(I2.2)')

        if (dt%millisecond /= 0) then
            str = str // '.' // to_string(dt%millisecond, '(I3.3)')
        end if

        if (dt%utc_offset_minutes == 0) then
            str = str // 'Z'
        else
            off_h = abs(dt%utc_offset_minutes) / 60
            off_m = mod(abs(dt%utc_offset_minutes), 60)
            if (dt%utc_offset_minutes > 0) then
                str = str // '+'
            else
                str = str // '-'
            end if
            str = str // to_string(off_h, '(I2.2)') // ':' // &
                  to_string(off_m, '(I2.2)')
        end if
    end function format_datetime