TimeStringConversion.F90 Source File


This file depends on

sourcefile~~timestringconversion.f90~~EfferentGraph sourcefile~timestringconversion.f90 TimeStringConversion.F90 sourcefile~keywordenforcer.f90 KeywordEnforcer.F90 sourcefile~timestringconversion.f90->sourcefile~keywordenforcer.f90 sourcefile~mapl_exceptionhandling.f90 MAPL_ExceptionHandling.F90 sourcefile~timestringconversion.f90->sourcefile~mapl_exceptionhandling.f90 sourcefile~errorhandling.f90 ErrorHandling.F90 sourcefile~mapl_exceptionhandling.f90->sourcefile~errorhandling.f90 sourcefile~mapl_throw.f90 MAPL_Throw.F90 sourcefile~mapl_exceptionhandling.f90->sourcefile~mapl_throw.f90 sourcefile~errorhandling.f90->sourcefile~mapl_throw.f90

Files dependent on this one

sourcefile~~timestringconversion.f90~~AfferentGraph sourcefile~timestringconversion.f90 TimeStringConversion.F90 sourcefile~cap.f90 Cap.F90 sourcefile~cap.f90->sourcefile~timestringconversion.f90 sourcefile~extdataabstractfilehandler.f90 ExtDataAbstractFileHandler.F90 sourcefile~extdataabstractfilehandler.f90->sourcefile~timestringconversion.f90 sourcefile~extdatafilestream.f90 ExtDataFileStream.F90 sourcefile~extdataabstractfilehandler.f90->sourcefile~extdatafilestream.f90 sourcefile~extdataclimfilehandler.f90 ExtDataClimFileHandler.F90 sourcefile~extdataclimfilehandler.f90->sourcefile~timestringconversion.f90 sourcefile~extdataclimfilehandler.f90->sourcefile~extdataabstractfilehandler.f90 sourcefile~extdataconfig.f90 ExtDataConfig.F90 sourcefile~extdataconfig.f90->sourcefile~timestringconversion.f90 sourcefile~extdataconfig.f90->sourcefile~extdatafilestream.f90 sourcefile~extdatarule.f90 ExtDataRule.F90 sourcefile~extdataconfig.f90->sourcefile~extdatarule.f90 sourcefile~extdatasample.f90 ExtDataSample.F90 sourcefile~extdataconfig.f90->sourcefile~extdatasample.f90 sourcefile~extdatafilestream.f90->sourcefile~timestringconversion.f90 sourcefile~extdatarule.f90->sourcefile~timestringconversion.f90 sourcefile~extdatarule.f90->sourcefile~extdatasample.f90 sourcefile~extdatasample.f90->sourcefile~timestringconversion.f90 sourcefile~extdatasimplefilehandler.f90 ExtDataSimpleFileHandler.F90 sourcefile~extdatasimplefilehandler.f90->sourcefile~timestringconversion.f90 sourcefile~extdatasimplefilehandler.f90->sourcefile~extdataabstractfilehandler.f90 sourcefile~extdataupdatepointer.f90 ExtDataUpdatePointer.F90 sourcefile~extdataupdatepointer.f90->sourcefile~timestringconversion.f90 sourcefile~historycollectiongridcomp_private.f90 HistoryCollectionGridComp_private.F90 sourcefile~historycollectiongridcomp_private.f90->sourcefile~timestringconversion.f90 sourcefile~test_timestringconversion.pf test_TimeStringConversion.pf sourcefile~test_timestringconversion.pf->sourcefile~timestringconversion.f90 sourcefile~extdatagridcompng.f90 ExtDataGridCompNG.F90 sourcefile~extdatagridcompng.f90->sourcefile~extdataconfig.f90 sourcefile~extdataoldtypescreator.f90 ExtDataOldTypesCreator.F90 sourcefile~extdatagridcompng.f90->sourcefile~extdataoldtypescreator.f90 sourcefile~extdatatypedef.f90 ExtDataTypeDef.F90 sourcefile~extdatagridcompng.f90->sourcefile~extdatatypedef.f90 sourcefile~extdataderivedexportvector.f90 ExtDataDerivedExportVector.F90 sourcefile~extdatagridcompng.f90->sourcefile~extdataderivedexportvector.f90 sourcefile~extdataprimaryexportvector.f90 ExtDataPrimaryExportVector.F90 sourcefile~extdatagridcompng.f90->sourcefile~extdataprimaryexportvector.f90 sourcefile~extdataoldtypescreator.f90->sourcefile~extdataabstractfilehandler.f90 sourcefile~extdataoldtypescreator.f90->sourcefile~extdataclimfilehandler.f90 sourcefile~extdataoldtypescreator.f90->sourcefile~extdataconfig.f90 sourcefile~extdataoldtypescreator.f90->sourcefile~extdatafilestream.f90 sourcefile~extdataoldtypescreator.f90->sourcefile~extdatarule.f90 sourcefile~extdataoldtypescreator.f90->sourcefile~extdatasample.f90 sourcefile~extdataoldtypescreator.f90->sourcefile~extdatasimplefilehandler.f90 sourcefile~extdataoldtypescreator.f90->sourcefile~extdatatypedef.f90 sourcefile~extdatatypedef.f90->sourcefile~extdataabstractfilehandler.f90 sourcefile~extdatatypedef.f90->sourcefile~extdataupdatepointer.f90 sourcefile~historycollectiongridcomp.f90 HistoryCollectionGridComp.F90 sourcefile~historycollectiongridcomp.f90->sourcefile~historycollectiongridcomp_private.f90 sourcefile~mapl3g.f90 mapl3g.F90 sourcefile~mapl3g.f90->sourcefile~cap.f90 sourcefile~test_historycollectiongridcomp.pf Test_HistoryCollectionGridComp.pf sourcefile~test_historycollectiongridcomp.pf->sourcefile~historycollectiongridcomp_private.f90 sourcefile~extdataderivedexportvector.f90->sourcefile~extdatatypedef.f90 sourcefile~extdatadrivergridcomp.f90 ExtDataDriverGridComp.F90 sourcefile~extdatadrivergridcomp.f90->sourcefile~extdatagridcompng.f90 sourcefile~extdataprimaryexportvector.f90->sourcefile~extdatatypedef.f90 sourcefile~geos.f90 GEOS.F90 sourcefile~geos.f90->sourcefile~mapl3g.f90 sourcefile~historygridcomp.f90 HistoryGridComp.F90 sourcefile~historygridcomp.f90->sourcefile~historycollectiongridcomp.f90 sourcefile~mapl_capgridcomp.f90 MAPL_CapGridComp.F90 sourcefile~mapl_capgridcomp.f90->sourcefile~extdatagridcompng.f90 sourcefile~comp_testing_driver.f90 Comp_Testing_Driver.F90 sourcefile~comp_testing_driver.f90->sourcefile~mapl_capgridcomp.f90 sourcefile~extdatadriver.f90 ExtDataDriver.F90 sourcefile~extdatadriver.f90->sourcefile~extdatadrivergridcomp.f90 sourcefile~extdatadrivermod.f90 ExtDataDriverMod.F90 sourcefile~extdatadrivermod.f90->sourcefile~extdatadrivergridcomp.f90 sourcefile~mapl_cap.f90 MAPL_Cap.F90 sourcefile~mapl_cap.f90->sourcefile~mapl_capgridcomp.f90

