civil_from_days Subroutine

private pure subroutine civil_from_days(days, y, m, d)

Inverse of days_from_civil.

Arguments

Type IntentOptional Attributes Name
integer(kind=int64), intent(in) :: days
integer(kind=int64), intent(out) :: y
integer(kind=int64), intent(out) :: m
integer(kind=int64), intent(out) :: d

Called by

proc~~civil_from_days~~CalledByGraph proc~civil_from_days civil_from_days proc~datetime_from_unix_ms datetime_from_unix_ms proc~datetime_from_unix_ms->proc~civil_from_days proc~now_utc now_utc proc~now_utc->proc~datetime_from_unix_ms

Variables

Type Visibility Attributes Name Initial
integer(kind=int64), private :: doe
integer(kind=int64), private :: doy
integer(kind=int64), private :: era
integer(kind=int64), private :: mp
integer(kind=int64), private :: yoe
integer(kind=int64), private :: z

Source Code

   pure subroutine civil_from_days(days, y, m, d)
      !! Inverse of `days_from_civil`.
      integer(int64), intent(in) :: days
      integer(int64), intent(out) :: y
      integer(int64), intent(out) :: m
      integer(int64), intent(out) :: d

      integer(int64) :: z, era, doe, yoe, doy, mp

      z = days + DAYS_EPOCH_SHIFT
      if (z >= 0_int64) then
         era = z/146097_int64
      else
         era = (z - 146096_int64)/146097_int64
      end if

      doe = z - era*146097_int64                                       ! [0, 146096]
      yoe = (doe - doe/1460_int64 + doe/36524_int64 - doe/146096_int64)/365_int64
      y = yoe + era*400_int64
      doy = doe - (365_int64*yoe + yoe/4_int64 - yoe/100_int64)         ! [0, 365]
      mp = (5_int64*doy + 2_int64)/153_int64                            ! [0, 11]
      d = doy - (153_int64*mp + 2_int64)/5_int64 + 1_int64              ! [1, 31]

      if (mp < 10_int64) then
         m = mp + 3_int64
      else
         m = mp - 9_int64
      end if
      if (m <= 2_int64) y = y + 1_int64
   end subroutine civil_from_days