Skip to content

Commit 778624f

Browse files
authored
Merge pull request #13 from purescript-contrib/now
Add `JSDate`-specific `now`, add `fromInstant`
2 parents f882b5f + fa7d9d4 commit 778624f

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

bower.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"purescript-datetime": "^3.0.0",
2020
"purescript-exceptions": "^3.0.0",
2121
"purescript-foreign": "^4.0.0",
22-
"purescript-integers": "^3.0.0"
22+
"purescript-integers": "^3.0.0",
23+
"purescript-now": "^3.0.0"
2324
},
2425
"devDependencies": {
2526
"purescript-assert": "^3.0.0",

src/Data/JSDate.js

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ var createLocalDate = function (y, m, d, h, mi, s, ms) {
1717
return date;
1818
};
1919

20+
exports.now = function () {
21+
return new Date();
22+
};
23+
2024
exports.isValid = function (date) {
2125
return !isNaN(date.getTime());
2226
};
@@ -30,6 +34,10 @@ exports.toInstantImpl = function (just) {
3034
};
3135
};
3236

37+
exports.fromInstant = function (instant) {
38+
return new Date(instant);
39+
};
40+
3341
exports.jsdate = function (parts) {
3442
return createDate(parts.year, parts.month, parts.day, parts.hour, parts.minute, parts.second, parts.millisecond);
3543
};

src/Data/JSDate.purs

+13-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ module Data.JSDate
1414
, fromDateTime
1515
, toDateTime
1616
, toDate
17+
, fromInstant
1718
, toInstant
1819
, jsdate
1920
, jsdateLocal
21+
, now
2022
, parse
2123
, getTime
2224
, getUTCDate
@@ -47,7 +49,7 @@ import Prelude
4749

4850
import Control.Monad.Eff (kind Effect, Eff)
4951
import Control.Monad.Eff.Exception (EXCEPTION)
50-
52+
import Control.Monad.Eff.Now (NOW)
5153
import Data.Date as Date
5254
import Data.DateTime (DateTime(..), Date)
5355
import Data.DateTime as DateTime
@@ -100,6 +102,9 @@ toDateTime = map Instant.toDateTime <$> toInstant
100102
toDate :: JSDate -> Maybe Date
101103
toDate = map DateTime.date <$> toDateTime
102104

105+
-- | Creates a `JSDate` from an `Instant` value.
106+
foreign import fromInstant :: Instant -> JSDate
107+
103108
-- | Attempts to construct an `Instant` for a `JSDate`. `Nothing` is returned
104109
-- | only when the date value is an invalid date.
105110
toInstant :: JSDate -> Maybe Instant
@@ -151,6 +156,13 @@ foreign import dateMethod :: forall a. Fn2 String JSDate a
151156
foreign import parse
152157
:: forall eff. String -> Eff (locale :: LOCALE | eff) JSDate
153158

159+
-- | Gets a `JSDate` value for the date and time according to the current
160+
-- | machine's clock.
161+
-- |
162+
-- | Unless a `JSDate` is required specifically, consider using the functions in
163+
-- | `Control.Monad.Eff.Now` instead.
164+
foreign import now :: forall eff. Eff (now :: NOW | eff) JSDate
165+
154166
-- | Returns the date as a number of milliseconds since 1970-01-01 00:00:00 UTC.
155167
getTime :: JSDate -> Number
156168
getTime dt = runFn2 dateMethod "getTime" dt

0 commit comments

Comments
 (0)