Skip to content

Derive generic instances for Data.Date.Date and all types in Data.Time #22

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 3 commits 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.0"
}
}
9 changes: 9 additions & 0 deletions src/Data/Date.purs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ module Data.Date

import Prelude

import Type.Proxy

import Control.Monad.Eff
import Data.Enum (Enum, Cardinality(..), fromEnum, defaultSucc, defaultPred)
import Data.Function (on, Fn2(), runFn2, Fn3(), runFn3)
import Data.Maybe (Maybe(..))
import Data.Time
import Data.Generic

-- | A native JavaScript `Date` object.
foreign import data JSDate :: *
Expand All @@ -36,6 +39,12 @@ foreign import data JSDate :: *
-- | modules.
newtype Date = DateTime JSDate

instance genericDate :: Generic Date where
toSpine d = SProd "Date" [const (toSpine $ toEpochMilliseconds d)]
toSignature _ = SigProd "Date" [{ sigConstructor: "Date", sigValues: [const $ toSignature (Proxy :: Proxy Milliseconds)] }]
fromSpine (SProd "Date" [msf]) = fromSpine (msf unit) >>= fromEpochMilliseconds
fromSpine _ = Nothing

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

Expand Down
18 changes: 18 additions & 0 deletions src/Data/Time.purs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ import Prelude
, compare
, show )

import Data.Generic

-- | An hour component from a time value. Should fall between 0 and 23
-- | inclusive.
newtype HourOfDay = HourOfDay Int

derive instance genericHourOfDay :: Generic HourOfDay

instance eqHourOfDay :: Eq HourOfDay where
eq (HourOfDay x) (HourOfDay y) = x == y

Expand All @@ -31,6 +35,8 @@ instance ordHourOfDay :: Ord HourOfDay where
-- | A quantity of hours (not necessarily a value between 0 and 23).
newtype Hours = Hours Number

derive instance genericHours :: Generic Hours

instance eqHours :: Eq Hours where
eq (Hours x) (Hours y) = x == y

Expand Down Expand Up @@ -61,6 +67,8 @@ instance showHours :: Show Hours where
-- | inclusive.
newtype MinuteOfHour = MinuteOfHour Int

derive instance genericMinuteOfHour :: Generic MinuteOfHour

instance eqMinuteOfHour :: Eq MinuteOfHour where
eq (MinuteOfHour x) (MinuteOfHour y) = x == y

Expand All @@ -70,6 +78,8 @@ instance ordMinuteOfHour :: Ord MinuteOfHour where
-- | A quantity of minutes (not necessarily a value between 0 and 60).
newtype Minutes = Minutes Number

derive instance genericMinutes :: Generic Minutes

instance eqMinutes :: Eq Minutes where
eq (Minutes x) (Minutes y) = x == y

Expand Down Expand Up @@ -100,6 +110,8 @@ instance showMinutes :: Show Minutes where
-- | inclusive.
newtype SecondOfMinute = SecondOfMinute Int

derive instance genericSecondOfMinute :: Generic SecondOfMinute

instance eqSecondOfMinute :: Eq SecondOfMinute where
eq (SecondOfMinute x) (SecondOfMinute y) = x == y

Expand All @@ -109,6 +121,8 @@ instance ordSecondOfMinute :: Ord SecondOfMinute where
-- | A quantity of seconds (not necessarily a value between 0 and 60).
newtype Seconds = Seconds Number

derive instance genericSeconds :: Generic Seconds

instance eqSeconds :: Eq Seconds where
eq (Seconds x) (Seconds y) = x == y

Expand Down Expand Up @@ -139,6 +153,8 @@ instance showSeconds :: Show Seconds where
-- | inclusive.
newtype MillisecondOfSecond = MillisecondOfSecond Int

derive instance genericMillisecondOfSecond :: Generic MillisecondOfSecond

instance eqMillisecondOfSecond :: Eq MillisecondOfSecond where
eq (MillisecondOfSecond x) (MillisecondOfSecond y) = x == y

Expand All @@ -148,6 +164,8 @@ instance ordMillisecondOfSecond :: Ord MillisecondOfSecond where
-- | A quantity of milliseconds (not necessarily a value between 0 and 1000).
newtype Milliseconds = Milliseconds Number

derive instance genericMilliseconds :: Generic Milliseconds

instance eqMilliseconds :: Eq Milliseconds where
eq (Milliseconds x) (Milliseconds y) = x == y

Expand Down