diff --git a/src/mo_datetime.f90 b/src/mo_datetime.f90 index 94bb1d373914203af454f7c40822c1ab110675f5..f74a55ca4696cc6fef227c8d30ad3fef40240300 100644 --- a/src/mo_datetime.f90 +++ b/src/mo_datetime.f90 @@ -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 diff --git a/src/pf_tests/test_mo_datetime.pf b/src/pf_tests/test_mo_datetime.pf index 18ecbe8fa19e55c633e9e0edd7831ebbea000399..b82b7fdb49d74479cd4d9e4b2271b0bf633c8eb5 100644 --- a/src/pf_tests/test_mo_datetime.pf +++ b/src/pf_tests/test_mo_datetime.pf @@ -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