Returns a date number from an array with date and time
Note: Some wrong dates (e.g., 2023-02-29) will still return a number which will be wrong!
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer, | intent(in) | :: | date_array(6) |
Datetime [y,m,d,h,m,s] |
||
integer, | intent(in) | :: | ref_year |
Reference year |
Date number in seconds since ref_year
function date_to_number(date_array, ref_year) result(res) !! Returns a date number from an array with date and time !! !! Note: Some wrong dates (e.g., 2023-02-29) will still return a number which will be wrong! integer, intent(in) :: date_array(6) !! Datetime [y,m,d,h,m,s] integer, intent(in) :: ref_year !! Reference year real(dp) :: res !! Date number in seconds since ref_year ! Local variables integer :: y, m integer :: daysinmonth(12) = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] res = 0.0 if (date_array(1) .gt. ref_year) then ! Add up days in the year do y = ref_year, date_array(1) - 1 if (((mod(y,4) .eq. 0) .and. (mod(y,100) .ne. 0)) .or. (mod(y,400) .eq. 0)) then daysinmonth(2) = 29 else daysinmonth(2) = 28 end if do m = 1, 12 res = res + sngl(real(daysinmonth(m))) end do end do end if ! Add up days in the remaining months if (((mod(date_array(1),4) .eq. 0) .and. (mod(date_array(1),100) .ne. 0)) .or. (mod(date_array(1),400) .eq. 0)) then daysinmonth(2) = 29 else daysinmonth(2) = 28 end if if (date_array(2) .gt. 1) then do m = 1, date_array(2) - 1 res = res + sngl(real(daysinmonth(m))) end do end if res = res + sngl(real(date_array(3))) - 1.0 res = res + sngl(real(date_array(4)))/24.0 ! starts at 0 res = res + sngl(real(date_array(5)))/24.0/60.0 ! starts at 0 res = res + sngl(real(date_array(6)))/24.0/60.0/60.0 ! starts at 0 end function date_to_number