Skip to content

Add JSDate-specific now, add fromInstant #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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