From c452646c57a0bb63893d4248b55331517e91bab3 Mon Sep 17 00:00:00 2001 From: Robert Klotzner Date: Thu, 24 Mar 2016 18:34:32 +0100 Subject: [PATCH] Added Generic instance for Date + added two toString functions --- bower.json | 3 ++- src/Data/Date.purs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/bower.json b/bower.json index 499acf0..fe2535a 100644 --- a/bower.json +++ b/bower.json @@ -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" } } diff --git a/src/Data/Date.purs b/src/Data/Date.purs index 6711739..775ac3f 100644 --- a/src/Data/Date.purs +++ b/src/Data/Date.purs @@ -7,6 +7,8 @@ module Data.Date , toEpochMilliseconds , fromString , fromStringStrict + , toDateString + , toISOString , Now() , now , nowEpochMilliseconds @@ -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(..)) @@ -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 @@ -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 :: !