Skip to content

Commit

Permalink
Merge pull request #100 from Morgan-Stanley/date-intro
Browse files Browse the repository at this point in the history
LocalDate intro
  • Loading branch information
stephengoldbaum committed Jul 9, 2020
2 parents 51e7ceb + caeeac6 commit b9637b9
Show file tree
Hide file tree
Showing 4 changed files with 286 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Morphir/IR/SDK/Date.elm
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") []
106 changes: 106 additions & 0 deletions src/Morphir/SDK/Date.elm
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
)
126 changes: 126 additions & 0 deletions src/Morphir/SDK/LocalDate.elm
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
-- )
-}
24 changes: 24 additions & 0 deletions tests/Morphir/SDK/LocalDateTests.elm
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)
]
-}

0 comments on commit b9637b9

Please sign in to comment.