function parse_time_string(timeUnits,rc) result(time)
character(len=*), intent(inout) :: timeUnits
integer, optional, intent(out) :: rc
type(ESMF_Time) :: time
integer :: status
integer year ! 4-digit year
integer month ! month
integer day ! day
integer hour ! hour
integer min ! minute
integer sec ! second
integer ypos(2), mpos(2), dpos(2), hpos(2), spos(2)
integer strlen
integer firstdash, lastdash
integer firstcolon, lastcolon
integer lastspace
strlen = LEN_TRIM (TimeUnits)
firstdash = index(TimeUnits, '-')
lastdash = index(TimeUnits, '-', BACK=.TRUE.)
if (firstdash .LE. 0 .OR. lastdash .LE. 0) then
_FAIL('time string is not a valid format')
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 ( TimeUnits(ypos(1):ypos(2)), * ) year
read ( TimeUnits(mpos(1):mpos(2)), * ) month
read ( TimeUnits(dpos(1):dpos(2)), * ) day
firstcolon = index(TimeUnits, ':')
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 (TimeUnits(strlen:strlen) /= char(0)) then
TimeUnits = trim(TimeUnits)//char(0)
strlen=len_trim(TimeUnits)
endif
lastspace = index(TRIM(TimeUnits), ' ', BACK=.TRUE.)
if ((strlen-lastspace).eq.2 .or. (strlen-lastspace).eq.3) then
hpos(1) = lastspace+1
hpos(2) = strlen-1
read (TimeUnits(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(TimeUnits, ':', BACK=.TRUE.)
if ( lastcolon .EQ. firstcolon ) then
mpos(1) = firstcolon + 1
mpos(2) = firstcolon + 2
read (TimeUnits(hpos(1):hpos(2)), * ) hour
read (TimeUnits(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 (TimeUnits(hpos(1):hpos(2)), * ) hour
read (TimeUnits(mpos(1):mpos(2)), * ) min
read (TimeUnits(spos(1):spos(2)), * ) sec
endif
endif
call ESMF_TimeSet(time,yy=year,mm=month,dd=day,h=hour,m=min,s=sec,rc=status)
_VERIFY(status)
end function parse_time_string