module Chronos.Types
( Day(..)
, DayOfWeek(..)
, DayOfMonth(..)
, DayOfYear(..)
, Month(..)
, Year(..)
, Offset(..)
, Time(..)
, DayOfWeekMatch(..)
, MonthMatch(..)
, UnboxedMonthMatch(..)
, Timespan(..)
, SubsecondPrecision(..)
, Date(..)
, OrdinalDate(..)
, MonthDate(..)
, Datetime(..)
, OffsetDatetime(..)
, TimeOfDay(..)
, DatetimeFormat(..)
, OffsetFormat(..)
, DatetimeLocale(..)
, MeridiemLocale(..)
) where
import Torsor
import Data.Int
import Data.Vector (Vector)
import Data.Aeson (FromJSON,ToJSON)
import Data.Hashable (Hashable)
import Data.Primitive
import Foreign.Storable
import Control.Monad
import GHC.Generics (Generic)
import Data.Monoid (Monoid(..))
import qualified Data.Vector.Generic as GVector
import qualified Data.Vector.Unboxed as UVector
import qualified Data.Vector.Primitive as PVector
import qualified Data.Vector.Generic.Mutable as MGVector
newtype Day = Day { getDay :: Int }
deriving (Show,Read,Eq,Ord,Hashable,Enum,ToJSON,FromJSON,Storable,Prim)
instance Torsor Day Int where
add i (Day d) = Day (d + i)
difference (Day a) (Day b) = a b
newtype DayOfWeek = DayOfWeek { getDayOfWeek :: Int }
deriving (Show,Read,Eq,Ord,Hashable)
newtype DayOfMonth = DayOfMonth { getDayOfMonth :: Int }
deriving (Show,Read,Eq,Ord,Prim,Enum)
newtype DayOfYear = DayOfYear { getDayOfYear :: Int }
deriving (Show,Read,Eq,Ord,Prim)
newtype Month = Month { getMonth :: Int }
deriving (Show,Read,Eq,Ord,Prim)
instance Bounded Month where
minBound = Month 0
maxBound = Month 11
newtype Year = Year { getYear :: Int }
deriving (Show,Read,Eq,Ord)
newtype Offset = Offset { getOffset :: Int }
deriving (Show,Read,Eq,Ord)
newtype Time = Time { getTime :: Int64 }
deriving (FromJSON,ToJSON,Hashable,Eq,Ord,Show,Read,Storable,Prim)
newtype DayOfWeekMatch a = DayOfWeekMatch { getDayOfWeekMatch :: Vector a }
newtype MonthMatch a = MonthMatch { getMonthMatch :: Vector a }
newtype UnboxedMonthMatch a = UnboxedMonthMatch { getUnboxedMonthMatch :: UVector.Vector a }
newtype Timespan = Timespan { getTimespan :: Int64 }
deriving (Show,Read,Eq,Ord,ToJSON,FromJSON,Additive)
instance Monoid Timespan where
mempty = Timespan 0
mappend (Timespan a) (Timespan b) = Timespan (a + b)
instance Torsor Time Timespan where
add (Timespan ts) (Time t) = Time (t + ts)
difference (Time t) (Time s) = Timespan (t s)
instance Scaling Timespan Int64 where
scale i (Timespan ts) = Timespan (i * ts)
data SubsecondPrecision
= SubsecondPrecisionAuto
| SubsecondPrecisionFixed !Int
data Date = Date
{ dateYear :: !Year
, dateMonth :: !Month
, dateDay :: !DayOfMonth
} deriving (Show,Read,Eq,Ord)
data OrdinalDate = OrdinalDate
{ ordinalDateYear :: !Year
, ordinalDateDayOfYear :: !DayOfYear
} deriving (Show,Read,Eq,Ord)
data MonthDate = MonthDate
{ monthDateMonth :: !Month
, monthDateDay :: !DayOfMonth
} deriving (Show,Read,Eq,Ord)
data Datetime = Datetime
{ datetimeDate :: !Date
, datetimeTime :: !TimeOfDay
} deriving (Show,Read,Eq,Ord)
data OffsetDatetime = OffsetDatetime
{ offsetDatetimeDatetime :: !Datetime
, offsetDatetimeOffset :: !Offset
} deriving (Show,Read,Eq,Ord)
data TimeOfDay = TimeOfDay
{ timeOfDayHour :: !Int
, timeOfDayMinute :: !Int
, timeOfDayNanoseconds :: !Int64
} deriving (Show,Read,Eq,Ord)
data DatetimeFormat = DatetimeFormat
{ datetimeFormatDateSeparator :: !(Maybe Char)
, datetimeFormatSeparator :: !(Maybe Char)
, datetimeFormatTimeSeparator :: !(Maybe Char)
} deriving (Show,Read,Eq,Ord)
data OffsetFormat
= OffsetFormatColonOff
| OffsetFormatColonOn
| OffsetFormatSecondsPrecision
| OffsetFormatColonAuto
deriving (Show,Read,Eq,Ord,Enum,Bounded,Generic)
data DatetimeLocale a = DatetimeLocale
{ datetimeLocaleDaysOfWeekFull :: !(DayOfWeekMatch a)
, datetimeLocaleDaysOfWeekAbbreviated :: !(DayOfWeekMatch a)
, datetimeLocaleMonthsFull :: !(MonthMatch a)
, datetimeLocaleMonthsAbbreviated :: !(MonthMatch a)
}
data MeridiemLocale a = MeridiemLocale
{ meridiemLocaleAm :: !a
, meridiemLocalePm :: !a
} deriving (Read,Show,Eq,Ord)
newtype instance UVector.MVector s Month = MV_Month (PVector.MVector s Month)
newtype instance UVector.Vector Month = V_Month (PVector.Vector Month)
instance UVector.Unbox Month
instance MGVector.MVector UVector.MVector Month where
basicLength (MV_Month v) = MGVector.basicLength v
basicUnsafeSlice i n (MV_Month v) = MV_Month $ MGVector.basicUnsafeSlice i n v
basicOverlaps (MV_Month v1) (MV_Month v2) = MGVector.basicOverlaps v1 v2
basicUnsafeNew n = MV_Month `liftM` MGVector.basicUnsafeNew n
basicInitialize (MV_Month v) = MGVector.basicInitialize v
basicUnsafeReplicate n x = MV_Month `liftM` MGVector.basicUnsafeReplicate n x
basicUnsafeRead (MV_Month v) i = MGVector.basicUnsafeRead v i
basicUnsafeWrite (MV_Month v) i x = MGVector.basicUnsafeWrite v i x
basicClear (MV_Month v) = MGVector.basicClear v
basicSet (MV_Month v) x = MGVector.basicSet v x
basicUnsafeCopy (MV_Month v1) (MV_Month v2) = MGVector.basicUnsafeCopy v1 v2
basicUnsafeMove (MV_Month v1) (MV_Month v2) = MGVector.basicUnsafeMove v1 v2
basicUnsafeGrow (MV_Month v) n = MV_Month `liftM` MGVector.basicUnsafeGrow v n
instance GVector.Vector UVector.Vector Month where
basicUnsafeFreeze (MV_Month v) = V_Month `liftM` GVector.basicUnsafeFreeze v
basicUnsafeThaw (V_Month v) = MV_Month `liftM` GVector.basicUnsafeThaw v
basicLength (V_Month v) = GVector.basicLength v
basicUnsafeSlice i n (V_Month v) = V_Month $ GVector.basicUnsafeSlice i n v
basicUnsafeIndexM (V_Month v) i = GVector.basicUnsafeIndexM v i
basicUnsafeCopy (MV_Month mv) (V_Month v) = GVector.basicUnsafeCopy mv v
elemseq _ = seq
newtype instance UVector.MVector s DayOfMonth = MV_DayOfMonth (PVector.MVector s DayOfMonth)
newtype instance UVector.Vector DayOfMonth = V_DayOfMonth (PVector.Vector DayOfMonth)
instance UVector.Unbox DayOfMonth
instance MGVector.MVector UVector.MVector DayOfMonth where
basicLength (MV_DayOfMonth v) = MGVector.basicLength v
basicUnsafeSlice i n (MV_DayOfMonth v) = MV_DayOfMonth $ MGVector.basicUnsafeSlice i n v
basicOverlaps (MV_DayOfMonth v1) (MV_DayOfMonth v2) = MGVector.basicOverlaps v1 v2
basicUnsafeNew n = MV_DayOfMonth `liftM` MGVector.basicUnsafeNew n
basicInitialize (MV_DayOfMonth v) = MGVector.basicInitialize v
basicUnsafeReplicate n x = MV_DayOfMonth `liftM` MGVector.basicUnsafeReplicate n x
basicUnsafeRead (MV_DayOfMonth v) i = MGVector.basicUnsafeRead v i
basicUnsafeWrite (MV_DayOfMonth v) i x = MGVector.basicUnsafeWrite v i x
basicClear (MV_DayOfMonth v) = MGVector.basicClear v
basicSet (MV_DayOfMonth v) x = MGVector.basicSet v x
basicUnsafeCopy (MV_DayOfMonth v1) (MV_DayOfMonth v2) = MGVector.basicUnsafeCopy v1 v2
basicUnsafeMove (MV_DayOfMonth v1) (MV_DayOfMonth v2) = MGVector.basicUnsafeMove v1 v2
basicUnsafeGrow (MV_DayOfMonth v) n = MV_DayOfMonth `liftM` MGVector.basicUnsafeGrow v n
instance GVector.Vector UVector.Vector DayOfMonth where
basicUnsafeFreeze (MV_DayOfMonth v) = V_DayOfMonth `liftM` GVector.basicUnsafeFreeze v
basicUnsafeThaw (V_DayOfMonth v) = MV_DayOfMonth `liftM` GVector.basicUnsafeThaw v
basicLength (V_DayOfMonth v) = GVector.basicLength v
basicUnsafeSlice i n (V_DayOfMonth v) = V_DayOfMonth $ GVector.basicUnsafeSlice i n v
basicUnsafeIndexM (V_DayOfMonth v) i = GVector.basicUnsafeIndexM v i
basicUnsafeCopy (MV_DayOfMonth mv) (V_DayOfMonth v) = GVector.basicUnsafeCopy mv v
elemseq _ = seq