{-# 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.Functor.Const (Const(..))
import Data.Int (Int32)
import Data.Word (Word8, Word32)

-- 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)
  day' :: Functor f => (DayOfMonth -> f DayOfMonth) -> Date cal -> f (Date cal)
  month' :: Date cal -> Month cal
  monthl' :: Functor f => (Int -> f Int) -> Date cal -> f (Date cal)
  year' :: Functor f => (Year -> f Year) -> Date cal -> f (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
  -- | Lens for the day component of a 'HasDate'.  Please note that days are not clamped: if you add e.g. 400 days then the month and year will roll
  day :: Functor f => (DayOfMonth -> f DayOfMonth) -> d -> f d
  -- | Accessor for the Month component of a 'HasDate'.
  month :: d -> MoY d
  -- | Lens for interacting with the month component of a 'HasDate'.  Please note that we convert the month to an Int so meaningful math can be done on it.  Also
  --   please note that the day will be unaffected except in the case of "end of month" days which may clamp.  Note that this clamping will only occur as a final step,
  --   so that
  --
  --   >>> modify monthl (+ 2) <$> Gregorian.calendarDate 31 January 2000
  --   Just (CalendarDate 31 March 2000)
  --
  --   and not 29th of March as would happen with some libraries.
  monthl :: Functor f => (Int -> f Int) -> d -> f d
  -- | Lens for the year component of a 'HasDate'.  Please note that the rest of the date is left as is, with two exceptions: Feb 29 will clamp to 28 in a non-leapyear
  --   and if the new year is earlier than the earliest supported year it will clamp back to that year
  year :: Functor f => (Year -> f Year) -> d -> f 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
  -- 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
  -- 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 = (Const Int d -> Int
forall {k} a (b :: k). Const a b -> a
getConst ((Int -> Const Int Int) -> d -> Const Int d
forall d (f :: * -> *).
(HasDate d, Functor f) =>
(Int -> f Int) -> d -> f d
forall (f :: * -> *). Functor f => (Int -> f Int) -> d -> f d
year Int -> Const Int Int
forall {k} a (b :: k). a -> Const a b
Const d
d), d -> MoY d
forall d. HasDate d => d -> MoY d
month d
d, Const Int d -> Int
forall {k} a (b :: k). Const a b -> a
getConst ((Int -> Const Int Int) -> d -> Const Int d
forall d (f :: * -> *).
(HasDate d, Functor f) =>
(Int -> f Int) -> d -> f d
forall (f :: * -> *). Functor f => (Int -> f Int) -> d -> f d
day Int -> Const Int Int
forall {k} a (b :: k). a -> Const a b
Const d
d))

instance (IsCalendar cal) => HasDate (Date cal) where
  type DoW (Date cal) = DayOfWeek cal
  type MoY (Date cal) = Month cal
  day :: forall (f :: * -> *).
Functor f =>
(Int -> f Int) -> Date cal -> f (Date cal)
day = (Int -> f Int) -> Date cal -> f (Date cal)
forall cal (f :: * -> *).
(IsCalendar cal, Functor f) =>
(Int -> f Int) -> Date cal -> f (Date cal)
forall (f :: * -> *).
Functor f =>
(Int -> f Int) -> Date cal -> f (Date cal)
day'
  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'
  monthl :: forall (f :: * -> *).
Functor f =>
(Int -> f Int) -> Date cal -> f (Date cal)
monthl = (Int -> f Int) -> Date cal -> f (Date cal)
forall cal (f :: * -> *).
(IsCalendar cal, Functor f) =>
(Int -> f Int) -> Date cal -> f (Date cal)
forall (f :: * -> *).
Functor f =>
(Int -> f Int) -> Date cal -> f (Date cal)
monthl'
  year :: forall (f :: * -> *).
Functor f =>
(Int -> f Int) -> Date cal -> f (Date cal)
year = (Int -> f Int) -> Date cal -> f (Date cal)
forall cal (f :: * -> *).
(IsCalendar cal, Functor f) =>
(Int -> f Int) -> Date cal -> f (Date cal)
forall (f :: * -> *).
Functor f =>
(Int -> f Int) -> Date cal -> f (Date cal)
year'
  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'

