-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from Morgan-Stanley/date-intro
LocalDate intro
- Loading branch information
Showing
4 changed files
with
286 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Morphir.IR.SDK.Date exposing (..) | ||
|
||
import Dict | ||
import Morphir.IR.Documented exposing (Documented) | ||
import Morphir.IR.Module as Module exposing (ModulePath) | ||
import Morphir.IR.Name as Name | ||
import Morphir.IR.Path as Path exposing (Path) | ||
import Morphir.IR.SDK.Common exposing (toFQName) | ||
import Morphir.IR.Type exposing (Specification(..), Type(..)) | ||
|
||
|
||
moduleName : ModulePath | ||
moduleName = | ||
Path.fromString "Date" | ||
|
||
|
||
moduleSpec : Module.Specification () | ||
moduleSpec = | ||
{ types = | ||
Dict.fromList | ||
[ ( Name.fromString "Date", OpaqueTypeSpecification [] |> Documented "Type that represents a date concept." ) | ||
] | ||
, values = | ||
Dict.empty | ||
} | ||
|
||
|
||
dateType : a -> Type a | ||
dateType attributes = | ||
Reference attributes (toFQName moduleName "Date") [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
module Morphir.SDK.Date exposing (..) | ||
|
||
{-| A `Date` is a representation of a day of the calendar without consideration for location. | ||
This module defines local date operations. | ||
@docs Date | ||
-} | ||
|
||
import Morphir.SDK.Int exposing (Int) | ||
|
||
|
||
type alias Date = | ||
Basics.Date | ||
|
||
|
||
type alias RataDie = | ||
Int | ||
|
||
|
||
{-| Represents a date using the Rata Die system. | ||
This has the advantage of making Date comparable since Int is comparable. | ||
-} | ||
type alias Date = | ||
Native RataDie | ||
|
||
|
||
year : Date -> Int | ||
year ld = | ||
native | ||
(ld | ||
|> Date.fromRataDie | ||
|> Date.year | ||
) | ||
|
||
|
||
plusYears : Int -> Date -> Date | ||
plusYears yearCount ld = | ||
native | ||
(ld | ||
|> Date.fromRataDie | ||
|> Date.add Date.Years yearCount | ||
|> Date.toRataDie | ||
) | ||
|
||
|
||
month : Date -> Int | ||
month ld = | ||
native | ||
(ld | ||
|> Date.fromRataDie | ||
|> Date.monthNumber | ||
) | ||
|
||
|
||
plusMonths : Int -> Date -> Date | ||
plusMonths monthCount ld = | ||
native | ||
(ld | ||
|> Date.fromRataDie | ||
|> Date.add Date.Months monthCount | ||
|> Date.toRataDie | ||
) | ||
|
||
|
||
day : Date -> Int | ||
day ld = | ||
native | ||
(ld | ||
|> Date.fromRataDie | ||
|> Date.day | ||
) | ||
|
||
|
||
plusDays : Int -> Date -> Date | ||
plusDays dayCount ld = | ||
native | ||
(ld | ||
|> Date.fromRataDie | ||
|> Date.add Date.Days dayCount | ||
|> Date.toRataDie | ||
) | ||
|
||
|
||
diffInDays : Date -> Date -> Int | ||
diffInDays ld1 ld2 = | ||
native | ||
(Date.diff | ||
Date.Days | ||
(ld1 |> Date.fromRataDie) | ||
(ld2 |> Date.fromRataDie) | ||
) | ||
|
||
|
||
type alias Month = | ||
Native Date.Month | ||
|
||
|
||
lastDayOfMonth : Month -> Int -> Date | ||
lastDayOfMonth m y = | ||
native | ||
(Date.fromCalendarDate y m 1 | ||
|> Date.add Date.Months 1 | ||
|> Date.add Date.Days -1 | ||
|> Date.toRataDie | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
module Morphir.SDK.LocalDate exposing (LocalDate) | ||
|
||
-- This is a temporary stub until we get into more requirements | ||
type LocalDate = LocalDate | ||
|
||
|
||
|
||
{- | ||
module Morphir.SDK.LocalDate exposing (Date, Month, Unit, fromCalendarDate, add) | ||
-- ( Date | ||
-- , Month, Weekday | ||
-- , today, fromPosix, fromCalendarDate, fromWeekDate, fromOrdinalDate, fromIsoString, fromRataDie | ||
-- , toIsoString, toRataDie | ||
-- , year, month, day, weekYear, weekNumber, weekday, ordinalDay, quarter, monthNumber, weekdayNumber | ||
-- , format | ||
-- , Language, formatWithLanguage | ||
-- , Unit(..), add, diff | ||
-- , Interval(..), ceiling, floor | ||
-- , range | ||
-- , compare, isBetween, min, max, clamp | ||
-- , monthToNumber, numberToMonth, weekdayToNumber, numberToWeekday | ||
-- ) | ||
{-| A `Date` is a representation of a day of the calendar without consideration for location. | ||
This module defines local date operations. | ||
@docs Date | ||
-} | ||
import Morphir.SDK.Int exposing (Int) | ||
-- import Date exposing (Date, Month, Unit(..)) | ||
import Date | ||
{-| Represents a date using the Rata Die system. | ||
This has the advantage of making Date comparable since Int is comparable. | ||
-} | ||
type alias Date = | ||
Date.Date | ||
type alias Month = | ||
Date.Month | ||
type alias Unit = | ||
Date.Unit | ||
fromCalendarDate : Int -> Month -> Int -> Date | ||
fromCalendarDate = | ||
Date.fromCalendarDate | ||
-- Maths -- | ||
add : Unit -> Int -> Date -> Date | ||
add = Date.add | ||
-- year : Date -> Int | ||
-- year ld = | ||
-- Date.year ld | ||
-- month : Date -> Int | ||
-- month ld = | ||
-- native | ||
-- (ld | ||
-- |> Date.fromRataDie | ||
-- |> Date.monthNumber | ||
-- ) | ||
-- plusMonths : Int -> Date -> Date | ||
-- plusMonths monthCount ld = | ||
-- native | ||
-- (ld | ||
-- |> Date.fromRataDie | ||
-- |> Date.add Date.Months monthCount | ||
-- |> Date.toRataDie | ||
-- ) | ||
-- day : Date -> Int | ||
-- day ld = | ||
-- native | ||
-- (ld | ||
-- |> Date.fromRataDie | ||
-- |> Date.day | ||
-- ) | ||
-- plusDays : Int -> Date -> Date | ||
-- plusDays dayCount ld = | ||
-- native | ||
-- (ld | ||
-- |> Date.fromRataDie | ||
-- |> Date.add Date.Days dayCount | ||
-- |> Date.toRataDie | ||
-- ) | ||
-- diffInDays : Date -> Date -> Int | ||
-- diffInDays ld1 ld2 = | ||
-- native | ||
-- (Date.diff | ||
-- Date.Days | ||
-- (ld1 |> Date.fromRataDie) | ||
-- (ld2 |> Date.fromRataDie) | ||
-- ) | ||
-- type alias Month = | ||
-- Native Date.Month | ||
-- lastDayOfMonth : Month -> Int -> Date | ||
-- lastDayOfMonth m y = | ||
-- native | ||
-- (Date.fromCalendarDate y m 1 | ||
-- |> Date.add Date.Months 1 | ||
-- |> Date.add Date.Days -1 | ||
-- |> Date.toRataDie | ||
-- ) | ||
-} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Morphir.SDK.LocalDateTests exposing (..) | ||
|
||
import Expect | ||
import Test exposing (..) | ||
|
||
import Morphir.SDK.LocalDate | ||
|
||
ld = 1 | ||
|
||
{- | ||
import Time exposing (Month(..)) | ||
import Date exposing (Unit(..)) | ||
mathTests : Test | ||
mathTests = | ||
describe "date maths" | ||
[ test "add day" <| | ||
\_ -> | ||
(LocalDate.fromCalendarDate 2020 Jan 1) | ||
|> LocalDate.add Months 1 | ||
|> Expect.equal (LocalDate.fromCalendarDate 2020 Feb 1) | ||
] | ||
-} |