{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}

module Data.HodaTime.CalendarDateTime.Internal
(
   DayNth(..)
  ,Year
  ,WeekNumber
  ,DayOfMonth
  ,CalendarDate
  ,CalendarDateTime(..)
  ,IsCalendar(..)
  ,HasDate(..)
  ,LocalTime(..)
  ,IsCalendarDateTime(..)
  ,at
)
where

import Data.HodaTime.Instant.Internal (Instant)
import Data.Int (Int32)
import Data.Word (Word8, Word32)
import Control.DeepSeq (NFData(..))
import Data.Hashable (Hashable(..))

-- $setup
-- >>> import Data.Maybe (fromJust)
-- >>> import qualified Data.HodaTime.Calendar.Gregorian as Gregorian
-- >>> import Data.HodaTime.Calendar.Gregorian (Month(..), DayOfWeek(..))

-- CalendarDate

-- | Used by several smart constructors to chose a day relative to the start or end of the month.
data DayNth =
    FourthToLast
  | ThirdToLast
  | SecondToLast
  | Last
  | First
  | Second
  | Third
  | Fourth
  | Fifth
  deriving (DayNth -> DayNth -> Bool
(DayNth -> DayNth -> Bool)
-> (DayNth -> DayNth -> Bool) -> Eq DayNth
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DayNth -> DayNth -> Bool
== :: DayNth -> DayNth -> Bool
$c/= :: DayNth -> DayNth -> Bool
/= :: DayNth -> DayNth -> Bool
Eq, Int -> DayNth -> ShowS
[DayNth] -> ShowS
DayNth -> String
(Int -> DayNth -> ShowS)
-> (DayNth -> String) -> ([DayNth] -> ShowS) -> Show DayNth
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DayNth -> ShowS
showsPrec :: Int -> DayNth -> ShowS
$cshow :: DayNth -> String
show :: DayNth -> String
$cshowList :: [DayNth] -> ShowS
showList :: [DayNth] -> ShowS
Show, Int -> DayNth
DayNth -> Int
DayNth -> [DayNth]
DayNth -> DayNth
DayNth -> DayNth -> [DayNth]
DayNth -> DayNth -> DayNth -> [DayNth]
(DayNth -> DayNth)
-> (DayNth -> DayNth)
-> (Int -> DayNth)
-> (DayNth -> Int)
-> (DayNth -> [DayNth])
-> (DayNth -> DayNth -> [DayNth])
-> (DayNth -> DayNth -> [DayNth])
-> (DayNth -> DayNth -> DayNth -> [DayNth])
-> Enum DayNth
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: DayNth -> DayNth
succ :: DayNth -> DayNth
$cpred :: DayNth -> DayNth
pred :: DayNth -> DayNth
$ctoEnum :: Int -> DayNth
toEnum :: Int -> DayNth
$cfromEnum :: DayNth -> Int
fromEnum :: DayNth -> Int
$cenumFrom :: DayNth -> [DayNth]
enumFrom :: DayNth -> [DayNth]
$cenumFromThen :: DayNth -> DayNth -> [DayNth]
enumFromThen :: DayNth -> DayNth -> [DayNth]
$cenumFromTo :: DayNth -> DayNth -> [DayNth]
enumFromTo :: DayNth -> DayNth -> [DayNth]
$cenumFromThenTo :: DayNth -> DayNth -> DayNth -> [DayNth]
enumFromThenTo :: DayNth -> DayNth -> DayNth -> [DayNth]
Enum)

type Year = Int
type DayOfMonth = Int
type WeekNumber = Int

-- | A calendar date in the calendar system @cal@.  This is a public synonym for the per-calendar representation
--   'Date': each calendar defines its own @data instance Date cal@ (see 'IsCalendar'), so unrelated calendars
--   (e.g. Gregorian and Hebrew) need share nothing in how a date is stored.
type CalendarDate cal = Date cal

