diff --git a/bower.json b/bower.json index f58f7ee..60bc4b2 100644 --- a/bower.json +++ b/bower.json @@ -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", diff --git a/src/Data/JSDate.js b/src/Data/JSDate.js index 9601449..0e9de62 100644 --- a/src/Data/JSDate.js +++ b/src/Data/JSDate.js @@ -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()); }; @@ -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); }; diff --git a/src/Data/JSDate.purs b/src/Data/JSDate.purs index 3f69e8c..e756cea 100644 --- a/src/Data/JSDate.purs +++ b/src/Data/JSDate.purs @@ -14,9 +14,11 @@ module Data.JSDate , fromDateTime , toDateTime , toDate + , fromInstant , toInstant , jsdate , jsdateLocal + , now , parse , getTime , getUTCDate @@ -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 @@ -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 @@ -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