Skip to content

Commit

Permalink
ADD: Eq, Ord, and Show instances for JSDate (#17)
Browse files Browse the repository at this point in the history
* ADD: Eq, Ord, and Show instances for JSDate

* ADD: fromTime to export list
  • Loading branch information
eric-corumdigital authored and garyb committed Mar 20, 2018
1 parent 778624f commit 54a32b6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Data/JSDate.js
Expand Up @@ -63,3 +63,7 @@ exports.parse = function (dateString) {
return new Date(dateString);
};
};

exports.fromTime = function (time) {
return new Date(time);
};
13 changes: 13 additions & 0 deletions src/Data/JSDate.purs
Expand Up @@ -43,6 +43,7 @@ module Data.JSDate
, toString
, toTimeString
, toUTCString
, fromTime
) where

import Prelude
Expand All @@ -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
Expand Down Expand Up @@ -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
12 changes: 12 additions & 0 deletions test/Test/Main.purs
Expand Up @@ -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
Expand Down

0 comments on commit 54a32b6

Please sign in to comment.