diff --git a/src/Data/JSDate.js b/src/Data/JSDate.js index 0e9de62..0c9abf9 100644 --- a/src/Data/JSDate.js +++ b/src/Data/JSDate.js @@ -63,3 +63,7 @@ exports.parse = function (dateString) { return new Date(dateString); }; }; + +exports.fromTime = function (time) { + return new Date(time); +}; diff --git a/src/Data/JSDate.purs b/src/Data/JSDate.purs index e756cea..407d503 100644 --- a/src/Data/JSDate.purs +++ b/src/Data/JSDate.purs @@ -43,6 +43,7 @@ module Data.JSDate , toString , toTimeString , toUTCString + , fromTime ) where import Prelude @@ -66,6 +67,15 @@ import Data.Time.Duration (Milliseconds(..)) -- | The type of JavaScript `Date` objects. foreign import data JSDate :: Type +instance eqJSDate :: Eq JSDate where + eq a b = getTime a == getTime b + +instance ordJSDate :: Ord JSDate where + compare a b = getTime a `compare` getTime b + +instance showJSDate :: Show JSDate where + show a = "(fromTime " <> show (getTime a) <> ")" + -- | The effect type used when indicating the current machine's date/time locale -- | is used in computing a value. foreign import data LOCALE :: Effect @@ -263,3 +273,6 @@ toTimeString dt = runFn2 dateMethod "toTimeString" dt -- | Returns the date as a string using the UTC timezone. toUTCString :: JSDate -> String toUTCString dt = runFn2 dateMethod "toUTCString" dt + +-- | Returns the date at a number of milliseconds since 1970-01-01 00:00:00 UTC. +foreign import fromTime :: Number -> JSDate diff --git a/test/Test/Main.purs b/test/Test/Main.purs index fd6a456..ee1d6ac 100644 --- a/test/Test/Main.purs +++ b/test/Test/Main.purs @@ -60,6 +60,18 @@ main = do assert $ JSD.toDateTime (JSD.fromDateTime bottom) == Just bottom assert $ JSD.toDateTime (JSD.fromDateTime top) == Just top + log "Check that equal dates test equal" + assert $ JSD.fromDateTime dateTime == JSD.fromDateTime dateTime + assert $ JSD.fromDateTime ancientDateTime == JSD.fromDateTime ancientDateTime + + log "Check that unequal dates do not test equal" + assert $ JSD.fromDateTime dateTime /= JSD.fromDateTime ancientDateTime + + log "Check that dates are chronologically ordered" + assert $ JSD.fromDateTime dateTime `compare` JSD.fromDateTime dateTime == EQ + assert $ JSD.fromDateTime dateTime `compare` JSD.fromDateTime ancientDateTime == GT + assert $ JSD.fromDateTime ancientDateTime `compare` JSD.fromDateTime dateTime == LT + log "All tests done" where