Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional functions for LocalDate needed for business rules and morphir-scala #1118

Merged
merged 3 commits into from Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
96 changes: 94 additions & 2 deletions src/Morphir/SDK/LocalDate.elm
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
29 changes: 29 additions & 0 deletions tests/Morphir/SDK/LocalDateTests.elm
Expand Up @@ -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
]