module Data.Time.Hora.Type
(
DatePart(..),
Tz(..),
Tz'(..),
TimeSpan(..),
TwoInt(..)) where
import GHC.Generics
import Data.Binary
import Data.Time.Clock
import Data.Time.LocalTime
import Data.Time.LocalTime.TimeZone.Series
data DatePart a = DatePart {
year::a,
month::a,
day::a,
hour::a,
minute::a,
second::a,
pico::a
} deriving (Show, Eq, Generic)
instance Functor DatePart where
fmap f0 d0 = d0 {
day = f0 (day d0),
month = f0 (month d0),
year = f0 (year d0),
hour = f0 (hour d0),
minute = f0 (minute d0),
second = f0 (second d0),
pico = f0 (pico d0)
}
instance Binary (DatePart Int)
instance Binary (DatePart Integer)
instance Binary (DatePart String)
instance Ord a => Ord (DatePart a) where
(<=) a0 b0 =
let y1 = (year a0, year b0)
m1 = (month a0, month b0)
d1 = (day a0, day b0)
h1 = (hour a0, hour b0)
min1 = (minute a0, minute b0)
s1 = (second a0, second b0)
p1 = (pico a0, pico b0)
l1 = [y1,m1,d1,h1,min1,s1,p1]
f1 (Stop bo1) _ = Stop bo1
f1 Continue (a1, b1)
| a1 < b1 = Stop True
| a1 == b1 = Continue
| a1 > b1 = Stop False
res2 = foldl f1 Continue l1
in case res2 of
Continue -> True
(Stop b2) -> b2
data Ord_ = Stop Bool | Continue
data Tz a = Tz TimeZone a deriving (Show,Functor)
class Tz' tz where
tz'::tz -> UTCTime -> TimeZone
instance Tz' TimeZone where
tz' tz0 _ = tz0
instance Tz' TimeZoneSeries where
tz' = timeZoneFromSeries
data TimeSpan a = Sec a
| Pico a
| Milli a deriving (Show, Functor)
type TwoInt a b = (Integral a, Integral b)
instance Integral a => Num (TimeSpan a) where
(+) = withPico (+)
(*) = withPico (*)
abs a0 = if a0 > Pico 0 then a0 else a0
signum a0
| a0 > Pico 0 = 1
| a0 < Pico 0 = 1
| otherwise = 0
fromInteger i0 = Pico $ fromIntegral i0
() = withPico ()
instance (Eq a, Integral a) => Eq (TimeSpan a) where
(==) (Sec a0) (Sec b0) = a0 == b0
(==) (Milli a0) (Milli b0) = a0 == b0
(==) (Pico a0) (Pico b0) = a0 == b0
(==) a0 b0 = a1 == b1
where a1 = toPico a0::Integer
b1 = toPico b0::Integer
instance (Ord a, Integral a) => Ord (TimeSpan a) where
(<=) (Sec a0) (Sec b0) = a0 <= b0
(<=) (Milli a0) (Milli b0) = a0 <= b0
(<=) (Pico a0) (Pico b0) = a0 <= b0
(<=) a0 b0 = a1 <= b1
where a1 = toPico a0::Integer
b1 = toPico b0::Integer
withPico::Integral a => (a -> a -> a) ->
TimeSpan a -> TimeSpan a -> TimeSpan a
withPico fn0 a0 b0 = Pico $ fn0 a1 b1
where a1 = toPico a0
b1 = toPico b0
toPico::TwoInt a b => TimeSpan a -> b
toPico (Pico i0) = fromIntegral i0
toPico (Milli i0) = fromIntegral $ i0 * picoMs
toPico (Sec i0) = fromIntegral $ i0 * picoSec
picoSec::Integral a => a
picoSec = 1000000000000
picoMs::Integral a => a
picoMs = 1000000000