Skip to content

Commit 54a32b6

Browse files
eric-corumdigitalgaryb
authored andcommitted
ADD: Eq, Ord, and Show instances for JSDate (#17)
* ADD: Eq, Ord, and Show instances for JSDate * ADD: fromTime to export list
1 parent 778624f commit 54a32b6

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/Data/JSDate.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,7 @@ exports.parse = function (dateString) {
6363
return new Date(dateString);
6464
};
6565
};
66+
67+
exports.fromTime = function (time) {
68+
return new Date(time);
69+
};

src/Data/JSDate.purs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ module Data.JSDate
4343
, toString
4444
, toTimeString
4545
, toUTCString
46+
, fromTime
4647
) where
4748

4849
import Prelude
@@ -66,6 +67,15 @@ import Data.Time.Duration (Milliseconds(..))
6667
-- | The type of JavaScript `Date` objects.
6768
foreign import data JSDate :: Type
6869

70+
instance eqJSDate :: Eq JSDate where
71+
eq a b = getTime a == getTime b
72+
73+
instance ordJSDate :: Ord JSDate where
74+
compare a b = getTime a `compare` getTime b
75+
76+
instance showJSDate :: Show JSDate where
77+
show a = "(fromTime " <> show (getTime a) <> ")"
78+
6979
-- | The effect type used when indicating the current machine's date/time locale
7080
-- | is used in computing a value.
7181
foreign import data LOCALE :: Effect
@@ -263,3 +273,6 @@ toTimeString dt = runFn2 dateMethod "toTimeString" dt
263273
-- | Returns the date as a string using the UTC timezone.
264274
toUTCString :: JSDate -> String
265275
toUTCString dt = runFn2 dateMethod "toUTCString" dt
276+
277+
-- | Returns the date at a number of milliseconds since 1970-01-01 00:00:00 UTC.
278+
foreign import fromTime :: Number -> JSDate

test/Test/Main.purs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ main = do
6060
assert $ JSD.toDateTime (JSD.fromDateTime bottom) == Just bottom
6161
assert $ JSD.toDateTime (JSD.fromDateTime top) == Just top
6262

63+
log "Check that equal dates test equal"
64+
assert $ JSD.fromDateTime dateTime == JSD.fromDateTime dateTime
65+
assert $ JSD.fromDateTime ancientDateTime == JSD.fromDateTime ancientDateTime
66+
67+
log "Check that unequal dates do not test equal"
68+
assert $ JSD.fromDateTime dateTime /= JSD.fromDateTime ancientDateTime
69+
70+
log "Check that dates are chronologically ordered"
71+
assert $ JSD.fromDateTime dateTime `compare` JSD.fromDateTime dateTime == EQ
72+
assert $ JSD.fromDateTime dateTime `compare` JSD.fromDateTime ancientDateTime == GT
73+
assert $ JSD.fromDateTime ancientDateTime `compare` JSD.fromDateTime dateTime == LT
74+
6375
log "All tests done"
6476

6577
where

0 commit comments

Comments
 (0)