class IsCalendar cal where
  -- | The per-calendar date representation.  Each calendar picks whatever packing is most natural/efficient for it.
  data Date cal
  data DayOfWeek cal
  data Month cal
  -- | Build a date from a flat, epoch-relative day count (the calendar's own epoch).
  fromDays :: Int32 -> Date cal
  -- | Extract the flat, epoch-relative day count from a date.
  toDays :: Date cal -> Int32
  -- | Decode a date to @(year, zero-based month, day-of-month)@.  The year is signed so calendars can represent
  --   pre-epoch (e.g. BC) years without wraparound.
  toYmd :: Date cal -> (Int32, Word8, Word8)
  -- | The calendar's display name (e.g. @"Gregorian"@), used by 'Show' to render a date as the
  --   smart-constructor call that builds it.
  calendarName :: Date cal -> String
  day' :: Date cal -> DayOfMonth
  setDay' :: DayOfMonth -> Date cal -> Date cal
  month' :: Date cal -> Month cal
  setMonthIndex' :: Int -> Date cal -> Date cal
  year' :: Date cal -> Year
  setYear' :: Year -> Date cal -> Date cal
  dayOfWeek' :: Date cal -> DayOfWeek cal
  next' :: Int -> DayOfWeek cal -> Date cal -> Date cal
  previous' :: Int -> DayOfWeek cal -> Date cal -> Date cal

class HasDate d where
  type DoW d
  type MoY d
  -- | Day-of-month component.
  day :: d -> DayOfMonth
  setDay :: DayOfMonth -> d -> d
  -- | Accessor for the Month component of a 'HasDate'.
  month :: d -> MoY d
  setMonthIndex :: Int -> d -> d
  -- | Year component.
  year :: d -> Year
  setYear :: Year -> d -> d
  -- | Accessor for the Day of the week enum of a 'HasDate', for example:
  --
  -- >>> dayOfWeek . fromJust $ Gregorian.calendarDate 31 January 2000
  -- Monday
  dayOfWeek :: d -> DoW d
  -- | Returns a 'HasDate' shifted to the nth next Day of Week from the current 'HasDate', for example:
  --
  -- >>> next 1 Monday . fromJust $ Gregorian.calendarDate 31 January 2000
  -- fromJust (Gregorian.calendarDate 7 February 2000)
  next :: Int -> DoW d -> d -> d
  -- | Returns a 'HasDate' shifted to the nth previous Day of Week from the current 'HasDate', for example:
  --
  -- >>> previous 1 Monday . fromJust $ Gregorian.calendarDate 31 January 2000
  -- fromJust (Gregorian.calendarDate 24 January 2000)
  previous :: Int -> DoW d -> d -> d
  -- | Access the year, month and day-of-month components together in a single call, returned as a
  --   @(year, month, day)@ tuple.
  --
  --   This is purely an access optimization for code that needs more than one date component at once.  Reading the
  --   components individually with 'year', 'month' and 'day' is perfectly correct, but for a packed representation
  --   (such as the Gregorian 'Date', which stores a cycle\/century\/day-in-century triple) each of those accessors
  --   independently decodes the stored value, so asking for all three separately decodes it three times.
  --   'yearMonthDay' decodes once and hands back every component, which is noticeably cheaper on hot paths (for
  --   example date formatting).  For representations that already store the components separately (such as the Julian
  --   'Date') there is nothing to decode and this is simply the three field reads, so it is never slower than the
  --   individual accessors and callers can use it unconditionally.
  yearMonthDay :: d -> (Year, MoY d, DayOfMonth)
  yearMonthDay d
d = (d -> Int
forall d. HasDate d => d -> Int
year d
d, d -> MoY d
forall d. HasDate d => d -> MoY d
month d
d, d -> Int
forall d. HasDate d => d -> Int
day d
d)

instance (IsCalendar cal) => HasDate (Date cal) where
  type DoW (Date cal) = DayOfWeek cal
  type MoY (Date cal) = Month cal
  day :: Date cal -> Int
day = Date cal -> Int
forall cal. IsCalendar cal => Date cal -> Int
day'
  setDay :: Int -> Date cal -> Date cal
setDay = Int -> Date cal -> Date cal
forall cal. IsCalendar cal => Int -> Date cal -> Date cal
setDay'
  month :: Date cal -> MoY (Date cal)
month = Date cal -> MoY (Date cal)
Date cal -> Month cal
forall cal. IsCalendar cal => Date cal -> Month cal
month'
  setMonthIndex :: Int -> Date cal -> Date cal
setMonthIndex = Int -> Date cal -> Date cal
forall cal. IsCalendar cal => Int -> Date cal -> Date cal
setMonthIndex'
  year :: Date cal -> Int
year = Date cal -> Int
forall cal. IsCalendar cal => Date cal -> Int
year'
  setYear :: Int -> Date cal -> Date cal
setYear = Int -> Date cal -> Date cal
forall cal. IsCalendar cal => Int -> Date cal -> Date cal
setYear'
  dayOfWeek :: Date cal -> DoW (Date cal)
dayOfWeek = Date cal -> DoW (Date cal)
Date cal -> DayOfWeek cal
forall cal. IsCalendar cal => Date cal -> DayOfWeek cal
dayOfWeek'
  next :: Int -> DoW (Date cal) -> Date cal -> Date cal
next = Int -> DoW (Date cal) -> Date cal -> Date cal
Int -> DayOfWeek cal -> Date cal -> Date cal
forall cal.
IsCalendar cal =>
Int -> DayOfWeek cal -> Date cal -> Date cal
next'
  previous :: Int -> DoW (Date cal) -> Date cal -> Date cal
previous = Int -> DoW (Date cal) -> Date cal -> Date cal
Int -> DayOfWeek cal -> Date cal -> Date cal
forall cal.
IsCalendar cal =>
Int -> DayOfWeek cal -> Date cal -> Date cal
previous'

-- | Renders the (partial) smart-constructor call that produces a date, e.g. @Gregorian.calendarDate 31 March 2000@,
--   without the surrounding 'fromJust'.  Shared by the 'Show' instances for 'Date' and 'CalendarDateTime'.
showsDateCon :: (IsCalendar cal, Show (Month cal)) => Date cal -> ShowS
showsDateCon :: forall cal. (IsCalendar cal, Show (Month cal)) => Date cal -> ShowS
showsDateCon Date cal
date =
    String -> ShowS
showString (Date cal -> String
forall cal. IsCalendar cal => Date cal -> String
calendarName Date cal
date) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
".calendarDate "
  ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 (Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
dom :: Int) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
' '
  ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Month cal -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 (Date cal -> Month cal
forall cal. IsCalendar cal => Date cal -> Month cal
month' Date cal
date) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
' '
  ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 (Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
yr :: Int)
  where (Int32
yr, Word8
_m, Word8
dom) = Date cal -> (Int32, Word8, Word8)
forall cal. IsCalendar cal => Date cal -> (Int32, Word8, Word8)
toYmd Date cal
date

-- | Renders a date as the (honest) smart-constructor call that produces it, e.g.
--   @fromJust (Gregorian.calendarDate 31 March 2000)@.  This is a debug rendering, not code to paste back
--   verbatim (the calendar qualifier depends on how you imported it, and 'calendarDate' returns 'Maybe') \- it
--   is meant to tell you exactly what the value is.
instance (IsCalendar cal, Show (Month cal)) => Show (Date cal) where
  showsPrec :: Int -> Date cal -> ShowS
showsPrec Int
p Date cal
date = Bool -> ShowS -> ShowS
showParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ String -> ShowS
showString String
"fromJust (" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Date cal -> ShowS
forall cal. (IsCalendar cal, Show (Month cal)) => Date cal -> ShowS
showsDateCon Date cal
date ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
')'

-- LocalTime

-- | Represents a specific time of day with no reference to any calendar, date or time zone.
data LocalTime = LocalTime { LocalTime -> Word32
ltSecs :: Word32, LocalTime -> Word32
ltNsecs :: Word32 }
  deriving (LocalTime -> LocalTime -> Bool
(LocalTime -> LocalTime -> Bool)
-> (LocalTime -> LocalTime -> Bool) -> Eq LocalTime
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: LocalTime -> LocalTime -> Bool
== :: LocalTime -> LocalTime -> Bool
$c/= :: LocalTime -> LocalTime -> Bool
/= :: LocalTime -> LocalTime -> Bool
Eq, Eq LocalTime
Eq LocalTime =>
(LocalTime -> LocalTime -> Ordering)
-> (LocalTime -> LocalTime -> Bool)
-> (LocalTime -> LocalTime -> Bool)
-> (LocalTime -> LocalTime -> Bool)
-> (LocalTime -> LocalTime -> Bool)
-> (LocalTime -> LocalTime -> LocalTime)
-> (LocalTime -> LocalTime -> LocalTime)
-> Ord LocalTime
LocalTime -> LocalTime -> Bool
LocalTime -> LocalTime -> Ordering
LocalTime -> LocalTime -> LocalTime
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: LocalTime -> LocalTime -> Ordering
compare :: LocalTime -> LocalTime -> Ordering
$c< :: LocalTime -> LocalTime -> Bool
< :: LocalTime -> LocalTime -> Bool
$c<= :: LocalTime -> LocalTime -> Bool
<= :: LocalTime -> LocalTime -> Bool
$c> :: LocalTime -> LocalTime -> Bool
> :: LocalTime -> LocalTime -> Bool
$c>= :: LocalTime -> LocalTime -> Bool
>= :: LocalTime -> LocalTime -> Bool
$cmax :: LocalTime -> LocalTime -> LocalTime
max :: LocalTime -> LocalTime -> LocalTime
$cmin :: LocalTime -> LocalTime -> LocalTime
min :: LocalTime -> LocalTime -> LocalTime
Ord)

-- | Renders the (partial) smart-constructor call that produces a 'LocalTime', e.g. @localTime 4 30 0 0@, without
--   the surrounding 'fromJust'.  Shared by the 'Show' instances for 'LocalTime' and 'CalendarDateTime'.
showsLocalTimeCon :: LocalTime -> ShowS
showsLocalTimeCon :: LocalTime -> ShowS
showsLocalTimeCon (LocalTime Word32
secs Word32
nsecs) =
    String -> ShowS
showString String
"localTime "
  ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 Int
h ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
' ' ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 Int
m ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
' '
  ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 Int
s ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
' ' ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 (Word32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
nsecs :: Int)
  where
    (Int
h, Int
r) = (Word32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
secs :: Int) Int -> Int -> (Int, Int)
forall a. Integral a => a -> a -> (a, a)
`divMod` Int
3600
    (Int
m, Int
s) = Int
r Int -> Int -> (Int, Int)
forall a. Integral a => a -> a -> (a, a)
`divMod` Int
60

instance Show LocalTime where
  showsPrec :: Int -> LocalTime -> ShowS
showsPrec Int
p LocalTime
lt = Bool -> ShowS -> ShowS
showParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ String -> ShowS
showString String
"fromJust (" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LocalTime -> ShowS
showsLocalTimeCon LocalTime
lt ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
')'

instance NFData LocalTime where
  rnf :: LocalTime -> ()
rnf (LocalTime Word32
secs Word32
nsecs) = Word32 -> ()
forall a. NFData a => a -> ()
rnf Word32
secs () -> () -> ()
forall a b. a -> b -> b
`seq` Word32 -> ()
forall a. NFData a => a -> ()
rnf Word32
nsecs

instance Hashable LocalTime where
  hashWithSalt :: Int -> LocalTime -> Int
hashWithSalt Int
s (LocalTime Word32
secs Word32
nsecs) = Int
s Int -> Word32 -> Int
forall a. Hashable a => Int -> a -> Int
`hashWithSalt` Word32
secs Int -> Word32 -> Int
forall a. Hashable a => Int -> a -> Int
`hashWithSalt` Word32
nsecs

-- CalendarDateTime

-- | Represents a specific date and time within its calendar system.  NOTE: a CalendarDateTime does
--   *not* represent a specific time on the global time line because e.g. "10.March.2006 4pm" is a different instant
--   in most time zones.  Convert it to a ZonedDateTime first if you wish to convert to an instant (or use a convenience
--   function).
data CalendarDateTime calendar = CalendarDateTime (Date calendar) LocalTime

deriving instance Eq (Date cal) => Eq (CalendarDateTime cal)
deriving instance Ord (Date cal) => Ord (CalendarDateTime cal)

-- | Renders a 'CalendarDateTime' as the applicative construction that produces it, e.g.
--   @fromJust (at \<$\> Gregorian.calendarDate 31 March 2000 \<*\> localTime 4 30 0 0)@.  A single 'fromJust'
--   wraps the whole thing because that is how one actually builds the value from the (partial) smart constructors.
instance (IsCalendar cal, Show (Month cal)) => Show (CalendarDateTime cal) where
  showsPrec :: Int -> CalendarDateTime cal -> ShowS
showsPrec Int
p (CalendarDateTime Date cal
d LocalTime
lt) = Bool -> ShowS -> ShowS
showParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
      String -> ShowS
showString String
"fromJust (at <$> " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Date cal -> ShowS
forall cal. (IsCalendar cal, Show (Month cal)) => Date cal -> ShowS
showsDateCon Date cal
d ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
" <*> " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LocalTime -> ShowS
showsLocalTimeCon LocalTime
lt ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
')'

instance NFData (Date cal) => NFData (CalendarDateTime cal) where
  rnf :: CalendarDateTime cal -> ()
rnf (CalendarDateTime Date cal
d LocalTime
lt) = Date cal -> ()
forall a. NFData a => a -> ()
rnf Date cal
d () -> () -> ()
forall a b. a -> b -> b
`seq` LocalTime -> ()
forall a. NFData a => a -> ()
rnf LocalTime
lt

instance Hashable (Date cal) => Hashable (CalendarDateTime cal) where
  hashWithSalt :: Int -> CalendarDateTime cal -> Int
hashWithSalt Int
s (CalendarDateTime Date cal
d LocalTime
lt) = Int
s Int -> Date cal -> Int
forall a. Hashable a => Int -> a -> Int
`hashWithSalt` Date cal
d Int -> LocalTime -> Int
forall a. Hashable a => Int -> a -> Int
`hashWithSalt` LocalTime
lt

instance (IsCalendar cal) => HasDate (CalendarDateTime cal) where
  type DoW (CalendarDateTime cal) = DayOfWeek cal
  type MoY (CalendarDateTime cal) = Month cal
  day :: CalendarDateTime cal -> Int
day (CalendarDateTime Date cal
cd LocalTime
_) = Date cal -> Int
forall d. HasDate d => d -> Int
day Date cal
cd
  setDay :: Int -> CalendarDateTime cal -> CalendarDateTime cal
setDay Int
value (CalendarDateTime Date cal
cd LocalTime
lt) = Date cal -> LocalTime -> CalendarDateTime cal
forall calendar.
Date calendar -> LocalTime -> CalendarDateTime calendar
CalendarDateTime (Int -> Date cal -> Date cal
forall d. HasDate d => Int -> d -> d
setDay Int
value Date cal
cd) LocalTime
lt
  month :: CalendarDateTime cal -> MoY (CalendarDateTime cal)
month (CalendarDateTime Date cal
cd LocalTime
_) = Date cal -> MoY (Date cal)
forall d. HasDate d => d -> MoY d
month Date cal
cd
  setMonthIndex :: Int -> CalendarDateTime cal -> CalendarDateTime cal
setMonthIndex Int
value (CalendarDateTime Date cal
cd LocalTime
lt) = Date cal -> LocalTime -> CalendarDateTime cal
forall calendar.
Date calendar -> LocalTime -> CalendarDateTime calendar
CalendarDateTime (Int -> Date cal -> Date cal
forall d. HasDate d => Int -> d -> d
setMonthIndex Int
value Date cal
cd) LocalTime
lt
  year :: CalendarDateTime cal -> Int
year (CalendarDateTime Date cal
cd LocalTime
_) = Date cal -> Int
forall d. HasDate d => d -> Int
year Date cal
cd
  setYear :: Int -> CalendarDateTime cal -> CalendarDateTime cal
setYear Int
value (CalendarDateTime Date cal
cd LocalTime
lt) = Date cal -> LocalTime -> CalendarDateTime cal
forall calendar.
Date calendar -> LocalTime -> CalendarDateTime calendar
CalendarDateTime (Int -> Date cal -> Date cal
forall d. HasDate d => Int -> d -> d
setYear Int
value Date cal
cd) LocalTime
lt
  dayOfWeek :: CalendarDateTime cal -> DoW (CalendarDateTime cal)
dayOfWeek (CalendarDateTime Date cal
cd LocalTime
_) = Date cal -> DoW (Date cal)
forall d. HasDate d => d -> DoW d
dayOfWeek Date cal
cd
  next :: Int
-> DoW (CalendarDateTime cal)
-> CalendarDateTime cal
-> CalendarDateTime cal
next Int
i DoW (CalendarDateTime cal)
dow (CalendarDateTime Date cal
cd LocalTime
lt) = Date cal -> LocalTime -> CalendarDateTime cal
forall calendar.
Date calendar -> LocalTime -> CalendarDateTime calendar
CalendarDateTime (Int -> DoW (Date cal) -> Date cal -> Date cal
forall d. HasDate d => Int -> DoW d -> d -> d
next Int
i DoW (CalendarDateTime cal)
DoW (Date cal)
dow Date cal
cd) LocalTime
lt
  previous :: Int
-> DoW (CalendarDateTime cal)
-> CalendarDateTime cal
-> CalendarDateTime cal
previous Int
i DoW (CalendarDateTime cal)
dow (CalendarDateTime Date cal
cd LocalTime
lt) = Date cal -> LocalTime -> CalendarDateTime cal
forall calendar.
Date calendar -> LocalTime -> CalendarDateTime calendar
CalendarDateTime (Int -> DoW (Date cal) -> Date cal -> Date cal
forall d. HasDate d => Int -> DoW d -> d -> d
previous Int
i DoW (CalendarDateTime cal)
DoW (Date cal)
dow Date cal
cd) LocalTime
lt

-- | Private class used to allow conversions to and from CalendarDateTime for a given calendar.  If you see this in the documentation, consider it a bug
class IsCalendarDateTime cal where
  -- | Convert an Instant which has already been converted to the correct time for the Calendar and TimeZone into CalendarDateTime
  fromAdjustedInstant :: Instant -> CalendarDateTime cal
  -- | Convert a CalendarDateTime directly to an Instant.  Needed because different calendars use different epochs.  If this ever changes we can revisit this
  toUnadjustedInstant :: CalendarDateTime cal -> Instant

-- constructors

-- | Returns a 'CalendarDateTime' of the 'CalendarDate' at the given 'LocalTime'
at :: Date cal -> LocalTime -> CalendarDateTime cal
at :: forall calendar.
Date calendar -> LocalTime -> CalendarDateTime calendar
at = Date cal -> LocalTime -> CalendarDateTime cal
forall calendar.
Date calendar -> LocalTime -> CalendarDateTime calendar
CalendarDateTime