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

Add additional functions to Morphir.SDK.LocalDate and ensure exports and Morphir.IR.SDK.LocalDate matches #1119

Merged
merged 8 commits into from Dec 16, 2023
4 changes: 3 additions & 1 deletion .gitignore
Expand Up @@ -19,4 +19,6 @@ Morphir.Elm.CLI.js
Morphir.Elm.Generator.js
docs.json
/.coverage/
tests-integration/reference-model/Dockerfile
tests-integration/reference-model/Dockerfile

.scala-build/
7 changes: 7 additions & 0 deletions elm-tooling.json
@@ -0,0 +1,7 @@
{
"tools": {
"elm": "0.19.1",
"elm-format": "0.8.5",
"elm-json": "0.2.13"
}
}
19 changes: 11 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -19,7 +19,8 @@
"ncc-morphir-server": "ncc build cli/morphir-elm-develop.js -o dist/morphir-server",
"build": "gulp && npm run ncc-morphir && npm run ncc-morphir-server && npx jest && gulp test",
"test-coverage": "elm-coverage src/ --open",
"build-value-editor": "elm make cli/src/Morphir/Web/Editor.elm --output=cli/web/valueEditor.js"
"build-value-editor": "elm make cli/src/Morphir/Web/Editor.elm --output=cli/web/valueEditor.js",
"postinstall": "elm-tooling install"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -75,6 +76,7 @@
"elm-doc-preview": "^5.0.5",
"elm-format": "^0.8.5",
"elm-test": "^0.19.1-revision6",
"elm-tooling": "^1.15.0",
"execa": "^5.1.1",
"gulp": "^4.0.2",
"gulp-mocha": "^8.0.0",
Expand Down
40 changes: 37 additions & 3 deletions src/Morphir/IR/SDK/LocalDate.elm
Expand Up @@ -22,8 +22,8 @@ import Morphir.IR.Documented exposing (Documented)
import Morphir.IR.Literal as Literal
import Morphir.IR.Module as Module exposing (ModuleName)
import Morphir.IR.Name as Name
import Morphir.IR.Path as Path exposing (Path)
import Morphir.IR.SDK.Basics exposing (intType)
import Morphir.IR.Path as Path
import Morphir.IR.SDK.Basics exposing (boolType, intType)
import Morphir.IR.SDK.Common exposing (toFQName, vSpec)
import Morphir.IR.SDK.Maybe exposing (maybeType)
import Morphir.IR.SDK.String exposing (stringType)
Expand Down Expand Up @@ -53,6 +53,20 @@ moduleSpec =
, DerivedTypeSpecification [] config
|> Documented "Type that represents a date concept."
)
, ( Name.fromString "DayOfWeek"
, CustomTypeSpecification []
(Dict.fromList
[ ( Name.fromString "Monday", [] )
, ( Name.fromString "Tuesday", [] )
, ( Name.fromString "Wednesday", [] )
, ( Name.fromString "Thursday", [] )
, ( Name.fromString "Friday", [] )
, ( Name.fromString "Saturday", [] )
, ( Name.fromString "Sunday", [] )
]
)
|> Documented "Type that represents days of the week."
)
, ( Name.fromString "Month"
, CustomTypeSpecification []
(Dict.fromList
Expand All @@ -75,9 +89,13 @@ moduleSpec =
]
, values =
Dict.fromList
[ vSpec "toISOString" [ ( "date", localDateType () ) ] (stringType ())
[ vSpec "fromCalendarDate" [ ( "y", intType () ), ( "m", monthType () ), ( "d", intType () ) ] (localDateType ())
, vSpec "toISOString" [ ( "date", localDateType () ) ] (stringType ())
, vSpec "fromISO" [ ( "iso", stringType () ) ] (maybeType () (localDateType ()))
, vSpec "fromOrdinalDate" [ ( "y", intType () ), ( "dayOfyear", intType () ) ] (localDateType ())
, vSpec "fromParts" [ ( "year", intType () ), ( "month", intType () ), ( "day", intType () ) ] (maybeType () (localDateType ()))
, vSpec "day" [ ( "localDate", localDateType () ) ] (intType ())
, vSpec "dayOfWeek" [ ( "localDate", localDateType () ) ] (dayOfWeekType ())
, vSpec "diffInDays" [ ( "date1", localDateType () ), ( "date2", localDateType () ) ] (intType ())
, vSpec "diffInWeeks" [ ( "date1", localDateType () ), ( "date2", localDateType () ) ] (intType ())
, vSpec "diffInMonths" [ ( "date1", localDateType () ), ( "date2", localDateType () ) ] (intType ())
Expand All @@ -86,16 +104,32 @@ moduleSpec =
, vSpec "addWeeks" [ ( "offset", intType () ), ( "startDate", localDateType () ) ] (localDateType ())
, vSpec "addMonths" [ ( "offset", intType () ), ( "startDate", localDateType () ) ] (localDateType ())
, vSpec "addYears" [ ( "offset", intType () ), ( "startDate", localDateType () ) ] (localDateType ())
, vSpec "isWeekend" [ ( "localDate", localDateType () ) ] (boolType ())
, vSpec "isWeekday" [ ( "localDate", localDateType () ) ] (boolType ())
, vSpec "month" [ ( "localDate", localDateType () ) ] (monthType ())
, vSpec "monthNumber" [ ( "localDate", localDateType () ) ] (intType ())
, vSpec "monthToInt" [ ( "m", monthType () ) ] (intType ())
, vSpec "year" [ ( "localDate", localDateType () ) ] (intType ())
]
, doc = Just "Contains the LocalDate type (representing a date concept), and it's associated functions."
}


dayOfWeekType : a -> Type a
dayOfWeekType attributes =
Reference attributes (toFQName moduleName "DayOfWeek") []


localDateType : a -> Type a
localDateType attributes =
Reference attributes (toFQName moduleName "LocalDate") []


monthType : a -> Type a
monthType attributes =
Reference attributes (toFQName moduleName "Month") []


nativeFunctions : List ( String, Native.Function )
nativeFunctions =
[ ( "fromISO"
Expand Down
122 changes: 95 additions & 27 deletions src/Morphir/SDK/LocalDate.elm
Expand Up @@ -19,11 +19,11 @@ module Morphir.SDK.LocalDate exposing
( LocalDate
, diffInDays, diffInWeeks, diffInMonths, diffInYears
, addDays, addWeeks, addMonths, addYears
, fromCalendarDate, fromISO, fromOrdinalDate, fromParts, fromRataDie
, toISOString, toRataDie
, fromCalendarDate, fromISO, fromOrdinalDate, fromParts
, toISOString, monthToInt
, DayOfWeek(..), dayOfWeek, isWeekend, isWeekday
, Month(..)
, year, month, day
, year, month, monthNumber, day
)

{-| This module adds the definition of a date without time zones. Useful in business modeling.
Expand All @@ -42,19 +42,19 @@ module Morphir.SDK.LocalDate exposing

# Constructors

@docs fromCalendarDate, fromISO, fromOrdinalDate, fromParts, fromRataDie
@docs fromCalendarDate, fromISO, fromOrdinalDate, fromParts


# Convert

@docs toISOString, toRataDie
@docs toISOString, monthToInt


# Query

@docs DayOfWeek, dayOfWeek, isWeekend, isWeekday
@docs Month
@docs year, month, day
@docs year, month, monthNumber, day

-}

Expand Down Expand Up @@ -150,8 +150,8 @@ year. Out-of-range day values will be clamped.

-}
fromOrdinalDate : Int -> Int -> LocalDate
fromOrdinalDate y d =
Date.fromOrdinalDate y d
fromOrdinalDate y dayOfYear =
Date.fromOrdinalDate y dayOfYear


{-| Construct a LocalDate based on ISO formatted string. Opportunity for error denoted by Maybe return type.
Expand All @@ -173,14 +173,14 @@ Errors can occur when any of the given values fall outside of their relevant con
For example, the date given as 2000 2 30 (2000-Feb-30) would fail because the day of the 30th is impossible.
-}
fromParts : Int -> Int -> Int -> Maybe LocalDate
fromParts yearNumber monthNumber dayOfMonthNumber =
fromParts yearNumber monthNum dayOfMonthNumber =
-- We do all of this processing because our Elm Date library accepts invalid values while most other languages don't.
-- So we want to maintain consistency.
-- Oddly, Date has fromCalendarParts, but it's not exposed.
let
maybeMonth =
if monthNumber > 0 && monthNumber < 13 then
Just (Date.numberToMonth monthNumber)
if monthNum > 0 && monthNum < 13 then
Just (Date.numberToMonth monthNum)

else
Nothing
Expand Down Expand Up @@ -250,6 +250,90 @@ month localDate =
December


{-| Returns the month of the year as an Int, where January is month 1 and December is month 12.
-}
monthNumber : LocalDate -> Int
monthNumber localDate =
case Date.month localDate of
Time.Jan ->
1

Time.Feb ->
2

Time.Mar ->
3

Time.Apr ->
4

Time.May ->
5

Time.Jun ->
6

Time.Jul ->
7

Time.Aug ->
8

Time.Sep ->
9

Time.Oct ->
10

Time.Nov ->
11

Time.Dec ->
12


{-| Converts a Month to an Int, where January is month 1 and December is month 12.
-}
monthToInt : Month -> Int
monthToInt m =
case m of
January ->
1

February ->
2

March ->
3

April ->
4

May ->
5

June ->
6

July ->
7

August ->
8

September ->
9

October ->
10

November ->
11

December ->
12


monthToMonth : Month -> Time.Month
monthToMonth m =
case m of
Expand Down Expand Up @@ -373,19 +457,3 @@ 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