Skip to content

Commit

Permalink
Additional functions for LocalDate
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianReeves committed Dec 9, 2023
1 parent 8e28912 commit 5ed1c85
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
79 changes: 77 additions & 2 deletions src/Morphir/SDK/LocalDate.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,15 +41,19 @@ module Morphir.SDK.LocalDate exposing
# Constructors
@docs toISOString, fromISO, fromParts
@docs fromCalendarDate, fromISO, fromOrdinalDate, fromParts, fromRataDie
# Convert
@docs toISOString, toRataDie
# Query
@docs DayOfWeek, dayOfWeek, isWeekend, isWeekday
@docs Month
@docs year, month, day
@docs toISOString
-}

import Date exposing (Date, Unit(..))
Expand Down Expand Up @@ -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.
-}
Expand Down Expand Up @@ -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).
-}
Expand Down Expand Up @@ -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
25 changes: 25 additions & 0 deletions tests/Morphir/SDK/LocalDateTests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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
]

0 comments on commit 5ed1c85

Please sign in to comment.