Skip to content

Commit

Permalink
Add Generic instances to all types with exposed constructors (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
AshleyYakeley committed Feb 29, 2024
1 parent 7cb9669 commit c6a718e
Show file tree
Hide file tree
Showing 15 changed files with 29 additions and 12 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [next]
- add Lift instances to all types
- add Generic instances to all types that have exposed constructors
- fix show of CalendarDiffTime
- fix diffGregorianDurationRollOver, diffJulianDurationRollOver

Expand Down
2 changes: 2 additions & 0 deletions lib/Data/Time/Calendar/CalendarDiffDays.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Data.Time.Calendar.CalendarDiffDays (

import Control.DeepSeq
import Data.Data
import GHC.Generics
import qualified Language.Haskell.TH.Syntax as TH

data CalendarDiffDays = CalendarDiffDays
Expand All @@ -21,6 +22,7 @@ data CalendarDiffDays = CalendarDiffDays
Typeable
, -- | @since 1.13.0
TH.Lift
, Generic
)

instance NFData CalendarDiffDays where
Expand Down
3 changes: 2 additions & 1 deletion lib/Data/Time/Calendar/Days.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ module Data.Time.Calendar.Days (
import Control.DeepSeq
import Data.Data
import Data.Ix
import GHC.Generics
import qualified Language.Haskell.TH.Syntax as TH

-- | The Modified Julian Day is a standard count of days, with zero being the day 1858-11-17.
newtype Day = ModifiedJulianDay
{ toModifiedJulianDay :: Integer
}
deriving (Eq, Ord, Data, Typeable, TH.Lift)
deriving (Eq, Ord, Data, Typeable, TH.Lift, Generic)

instance NFData Day where
rnf (ModifiedJulianDay a) = rnf a
Expand Down
3 changes: 2 additions & 1 deletion lib/Data/Time/Calendar/Month.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ import Data.Ix
import Data.Time.Calendar.Days
import Data.Time.Calendar.Gregorian
import Data.Time.Calendar.Private
import GHC.Generics
import qualified Language.Haskell.TH.Syntax as TH
import Text.ParserCombinators.ReadP
import Text.Read

-- | An absolute count of common calendar months.
-- Number is equal to @(year * 12) + (monthOfYear - 1)@.
newtype Month = MkMonth Integer deriving (Eq, Ord, Data, Typeable, TH.Lift)
newtype Month = MkMonth Integer deriving (Eq, Ord, Data, Typeable, TH.Lift, Generic)

instance NFData Month where
rnf (MkMonth m) = rnf m
Expand Down
5 changes: 3 additions & 2 deletions lib/Data/Time/Calendar/Quarter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import Data.Time.Calendar.Days
import Data.Time.Calendar.Month
import Data.Time.Calendar.Private
import Data.Time.Calendar.Types
import GHC.Generics
import qualified Language.Haskell.TH.Syntax as TH
import Text.ParserCombinators.ReadP
import Text.Read

-- | Quarters of each year. Each quarter corresponds to three months.
data QuarterOfYear = Q1 | Q2 | Q3 | Q4 deriving (Eq, Ord, Data, Typeable, Read, Show, Ix, TH.Lift)
data QuarterOfYear = Q1 | Q2 | Q3 | Q4 deriving (Eq, Ord, Data, Typeable, Read, Show, Ix, TH.Lift, Generic)

-- | maps Q1..Q4 to 1..4
instance Enum QuarterOfYear where
Expand All @@ -54,7 +55,7 @@ instance NFData QuarterOfYear where

-- | An absolute count of year quarters.
-- Number is equal to @(year * 4) + (quarterOfYear - 1)@.
newtype Quarter = MkQuarter Integer deriving (Eq, Ord, Data, Typeable)
newtype Quarter = MkQuarter Integer deriving (Eq, Ord, Data, Typeable, Generic)

instance NFData Quarter where
rnf (MkQuarter m) = rnf m
Expand Down
3 changes: 2 additions & 1 deletion lib/Data/Time/Calendar/Week.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Data.Data
import Data.Fixed
import Data.Ix
import Data.Time.Calendar.Days
import GHC.Generics
import qualified Language.Haskell.TH.Syntax as TH

data DayOfWeek
Expand All @@ -26,7 +27,7 @@ data DayOfWeek
| Friday
| Saturday
| Sunday
deriving (Eq, Show, Read, Data, Typeable, Ord, Ix, TH.Lift)
deriving (Eq, Show, Read, Data, Typeable, Ord, Ix, TH.Lift, Generic)

instance NFData DayOfWeek where
rnf Monday = ()
Expand Down
3 changes: 2 additions & 1 deletion lib/Data/Time/Clock/Internal/SystemTime.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Data.Data
import Data.Int (Int64)
import Data.Time.Clock.Internal.DiffTime
import Data.Word
import GHC.Generics
import qualified Language.Haskell.TH.Syntax as TH

#ifdef mingw32_HOST_OS
Expand All @@ -40,7 +41,7 @@ data SystemTime = MkSystemTime
{ systemSeconds :: {-# UNPACK #-} !Int64
, systemNanoseconds :: {-# UNPACK #-} !Word32
}
deriving (Eq, Ord, Show, Data, Typeable, TH.Lift)
deriving (Eq, Ord, Show, Data, Typeable, TH.Lift, Generic)

instance NFData SystemTime where
rnf a = a `seq` ()
Expand Down
3 changes: 2 additions & 1 deletion lib/Data/Time/Clock/Internal/UTCTime.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Control.DeepSeq
import Data.Data
import Data.Time.Calendar.Days
import Data.Time.Clock.Internal.DiffTime
import GHC.Generics
import qualified Language.Haskell.TH.Syntax as TH

-- | This is the simplest representation of UTC.
Expand All @@ -29,7 +30,7 @@ data UTCTime = UTCTime
, utctDayTime :: DiffTime
-- ^ the time from midnight, 0 <= t < 86401s (because of leap-seconds)
}
deriving (Data, Typeable, TH.Lift)
deriving (Data, Typeable, TH.Lift, Generic)

instance NFData UTCTime where
rnf (UTCTime d t) = rnf d `seq` rnf t `seq` ()
Expand Down
3 changes: 2 additions & 1 deletion lib/Data/Time/Clock/Internal/UniversalTime.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ module Data.Time.Clock.Internal.UniversalTime (

import Control.DeepSeq
import Data.Data
import GHC.Generics
import qualified Language.Haskell.TH.Syntax as TH

-- | The Modified Julian Date is the day with the fraction of the day, measured from UT midnight.
-- It's used to represent UT1, which is time as measured by the earth's rotation, adjusted for various wobbles.
newtype UniversalTime = ModJulianDate
{ getModJulianDate :: Rational
}
deriving (Eq, Ord, Data, Typeable, TH.Lift)
deriving (Eq, Ord, Data, Typeable, TH.Lift, Generic)

instance NFData UniversalTime where
rnf (ModJulianDate a) = rnf a
2 changes: 2 additions & 0 deletions lib/Data/Time/LocalTime/Internal/CalendarDiffTime.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Control.DeepSeq
import Data.Data
import Data.Time.Calendar.CalendarDiffDays
import Data.Time.Clock.Internal.NominalDiffTime
import GHC.Generics

data CalendarDiffTime = CalendarDiffTime
{ ctMonths :: Integer
Expand All @@ -20,6 +21,7 @@ data CalendarDiffTime = CalendarDiffTime
Data
, -- | @since 1.9.2
Typeable
, Generic
)

instance NFData CalendarDiffTime where
Expand Down
3 changes: 2 additions & 1 deletion lib/Data/Time/LocalTime/Internal/LocalTime.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import Data.Time.Clock.Internal.UTCTime
import Data.Time.Clock.Internal.UniversalTime
import Data.Time.LocalTime.Internal.TimeOfDay
import Data.Time.LocalTime.Internal.TimeZone
import GHC.Generics

-- | A simple day and time aggregate, where the day is of the specified parameter,
-- and the time is a TimeOfDay.
Expand All @@ -33,7 +34,7 @@ data LocalTime = LocalTime
{ localDay :: Day
, localTimeOfDay :: TimeOfDay
}
deriving (Eq, Ord, Data, Typeable)
deriving (Eq, Ord, Data, Typeable, Generic)

instance NFData LocalTime where
rnf (LocalTime d t) = rnf d `seq` rnf t `seq` ()
Expand Down
3 changes: 2 additions & 1 deletion lib/Data/Time/LocalTime/Internal/TimeOfDay.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import Data.Time.Calendar.Private
import Data.Time.Clock.Internal.DiffTime
import Data.Time.Clock.Internal.NominalDiffTime
import Data.Time.LocalTime.Internal.TimeZone
import GHC.Generics

-- | Time of day as represented in hour, minute and second (with picoseconds), typically used to express local time of day.
--
Expand All @@ -39,7 +40,7 @@ data TimeOfDay = TimeOfDay
-- ^ Note that 0 <= 'todSec' < 61, accomodating leap seconds.
-- Any local minute may have a leap second, since leap seconds happen in all zones simultaneously
}
deriving (Eq, Ord, Data, Typeable)
deriving (Eq, Ord, Data, Typeable, Generic)

instance NFData TimeOfDay where
rnf (TimeOfDay h m s) = rnf h `seq` rnf m `seq` rnf s `seq` ()
Expand Down
3 changes: 2 additions & 1 deletion lib/Data/Time/LocalTime/Internal/TimeZone.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Data.Time.Clock.POSIX
import Data.Time.Clock.System
import Foreign
import Foreign.C
import GHC.Generics

-- | A TimeZone is a whole number of minutes offset from UTC, together with a name and a \"just for summer\" flag.
data TimeZone = TimeZone
Expand All @@ -33,7 +34,7 @@ data TimeZone = TimeZone
, timeZoneName :: String
-- ^ The name of the zone, typically a three- or four-letter acronym.
}
deriving (Eq, Ord, Data, Typeable)
deriving (Eq, Ord, Data, Typeable, Generic)

instance NFData TimeZone where
rnf (TimeZone m so n) = rnf m `seq` rnf so `seq` rnf n `seq` ()
Expand Down
3 changes: 2 additions & 1 deletion lib/Data/Time/LocalTime/Internal/ZonedTime.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Data.Time.Clock.Internal.UTCTime
import Data.Time.Clock.POSIX
import Data.Time.LocalTime.Internal.LocalTime
import Data.Time.LocalTime.Internal.TimeZone
import GHC.Generics

-- | A local time together with a time zone.
--
Expand All @@ -26,7 +27,7 @@ data ZonedTime = ZonedTime
{ zonedTimeToLocalTime :: LocalTime
, zonedTimeZone :: TimeZone
}
deriving (Data, Typeable)
deriving (Data, Typeable, Generic)

instance NFData ZonedTime where
rnf (ZonedTime lt z) = rnf lt `seq` rnf z `seq` ()
Expand Down
1 change: 1 addition & 0 deletions time.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ library
Rank2Types
DeriveDataTypeable
DeriveLift
DeriveGeneric
StandaloneDeriving
PatternSynonyms
ViewPatterns
Expand Down

0 comments on commit c6a718e

Please sign in to comment.