From eb46ff7d191775452caf09cc13ac48660073de77 Mon Sep 17 00:00:00 2001 From: goldste Date: Wed, 8 Jul 2020 18:27:57 -0400 Subject: [PATCH 1/2] Added Date --- src/Morphir/IR/SDK/Date.elm | 30 ++++++++++ src/Morphir/SDK/Date.elm | 106 ++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 src/Morphir/IR/SDK/Date.elm create mode 100644 src/Morphir/SDK/Date.elm diff --git a/src/Morphir/IR/SDK/Date.elm b/src/Morphir/IR/SDK/Date.elm new file mode 100644 index 000000000..9d40ec86d --- /dev/null +++ b/src/Morphir/IR/SDK/Date.elm @@ -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") [] diff --git a/src/Morphir/SDK/Date.elm b/src/Morphir/SDK/Date.elm new file mode 100644 index 000000000..17474043a --- /dev/null +++ b/src/Morphir/SDK/Date.elm @@ -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 + ) From caeeac6b248b95366bc15bf2564fd850ff49ed7b Mon Sep 17 00:00:00 2001 From: Stephen Goldbaum Date: Thu, 9 Jul 2020 16:47:36 -0400 Subject: [PATCH 2/2] Introducing LocalDate --- src/Morphir/SDK/LocalDate.elm | 126 +++++++++++++++++++++++++++ tests/Morphir/SDK/LocalDateTests.elm | 24 +++++ 2 files changed, 150 insertions(+) create mode 100644 src/Morphir/SDK/LocalDate.elm create mode 100644 tests/Morphir/SDK/LocalDateTests.elm diff --git a/src/Morphir/SDK/LocalDate.elm b/src/Morphir/SDK/LocalDate.elm new file mode 100644 index 000000000..e8a22c8fd --- /dev/null +++ b/src/Morphir/SDK/LocalDate.elm @@ -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 +-- ) +-} diff --git a/tests/Morphir/SDK/LocalDateTests.elm b/tests/Morphir/SDK/LocalDateTests.elm new file mode 100644 index 000000000..b70407d00 --- /dev/null +++ b/tests/Morphir/SDK/LocalDateTests.elm @@ -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) + ] +-}