diff --git a/src/Morphir/SDK/LocalDate.elm b/src/Morphir/SDK/LocalDate.elm index e377aeb15..3357383a4 100644 --- a/src/Morphir/SDK/LocalDate.elm +++ b/src/Morphir/SDK/LocalDate.elm @@ -19,7 +19,8 @@ module Morphir.SDK.LocalDate exposing ( LocalDate , diffInDays, diffInWeeks, diffInMonths, diffInYears , addDays, addWeeks, addMonths, addYears - , toISOString, fromISO, fromParts + , fromCalendarDate, fromISO, fromOrdinalDate, fromParts, fromRataDie + , toISOString, toRataDie , DayOfWeek(..), dayOfWeek, isWeekend, isWeekday , Month(..) , year, month, day @@ -41,7 +42,12 @@ module Morphir.SDK.LocalDate exposing # Constructors -@docs toISOString, fromISO, fromParts +@docs fromCalendarDate, fromISO, fromOrdinalDate, fromParts, fromRataDie + + +# Convert + +@docs toISOString, toRataDie # Query @@ -118,6 +124,36 @@ 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 Morphir.SDK.LocalDate exposing (fromCalendarDate, Month(..)) + + fromCalendarDate 2018 September 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 + + +{-| Create a date from an [ordinal date][ordinaldate]: a year and day of the +year. Out-of-range day values will be clamped. + + import Morphir.SDK.LocalDate exposing (fromOrdinalDate) + + fromOrdinalDate 2018 269 + +[ordinaldate]: https://en.wikipedia.org/wiki/Ordinal_date + +-} +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. -} fromISO : String -> Maybe LocalDate @@ -214,6 +250,46 @@ month localDate = 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). -} day : LocalDate -> Int @@ -297,3 +373,19 @@ 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..ec85a15af 100644 --- a/tests/Morphir/SDK/LocalDateTests.elm +++ b/tests/Morphir/SDK/LocalDateTests.elm @@ -96,4 +96,33 @@ 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 fromOrdinalDate" <| + \_ -> + LocalDate.fromOrdinalDate 2023 15 + |> Expect.equal (Date.fromCalendarDate 2023 Jan 15) + , 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 ]