{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module HerfTime ( module Data.Time
, herfShow
, reherf
, getYear
, getMonth
, getDay
, getDateParts
, getHour
, getMin
, getSeconds
, getPicoseconds
, UTCHerfTime
, HerfedTime(..)
, ToUTCHerfTime(..)
, FromUTCHerfTime(..)
, HerfAdd(..)
, HerfYear
, HerfMonth
, HerfWeek
, HerfDay
, HerfHour
, HerfMin
, HerfSec
, HerfPico
, year
, month
, week
, day
, hour
, minute
, second
, pico
) where
import Data.Time
year :: Integer -> HerfYear
year = HerfYear
month :: Integer -> HerfMonth
month = HerfMonth
week :: Integer -> HerfWeek
week = HerfWeek
day :: Integer -> HerfDay
day = HerfDay
hour :: Integer -> HerfHour
hour = HerfHour
minute :: Integer -> HerfMin
minute = HerfMin
second :: Integer -> HerfSec
second = HerfSec
pico :: Integer -> HerfPico
pico = HerfPico
herfShow :: (HerfedTime t, FormatTime t) => t -> String
herfShow = formatTime defaultTimeLocale (iso8601DateFormat (Just "%H:%M:%S:%Z") )
dateHerf :: Integer -> Integer-> Integer-> UTCHerfTime
dateHerf y m d = UTCHerfTime $ UTCTime dayPart timePart
where
dayPart = fromGregorian y (fromInteger m) (fromInteger d)
timePart = 0
time :: Integer -> Integer -> Integer -> DiffTime
time h m s = secondsToDiffTime ( convertedHours +
convertedMinutes +
convertedSeconds)
where
convertedHours = h * 3600
convertedMinutes = m * 60
convertedSeconds = s
timePico :: Integer -> Integer -> Integer -> Integer -> DiffTime
timePico h m s p = picoTime +
(time h m s)
where
picoTime = picosecondsToDiffTime p
dateTimeHerf :: Integer -> Integer -> Integer ->
Integer -> Integer -> Integer -> UTCHerfTime
dateTimeHerf y m d h i s = UTCHerfTime $ UTCTime dayPart timePart
where
dayPart = fromGregorian y (fromInteger m) (fromInteger d)
timePart = time h i s
dateTimePicoHerf :: Integer -> Integer -> Integer
-> Integer -> Integer -> Integer -> Integer
-> UTCHerfTime
dateTimePicoHerf y m d h i s p = UTCHerfTime $ UTCTime dayPart timePart
where
dayPart = fromGregorian y (fromInteger m) (fromInteger d)
timePart = timePico h i s p
newtype UTCHerfTime = UTCHerfTime UTCTime
deriving (Eq,Ord,Show,FormatTime)
class ToUTCHerfTime a where
herf :: a -> UTCHerfTime
class FromUTCHerfTime a where
unherf :: UTCHerfTime -> a
reherf :: (ToUTCHerfTime a, ToUTCHerfTime b, FromUTCHerfTime a, FromUTCHerfTime b ) =>
(a -> b)
reherf = unherf.herf
class (ToUTCHerfTime a, FromUTCHerfTime a) => HerfedTime a where
addYear :: a -> HerfYear -> a
addMonth :: a -> HerfMonth -> a
addWeek :: a -> HerfWeek -> a
addDay :: a -> HerfDay -> a
addHour :: a -> HerfHour -> a
addMinute :: a -> HerfMin -> a
addSecond :: a -> HerfSec -> a
addPicosecond :: a -> HerfPico -> a
date :: HerfYear -> HerfMonth -> HerfDay -> a
dateTime :: HerfYear -> HerfMonth -> HerfDay -> HerfHour -> HerfMin -> HerfSec -> a
dateTimePico :: HerfYear -> HerfMonth -> HerfDay -> HerfHour -> HerfMin -> HerfSec -> HerfPico -> a
class HerfAdd a where
add :: (HerfedTime t) => t -> a -> t
instance HerfAdd HerfYear where
add = addYear
instance HerfAdd HerfMonth where
add = addMonth
instance HerfAdd HerfWeek where
add = addWeek
instance HerfAdd HerfDay where
add = addDay
instance HerfAdd HerfHour where
add = addHour
instance HerfAdd HerfMin where
add = addMinute
instance HerfAdd HerfSec where
add = addSecond
instance HerfAdd HerfPico where
add = addPicosecond
newtype HerfYear = HerfYear Integer
deriving (Num,Eq,Ord,Show)
newtype HerfMonth = HerfMonth Integer
deriving (Num,Eq,Ord,Show)
newtype HerfWeek = HerfWeek Integer
deriving (Num,Eq,Ord,Show)
newtype HerfDay = HerfDay Integer
deriving (Num,Eq,Ord,Show)
newtype HerfHour = HerfHour Integer
deriving (Num,Eq,Ord,Show)
newtype HerfMin = HerfMin Integer
deriving (Num,Eq,Ord,Show)
newtype HerfSec = HerfSec Integer
deriving (Num,Eq,Ord,Show)
newtype HerfPico = HerfPico Integer
deriving (Num, Eq, Ord, Show)
instance ToUTCHerfTime UTCHerfTime where
herf = id
instance FromUTCHerfTime UTCHerfTime where
unherf = id
instance HerfedTime UTCHerfTime where
addYear (UTCHerfTime k) y = UTCHerfTime $ addYear k y
addMonth (UTCHerfTime k) m = UTCHerfTime $ addMonth k m
addWeek (UTCHerfTime k) w = UTCHerfTime $ addWeek k w
addDay (UTCHerfTime k) d = UTCHerfTime $ addDay k d
addHour (UTCHerfTime k) h = UTCHerfTime $ addHour k h
addMinute (UTCHerfTime k) i = UTCHerfTime $ addMinute k i
addSecond (UTCHerfTime k) s = UTCHerfTime $ addSecond k s
addPicosecond (UTCHerfTime k) p = UTCHerfTime $ addPicosecond k p
date (HerfYear y) (HerfMonth m) (HerfDay d) = dateHerf y m d
dateTime (HerfYear y) (HerfMonth m) (HerfDay d) (HerfHour h) (HerfMin i) (HerfSec s ) = dateTimeHerf y m d h i s
dateTimePico (HerfYear y) (HerfMonth m) (HerfDay d) (HerfHour h) (HerfMin i) (HerfSec s ) (HerfPico p ) = dateTimePicoHerf y m d h i s p
instance ToUTCHerfTime UTCTime where
herf = UTCHerfTime
instance FromUTCHerfTime UTCTime where
unherf (UTCHerfTime u) = u
instance HerfedTime UTCTime where
addYear (UTCTime d t) (HerfYear y) = UTCTime (addGregorianYearsRollOver y d) t
addMonth (UTCTime d t) (HerfMonth m) = UTCTime (addGregorianMonthsRollOver m d) t
addWeek (UTCTime d t) (HerfWeek w) = UTCTime (addDays (7*w) d) t
addDay (UTCTime d t) (HerfDay ds) = UTCTime (addDays ds d) t
addHour u (HerfHour h) = addUTCTime (fromIntegral $ h*3600) u
addMinute u (HerfMin i) = addUTCTime (fromIntegral $ i*60) u
addSecond u (HerfSec s) = addUTCTime (fromIntegral s) u
addPicosecond u (HerfPico p) = addUTCTime (toNominal p) u
where
toNominal = fromRational . toRational . picosecondsToDiffTime
date (HerfYear y) (HerfMonth m) (HerfDay d) = unherf $ dateHerf y m d
dateTime (HerfYear y) (HerfMonth m) (HerfDay d) (HerfHour h) (HerfMin i) (HerfSec s ) = unherf $ dateTimeHerf y m d h i s
dateTimePico (HerfYear y) (HerfMonth m) (HerfDay d) (HerfHour h) (HerfMin i) (HerfSec s ) (HerfPico p ) = unherf $ dateTimePicoHerf y m d h i s p
getYear :: UTCTime -> Integer
getYear incomingTime = case incomingTime of
(UTCTime d _) -> let (year',_ ,_ ) = toGregorian d
in year'
getMonth :: UTCTime -> Integer
getMonth incomingTime = case incomingTime of
(UTCTime d _) -> let (_ , month' ,_ ) = toGregorian d
in fromIntegral month'
getDay :: UTCTime -> Integer
getDay incomingTime = case incomingTime of
(UTCTime d _) -> let (_ , _ ,day' ) = toGregorian d
in fromIntegral day'
getDateParts :: UTCTime -> (Integer,Integer,Integer)
getDateParts (UTCTime d _) = (year',fromIntegral month',fromIntegral day')
where
(year',month',day') = toGregorian d
getHour :: UTCTime -> Integer
getHour (UTCTime _ t) = floor (t / 3600)
getMin :: UTCTime -> Integer
getMin u@(UTCTime _ t) = div remainingSeconds 60
where
timeInSeconds = floor t
remainingSeconds = timeInSeconds - secondsInHours
secondsInHours = 3600 * (getHour u)
getSeconds :: UTCTime -> Integer
getSeconds u@(UTCTime _ t) = remainingSeconds
where
timeInSeconds = floor t
remainingSeconds = timeInSeconds - secondsInHours - secondsInMinutes
secondsInHours = 3600 * (getHour u)
secondsInMinutes = 60 * (getMin u)
getPicoseconds :: UTCTime -> Integer
getPicoseconds u@(UTCTime _ t) = round $ remainingPico *
(fromRational (10^(12 :: Integer)))
where
remainingPico = t - (fromIntegral $ secondsInHours - secondsInMinutes - seconds')
secondsInHours = 3600 * (getHour u)
secondsInMinutes = 60 * (getMin u)
seconds' = getSeconds u