File tree Expand file tree Collapse file tree 3 files changed +29
-0
lines changed Expand file tree Collapse file tree 3 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -63,3 +63,7 @@ exports.parse = function (dateString) {
63
63
return new Date ( dateString ) ;
64
64
} ;
65
65
} ;
66
+
67
+ exports . fromTime = function ( time ) {
68
+ return new Date ( time ) ;
69
+ } ;
Original file line number Diff line number Diff line change @@ -43,6 +43,7 @@ module Data.JSDate
43
43
, toString
44
44
, toTimeString
45
45
, toUTCString
46
+ , fromTime
46
47
) where
47
48
48
49
import Prelude
@@ -66,6 +67,15 @@ import Data.Time.Duration (Milliseconds(..))
66
67
-- | The type of JavaScript `Date` objects.
67
68
foreign import data JSDate :: Type
68
69
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
+
69
79
-- | The effect type used when indicating the current machine's date/time locale
70
80
-- | is used in computing a value.
71
81
foreign import data LOCALE :: Effect
@@ -263,3 +273,6 @@ toTimeString dt = runFn2 dateMethod "toTimeString" dt
263
273
-- | Returns the date as a string using the UTC timezone.
264
274
toUTCString :: JSDate -> String
265
275
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
Original file line number Diff line number Diff line change @@ -60,6 +60,18 @@ main = do
60
60
assert $ JSD .toDateTime (JSD .fromDateTime bottom) == Just bottom
61
61
assert $ JSD .toDateTime (JSD .fromDateTime top) == Just top
62
62
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
+
63
75
log " All tests done"
64
76
65
77
where
You can’t perform that action at this time.
0 commit comments