diff --git a/src/Morphir/SDK/LocalDate.elm b/src/Morphir/SDK/LocalDate.elm index e377aeb15..9e57ac5ef 100644 --- a/src/Morphir/SDK/LocalDate.elm +++ b/src/Morphir/SDK/LocalDate.elm @@ -19,7 +19,7 @@ module Morphir.SDK.LocalDate exposing ( LocalDate , diffInDays, diffInWeeks, diffInMonths, diffInYears , addDays, addWeeks, addMonths, addYears - , toISOString, fromISO, fromParts + , toISOString, fromCalendarDate, fromISO, fromOrdinalDate, fromParts, fromRataDie, toRataDie , DayOfWeek(..), dayOfWeek, isWeekend, isWeekday , Month(..) , year, month, day @@ -41,8 +41,10 @@ module Morphir.SDK.LocalDate exposing # Constructors -@docs toISOString, fromISO, fromParts +@docs fromCalendarDate, fromISO, fromOrdinalDate, fromParts, fromRataDie +# Convert +@docs toISOString, toRataDie # Query @@ -50,6 +52,8 @@ module Morphir.SDK.LocalDate exposing @docs Month @docs year, month, day + +@docs toISOString -} import Date exposing (Date, Unit(..)) @@ -117,6 +121,24 @@ addYears : Int -> LocalDate -> LocalDate addYears count date = Date.add Years count date +{-| Create a date from a [calendar date][gregorian]: a year, month, and day of +the month. Out-of-range day values will be clamped. + + import Date exposing (fromCalendarDate) + import Time exposing (Month(..)) + + fromCalendarDate 2018 Sep 26 + +[gregorian]: https://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar + +-} +fromCalendarDate : Int -> Month -> Int -> LocalDate +fromCalendarDate y m d = + Date.fromCalendarDate y (monthToMonth m) d + +fromOrdinalDate : Int -> Int -> LocalDate +fromOrdinalDate y d = + Date.fromOrdinalDate y d {-| Construct a LocalDate based on ISO formatted string. Opportunity for error denoted by Maybe return type. -} @@ -213,6 +235,44 @@ month localDate = Time.Dec -> December +monthToMonth : Month -> Time.Month +monthToMonth m = + case m of + January -> + Time.Jan + + February -> + Time.Feb + + March -> + Time.Mar + + April -> + Time.Apr + + May -> + Time.May + + June -> + Time.Jun + + July -> + Time.Jul + + August -> + Time.Aug + + September -> + Time.Sep + + October -> + Time.Oct + + November -> + Time.Nov + + December -> + Time.Dec {-| The day of the month (1–31). -} @@ -297,3 +357,18 @@ type Month | October | November | December + +{-| Construct a LocalDate from Integer Rata Die, a system for system for assigning calendar days to +numbers, with 1 representing 0001-01-01. +-} +fromRataDie : Int -> LocalDate +fromRataDie rataDieNumber = + Date.fromRataDie rataDieNumber + + +{-| Convert a LocalDate to its number representation in Rata Die. Rata Die is a system for +assigning calendar days to numbers, with 1 representing 0001-01-01. +-} +toRataDie : LocalDate -> Int +toRataDie localDate = + Date.toRataDie localDate diff --git a/tests/Morphir/SDK/LocalDateTests.elm b/tests/Morphir/SDK/LocalDateTests.elm index 220c37fd0..89ae364c6 100644 --- a/tests/Morphir/SDK/LocalDateTests.elm +++ b/tests/Morphir/SDK/LocalDateTests.elm @@ -96,4 +96,29 @@ constructorTests = \_ -> LocalDate.fromParts 2020 2 30 |> Expect.equal Nothing + , test "valid fromCalendarDate" <| + \_ -> + LocalDate.fromCalendarDate 2023 December 25 + |> Expect.equal (Date.fromCalendarDate 2023 Dec 25) + , test "invalid but pinned fromCalendarDate" <| + \_ -> + LocalDate.fromCalendarDate 2023 December 39 + |> Expect.equal (Date.fromCalendarDate 2023 Dec 31) + , test "valid fromRataDie" <| + \_ -> + LocalDate.fromRataDie 1 + |> Expect.equal (Date.fromCalendarDate 1 Jan 1) + , test "valid contemporary fromRataDie" <| + \_ -> + LocalDate.fromRataDie 738860 + |> Expect.equal (Date.fromCalendarDate 2023 Dec 6) + , test "valid toRataDie" <| + \_ -> + Date.fromCalendarDate 1 Jan 1 + |> LocalDate.toRataDie + |> Expect.equal 1 + , test "valid contemporary toRataDie" <| + \_ -> + LocalDate.toRataDie (Date.fromCalendarDate 2023 Dec 6) + |> Expect.equal 738860 ]