date_to_datestr Subroutine

public subroutine date_to_datestr(date_array, format_str, date_str)

Returns a date string based on (yyyy.mm.dd HH:MM:SS) from a date array

Currently only accepts 'yyyymmddHH', 'yyymmdd' and 'yyyymm' as the format string.

Example: call date_to_datestr([2019, 4, 29, 30, 0, 0], "yyyymmdd", date_str) Returns: date_str = "20190429"

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: date_array(6)

Datetime [y,m,d,h,m,s]

character(len=*), intent(in) :: format_str

Format string

character(len=*), intent(out) :: date_str

Date string


Called by

proc~~date_to_datestr~~CalledByGraph proc~date_to_datestr date_to_datestr proc~uemep_calculate_emissions_for_emep uEMEP_calculate_emissions_for_EMEP proc~uemep_calculate_emissions_for_emep->proc~date_to_datestr proc~uemep_read_config uEMEP_read_config proc~uemep_read_config->proc~date_to_datestr program~uemep uEMEP program~uemep->proc~uemep_calculate_emissions_for_emep program~uemep->proc~uemep_read_config

Source Code

    subroutine date_to_datestr(date_array, format_str, date_str)
        !! Returns a date string based on (yyyy.mm.dd HH:MM:SS) from a date array
        !!
        !! Currently only accepts 'yyyymmddHH', 'yyymmdd' and 'yyyymm' as the format string.
        !!
        !! Example:
        !!     call date_to_datestr([2019, 4, 29, 30, 0, 0], "yyyymmdd", date_str)
        !! Returns:
        !!     date_str = "20190429"
        integer, intent(in) :: date_array(6) !! Datetime [y,m,d,h,m,s]
        character(len=*), intent(in) :: format_str !! Format string
        character(len=*), intent(out) :: date_str !! Date string

        ! Local variables
        integer :: pos

        date_str = format_str

        ! To avoid just putting in date parts e.g. mm or dd that might occurr in a string then it is required that at least two of the date
        ! strings are present, i.e. yyyy, mm and dd or HH, MM and SS
        if ((index(format_str, 'yyyy') .gt. 0 .and. index(format_str, 'mm') .gt. 0) .or. (index(format_str, 'yyyy') .gt. 0 &
            .and. index(format_str, 'dd') .gt. 0) .or. (index(format_str, 'dd') .gt. 0 .and.index(format_str, 'mm').gt.0) &
            .or. (index(format_str, 'HH') .gt. 0 .and. index(format_str,'MM') .gt. 0) .or. (index(format_str,'HH') .gt. 0 &
            .and. index(format_str,'SS') .gt. 0) .or. (index(format_str,'MM') .gt. 0 .and. index(format_str,'SS') .gt. 0)) then
            
            ! Do nothing but continue with routine as this is a valid format for date string substitution
        else
            ! Leave the routine
            return
        end if

        ! Now it only accepts the strings 'yyyymmddHH', 'yyyymmdd' and 'yyyymm' for replacement
        pos = index(format_str, 'yyyymmddHH')
        if (pos .gt. 0) then
            write(date_str(pos:pos+3),'(i4)') date_array(1)
            if (date_array(2) .gt. 9) then
                write(date_str(pos+4:pos+5),'(i2)') date_array(2)
            else
                write(date_str(pos+4:pos+5),'(a1,i1)') '0', date_array(2)
            end if
            if (date_array(3) .gt. 9) then
                write(date_str(pos+6:pos+7),'(i2)') date_array(3)
            else
                write(date_str(pos+6:pos+7),'(a1,i1)') '0', date_array(3)
            end if
            if (date_array(4) .gt. 9) then
                write(date_str(pos+8:pos+9),'(i2)') date_array(4)
            else
                write(date_str(pos+8:pos+9),'(a1,i1)') '0', date_array(4)
            end if
            return
        end if

        pos = index(format_str, 'yyyymmdd')
        if (pos .gt. 0) then
            write(date_str(pos:pos+3),'(i4)') date_array(1)
            if (date_array(2) .gt. 9) then
                write(date_str(pos+4:pos+5),'(i2)') date_array(2)
            else
                write(date_str(pos+4:pos+5),'(a1,i1)') '0', date_array(2)
            end if
            if (date_array(3) .gt. 9) then
                write(date_str(pos+6:pos+7),'(i2)') date_array(3)
            else
                write(date_str(pos+6:pos+7),'(a1,i1)') '0', date_array(3)
            end if
            return
        end if

        pos = index(format_str, 'yyyymm')
        if (pos .gt. 0) then
            write(date_str(pos:pos+3),'(i4)') date_array(1)
            if (date_array(2) .gt. 9) then
                write(date_str(pos+4:pos+5),'(i2)') date_array(2)
            else
                write(date_str(pos+4:pos+5),'(a1,i1)') '0', date_array(2)
            end if
            return
        end if
    end subroutine date_to_datestr