Source Code

#include "MAPL_Exceptions.h"
#include "MAPL_ErrLog.h"
module MAPL_TimeStringConversion
   use ESMF
   use MAPL_KeywordEnforcerMod
   use MAPL_ExceptionHandling
   implicit none
   private

   public :: string_to_integer_time
   public :: string_to_integer_date
   public :: string_to_esmf_time
   public :: string_to_esmf_timeinterval
   public :: hconfig_to_esmf_timeinterval

contains

   function string_to_integer_time(time_string,unusable,rc) result(time)
      character(len=*), intent(in) :: time_string
      class(KeywordEnforcer), optional, intent(in) :: unusable
      integer, optional, intent(out) :: rc

      integer :: time
      integer mpos(2), hpos(2), spos(2)
      integer strlen
      integer firstcolon, lastcolon
      integer lastspace
      integer hour,min,sec

      _UNUSED_DUMMY(unusable)

      strlen = LEN_TRIM (time_string)

      firstcolon = index(time_string, ':')
      if (firstcolon .LE. 0) then

        ! If no colons, check for hour.

        ! Logic below assumes a null character or something else is after the hour
        ! if we do not find a null character add one so that it correctly parses time
        !if (time_string(strlen:strlen) /= char(0)) then
           !time_string = trim(time_string)//char(0)
           !strlen=len_trim(time_string)
        !endif
        lastspace = index(TRIM(time_string), ' ', BACK=.TRUE.)
        if ((strlen-lastspace).eq.2 .or. (strlen-lastspace).eq.3) then
          hpos(1) = lastspace+1
          hpos(2) = strlen-1
          read (time_string(hpos(1):hpos(2)), * ) hour
          min  = 0
          sec  = 0
        else
          hour = 0
          min  = 0
          sec  = 0
        endif

      else
        hpos(1) = firstcolon - 2
        hpos(2) = firstcolon - 1
        lastcolon =  index(time_string, ':', BACK=.TRUE.)
        if ( lastcolon .EQ. firstcolon ) then
          mpos(1) = firstcolon + 1
          mpos(2) = firstcolon + 2
          read (time_string(hpos(1):hpos(2)), * ) hour
          read (time_string(mpos(1):mpos(2)), * ) min
          sec = 0
        else
          mpos(1) = firstcolon + 1
          mpos(2) = lastcolon - 1
          spos(1) = lastcolon + 1
          spos(2) = lastcolon + 2
          read (time_string(hpos(1):hpos(2)), * ) hour
          read (time_string(mpos(1):mpos(2)), * ) min
          read (time_string(spos(1):spos(2)), * ) sec
        endif
      endif

      time = hour*10000+min*100+sec 
      _RETURN(_SUCCESS)

   end function string_to_integer_time

   function string_to_integer_date(time_string,unusable,rc) result(date)
      character(len=*), intent(in) :: time_string
      class(KeywordEnforcer), optional, intent(in) :: unusable
      integer, optional, intent(out) :: rc

      integer :: date
      integer ypos(2), mpos(2), dpos(2)
      integer strlen
      integer firstdash, lastdash
      integer year,month,day

      _UNUSED_DUMMY(unusable)

      strlen = LEN_TRIM (time_string)

      firstdash = index(time_string, '-')
      lastdash  = index(time_string, '-', BACK=.TRUE.)

      if (firstdash .LE. 0 .OR. lastdash .LE. 0) then
        _RETURN(_FAILURE)
      endif

      ypos(2) = firstdash - 1
      mpos(1) = firstdash + 1
      ypos(1) = ypos(2) - 3

      mpos(2) = lastdash - 1
      dpos(1) = lastdash + 1
      dpos(2) = dpos(1) + 1

      read ( time_string(ypos(1):ypos(2)), * ) year
      read ( time_string(mpos(1):mpos(2)), * ) month
      read ( time_string(dpos(1):dpos(2)), * ) day

      date = year*10000+month*100+day
      _RETURN(_SUCCESS)

   end function string_to_integer_date

   function string_to_esmf_time(input_string,unusable,rc) result(time)
      character(len=*), intent(in) :: input_string
      class(KeywordEnforcer), optional, intent(in) :: unusable
      integer, optional, intent(out) :: rc

      type(ESMF_Time) :: time
      integer :: status
      integer :: tpos
      integer year,month,day,hour,min,sec
      integer :: int_time, int_date
      character(len=:), allocatable :: date_string,time_string
      logical :: have_time

      _UNUSED_DUMMY(unusable)

      tpos = index(input_string,'T')
      if (tpos<=0) then
         have_time = .false.
      else
         have_time = .true.
      end if
    
      if (have_time) then
         time_string = input_string(tpos+1:)
         date_string = input_string(:tpos-1)
         int_time = string_to_integer_time(time_string,_RC)
      else
         date_string = trim(input_string) 
         int_time = 0
      end if
      int_date = string_to_integer_date(date_string,_RC)

      year=int_date/10000
      month=mod(int_date/100,100)
      day=mod(int_date,100)
      hour=int_time/10000
      min=mod(int_time/100,100)
      sec=mod(int_time,100)
      call ESMF_TimeSet(time,yy=year,mm=month,dd=day,h=hour,m=min,s=sec,_RC)
      _RETURN(_SUCCESS)

   end function string_to_esmf_time

   function string_to_esmf_timeinterval(time_interval_string,unusable,rc) result(time_interval)
      character(len=*), intent(in) :: time_interval_string
      class(KeywordEnforcer), optional, intent(in) :: unusable
      integer, optional, intent(out) :: rc

      type(ESMF_TimeInterval) :: time_interval
      integer :: status

      integer :: strlen,ppos,cpos,lpos,tpos
      integer year,month,day,hour,min,sec
      character(len=:), allocatable :: date_string,time_string
      _UNUSED_DUMMY(unusable)

      year=0
      month=0
      day=0
      hour=0
      min=0
      sec=0
      strlen = len_trim(time_interval_string)
      tpos = index(time_interval_string,'T')
      ppos = index(time_interval_string,'P')
      _ASSERT(time_interval_string(1:1) == 'P','Not valid time duration')

      if (tpos /= 0) then
         if (tpos /= ppos+1) then
            date_string = time_interval_string(ppos+1:tpos-1)
         end if
         time_string = time_interval_string(tpos+1:strlen)
      else
         date_string = time_interval_string(ppos+1:strlen)
      end if

      if (allocated(date_string)) then
         strlen = len_trim(date_string)
         lpos = 0
         cpos = index(date_string,'Y')
         if (cpos /= 0) then
            read(date_string(lpos+1:cpos-1),*)year
            lpos = cpos
         end if
         cpos = index(date_string,'M')
         if (cpos /= 0) then
            read(date_string(lpos+1:cpos-1),*)month
            lpos = cpos
         end if
         cpos = index(date_string,'D')
         if (cpos /= 0) then
            read(date_string(lpos+1:cpos-1),*)day
            lpos = cpos
         end if
      end if
      if (allocated(time_string)) then
         strlen = len_trim(time_string)
         lpos = 0
         cpos = index(time_string,'H')
         if (cpos /= 0) then
            read(time_string(lpos+1:cpos-1),*)hour
            lpos = cpos
         end if
         cpos = index(time_string,'M')
         if (cpos /= 0) then
            read(time_string(lpos+1:cpos-1),*)min
            lpos = cpos
         end if
         cpos = index(time_string,'S')
         if (cpos /= 0) then
            read(time_string(lpos+1:cpos-1),*)sec
            lpos = cpos
         end if
      end if

      call ESMF_TimeIntervalSet(time_interval,yy=year,mm=month,d=day,h=hour,m=min,s=sec,_RC)
      _RETURN(_SUCCESS)

   end function string_to_esmf_timeinterval

   function hconfig_to_esmf_timeinterval(hconfig, key, unusable, rc) result(time_interval)
      type(ESMF_TimeInterval) :: time_interval
      type(ESMF_HConfig), intent(in) :: hconfig
      character(len=*), intent(in) :: key
      class(KeywordEnforcer), optional, intent(in) :: unusable
      integer, optional, intent(out) :: rc

      integer :: status
      character(len=:), allocatable :: iso_duration

      _UNUSED_DUMMY(unusable)

      iso_duration = ESMF_HConfigAsString(hconfig, keystring=key, _RC)
      time_interval = string_to_esmf_timeinterval(iso_duration, _RC)

      _RETURN(_SUCCESS)
   end function hconfig_to_esmf_timeinterval

end module MAPL_TimeStringConversion