Skip to content
Snippets Groups Projects
Commit 25c80d0c authored by Sebastian Müller's avatar Sebastian Müller 🐈
Browse files

Merge branch 'datetime_partial_string_fix' into 'main'

mo_datetime: better conversion from string

See merge request !86
parents 243a0d28 1ef90d13
No related branches found
No related tags found
1 merge request!86mo_datetime: better conversion from string
Pipeline #285120 passed with stages
in 8 minutes and 26 seconds
......@@ -43,8 +43,8 @@
!!
!! ! create from datetime string
!! date5 = datetime('2023-05-08 12:32:30')
!! day1 = date('2023-05-08')
!! time1 = time('12:32:30')
!! day1 = puredate('2023-05-08')
!! time1 = puretime('12:32:30')
!! print*, date5 == time1%with_date(day1)
!! print*, date5 == day1%with_time(time1)
!! print*, date5 == datetime(day1, time1)
......@@ -605,7 +605,11 @@ contains
type(puredate) :: in_date
type(puretime) :: in_time
character(256), dimension(:), allocatable :: str_arr
call divide_string(trim(string), ' ', str_arr)
if ( index(string, "T") > 0 ) then
call divide_string(trim(string), 'T', str_arr)
else
call divide_string(trim(string), ' ', str_arr)
end if
in_date = d_from_string(str_arr(1))
in_time = midnight()
if(size(str_arr) > 1_i4) in_time = t_from_string(str_arr(2))
......@@ -995,9 +999,12 @@ contains
character(256), dimension(:), allocatable :: date_str
integer(i4) :: year, month, day
call divide_string(trim(string), '-', date_str)
read(date_str(1), *) year
read(date_str(2), *) month
read(date_str(3), *) day
year = 1_i4
month = 1_i4
day = 1_i4
if (len_trim(date_str(1)) > 0) read(date_str(1), *) year
if (size(date_str) > 1) read(date_str(2), *) month
if (size(date_str) > 2) read(date_str(3), *) day
d_from_string = d_init(year=year, month=month, day=day)
end function d_from_string
......@@ -1275,9 +1282,12 @@ contains
character(256), dimension(:), allocatable :: time_str
integer(i4) :: hour, minute, second
call divide_string(trim(string), ':', time_str)
read(time_str(1), *) hour
read(time_str(2), *) minute
read(time_str(3), *) second
hour = 0_i4
minute = 0_i4
second = 0_i4
if (len_trim(time_str(1)) > 0) read(time_str(1), *) hour
if (size(time_str) > 1) read(time_str(2), *) minute
if (size(time_str) > 2) read(time_str(3), *) second
t_from_string = t_init(hour=hour, minute=minute, second=second)
end function t_from_string
......
......@@ -247,6 +247,16 @@ contains
! date with time has higher julian day fraction
@assertTrue(julian >= julian2)
! partial datetime init from string
@assertTrue(datetime("1992-10-8 15:15:42") == datetime("1992-10-08T15:15:42"))
@assertTrue(datetime("1992-10-08 15:15:00") == datetime("1992-10-08 15:15"))
@assertTrue(datetime("1992-10-08 15:00:00") == datetime("1992-10-08 15"))
@assertTrue(datetime("1992-10-08T00:00:00") == datetime("1992-10-08"))
@assertTrue(datetime("1992-10-01T00:00:00") == datetime("1992-10"))
@assertTrue(datetime("1992-01-01T00:00:00") == datetime("1992"))
@assertTrue(datetime("1992-10-01 15:15:00") == datetime("1992-10T15:15"))
@assertTrue(datetime("1992-01-01 15:15:00") == datetime("1992 15:15"))
end subroutine test_datetime_types
end module test_mo_datetime
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment