Skip to content

Commit

Permalink
Merge pull request #13 from purescript-contrib/now
Browse files Browse the repository at this point in the history
Add `JSDate`-specific `now`, add `fromInstant`
  • Loading branch information
garyb authored Jul 16, 2017
2 parents f882b5f + fa7d9d4 commit 778624f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"purescript-datetime": "^3.0.0",
"purescript-exceptions": "^3.0.0",
"purescript-foreign": "^4.0.0",
"purescript-integers": "^3.0.0"
"purescript-integers": "^3.0.0",
"purescript-now": "^3.0.0"
},
"devDependencies": {
"purescript-assert": "^3.0.0",
Expand Down
8 changes: 8 additions & 0 deletions src/Data/JSDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ var createLocalDate = function (y, m, d, h, mi, s, ms) {
return date;
};

exports.now = function () {
return new Date();
};

exports.isValid = function (date) {
return !isNaN(date.getTime());
};
Expand All @@ -30,6 +34,10 @@ exports.toInstantImpl = function (just) {
};
};

exports.fromInstant = function (instant) {
return new Date(instant);
};

exports.jsdate = function (parts) {
return createDate(parts.year, parts.month, parts.day, parts.hour, parts.minute, parts.second, parts.millisecond);
};
Expand Down
14 changes: 13 additions & 1 deletion src/Data/JSDate.purs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ module Data.JSDate
, fromDateTime
, toDateTime
, toDate
, fromInstant
, toInstant
, jsdate
, jsdateLocal
, now
, parse
, getTime
, getUTCDate
Expand Down Expand Up @@ -47,7 +49,7 @@ import Prelude

import Control.Monad.Eff (kind Effect, Eff)
import Control.Monad.Eff.Exception (EXCEPTION)

import Control.Monad.Eff.Now (NOW)
import Data.Date as Date
import Data.DateTime (DateTime(..), Date)
import Data.DateTime as DateTime
Expand Down Expand Up @@ -100,6 +102,9 @@ toDateTime = map Instant.toDateTime <$> toInstant
toDate :: JSDate -> Maybe Date
toDate = map DateTime.date <$> toDateTime

-- | Creates a `JSDate` from an `Instant` value.
foreign import fromInstant :: Instant -> JSDate

-- | Attempts to construct an `Instant` for a `JSDate`. `Nothing` is returned
-- | only when the date value is an invalid date.
toInstant :: JSDate -> Maybe Instant
Expand Down Expand Up @@ -151,6 +156,13 @@ foreign import dateMethod :: forall a. Fn2 String JSDate a
foreign import parse
:: forall eff. String -> Eff (locale :: LOCALE | eff) JSDate

-- | Gets a `JSDate` value for the date and time according to the current
-- | machine's clock.
-- |
-- | Unless a `JSDate` is required specifically, consider using the functions in
-- | `Control.Monad.Eff.Now` instead.
foreign import now :: forall eff. Eff (now :: NOW | eff) JSDate

-- | Returns the date as a number of milliseconds since 1970-01-01 00:00:00 UTC.
getTime :: JSDate -> Number
getTime dt = runFn2 dateMethod "getTime" dt
Expand Down

0 comments on commit 778624f

Please sign in to comment.