-- 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, Int -> LocalTime -> ShowS
[LocalTime] -> ShowS
LocalTime -> String
(Int -> LocalTime -> ShowS)
-> (LocalTime -> String)
-> ([LocalTime] -> ShowS)
-> Show LocalTime
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> LocalTime -> ShowS
showsPrec :: Int -> LocalTime -> ShowS
$cshow :: LocalTime -> String
show :: LocalTime -> String
$cshowList :: [LocalTime] -> ShowS
showList :: [LocalTime] -> ShowS
Show)    -- TODO: Remove Show

-- 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)
deriving instance Show (Date cal) => Show (CalendarDateTime cal)

instance (IsCalendar cal) => HasDate (CalendarDateTime cal) where
  type DoW (CalendarDateTime cal) = DayOfWeek cal
  type MoY (CalendarDateTime cal) = Month cal
  day :: forall (f :: * -> *).
Functor f =>
(Int -> f Int) -> CalendarDateTime cal -> f (CalendarDateTime cal)
day Int -> f Int
f (CalendarDateTime Date cal
cd LocalTime
lt) = (Date cal -> LocalTime -> CalendarDateTime cal)
-> LocalTime -> Date cal -> CalendarDateTime cal
forall a b c. (a -> b -> c) -> b -> a -> c
flip Date cal -> LocalTime -> CalendarDateTime cal
forall calendar.
Date calendar -> LocalTime -> CalendarDateTime calendar
CalendarDateTime LocalTime
lt (Date cal -> CalendarDateTime cal)
-> f (Date cal) -> f (CalendarDateTime cal)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Int -> f Int) -> Date cal -> f (Date cal)
forall d (f :: * -> *).
(HasDate d, Functor f) =>
(Int -> f Int) -> d -> f d
forall (f :: * -> *).
Functor f =>
(Int -> f Int) -> Date cal -> f (Date cal)
day Int -> f Int
f Date cal
cd
  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
  monthl :: forall (f :: * -> *).
Functor f =>
(Int -> f Int) -> CalendarDateTime cal -> f (CalendarDateTime cal)
monthl Int -> f Int
f (CalendarDateTime Date cal
cd LocalTime
lt) = (Date cal -> LocalTime -> CalendarDateTime cal)
-> LocalTime -> Date cal -> CalendarDateTime cal
forall a b c. (a -> b -> c) -> b -> a -> c
flip Date cal -> LocalTime -> CalendarDateTime cal
forall calendar.
Date calendar -> LocalTime -> CalendarDateTime calendar
CalendarDateTime LocalTime
lt (Date cal -> CalendarDateTime cal)
-> f (Date cal) -> f (CalendarDateTime cal)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Int -> f Int) -> Date cal -> f (Date cal)
forall d (f :: * -> *).
(HasDate d, Functor f) =>
(Int -> f Int) -> d -> f d
forall (f :: * -> *).
Functor f =>
(Int -> f Int) -> Date cal -> f (Date cal)
monthl Int -> f Int
f Date cal
cd
  year :: forall (f :: * -> *).
Functor f =>
(Int -> f Int) -> CalendarDateTime cal -> f (CalendarDateTime cal)
year Int -> f Int
f (CalendarDateTime Date cal
cd LocalTime
lt) = (Date cal -> LocalTime -> CalendarDateTime cal)
-> LocalTime -> Date cal -> CalendarDateTime cal
forall a b c. (a -> b -> c) -> b -> a -> c
flip Date cal -> LocalTime -> CalendarDateTime cal
forall calendar.
Date calendar -> LocalTime -> CalendarDateTime calendar
CalendarDateTime LocalTime
lt (Date cal -> CalendarDateTime cal)
-> f (Date cal) -> f (CalendarDateTime cal)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Int -> f Int) -> Date cal -> f (Date cal)
forall d (f :: * -> *).
(HasDate d, Functor f) =>
(Int -> f Int) -> d -> f d
forall (f :: * -> *).
Functor f =>
(Int -> f Int) -> Date cal -> f (Date cal)
year Int -> f Int
f Date cal
cd
  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