Skip to content

Added Generic instance for Date #27

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -26,6 +26,7 @@
"dependencies": {
"purescript-enums": "^0.7.0",
"purescript-functions": "^0.1.0",
"purescript-globals": "^0.2.0"
"purescript-globals": "^0.2.0",
"purescript-generics": "^0.7.2"
}
}
17 changes: 17 additions & 0 deletions src/Data/Date.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module Data.Date
, toEpochMilliseconds
, fromString
, fromStringStrict
, toDateString
, toISOString
, Now()
, now
, nowEpochMilliseconds
Expand All @@ -21,6 +23,7 @@ module Data.Date
import Prelude

import Control.Monad.Eff
import Data.Generic (class Generic, GenericSignature(..), GenericSpine(..))
import Data.Enum (Enum, Cardinality(..), fromEnum, defaultSucc, defaultPred)
import Data.Function (on, Fn2(), runFn2, Fn3(), runFn3)
import Data.Maybe (Maybe(..))
Expand All @@ -36,6 +39,12 @@ foreign import data JSDate :: *
-- | modules.
newtype Date = DateTime JSDate

instance genericDate :: Generic Date where
fromSpine (SString str) = fromString str
fromSpine _ = Nothing
toSpine = SString <<< toISOString
toSignature _ = SigString

instance eqDate :: Eq Date where
eq = eq `on` toEpochMilliseconds

Expand Down Expand Up @@ -79,6 +88,14 @@ fromString = fromJSDate <<< jsDateConstructor
fromStringStrict :: String -> Maybe Date
fromStringStrict s = runFn3 strictJsDate Just Nothing s >>= fromJSDate

-- | Returns the "date" portion of `Date` as a human-readable string.
toDateString :: Date -> String
toDateString (DateTime d) = runFn2 jsDateMethod "toDateString" d

-- | Converts a date to a string following the ISO 8601 Extended Format.
toISOString :: Date -> String
toISOString (DateTime d) = runFn2 jsDateMethod "toISOString" d

-- | Effect type for when accessing the current date/time.
foreign import data Now :: !

Expand Down