1 {-# OPTIONS_GHC -fno-warn-orphans #-}
2 module Hcompta.Date where
4 import Control.Monad (Monad(..))
5 import qualified Data.Fixed
6 import Data.Function (($), (.), id)
8 import Data.Maybe (Maybe(..))
9 import qualified Data.Time.Calendar as Time
10 import qualified Data.Time.Clock as Time
11 import qualified Data.Time.Clock.POSIX as Time (posixSecondsToUTCTime)
12 import qualified Data.Time.Format as Time ()
13 import qualified Data.Time.LocalTime as Time
14 import Prelude (Integer, Num(..))
19 type Date = Time.UTCTime
26 type Second = Data.Fixed.Pico
29 date_epoch = Time.posixSecondsToUTCTime 0
32 date_now = Time.getCurrentTime
34 date :: Year -> Month -> DoM -> Hour -> Minute -> Second -> Maybe Date
35 date y m d h mn s = do
36 gday <- Time.fromGregorianValid y m d
37 hod <- Time.makeTimeOfDayValid h mn s
40 Time.timeOfDayToTime hod
44 date_gregorian :: Date -> (Year, Month, DoM)
45 date_gregorian = Time.toGregorian . Time.utctDay
47 date_year :: Date -> Year
48 date_year = (\(x, _, _) -> x) . date_gregorian
50 date_month :: Date -> Month
51 date_month = (\(_, x, _) -> x) . date_gregorian
53 date_day :: Date -> Day
54 date_day = Time.utctDay
56 date_dom :: Date -> DoM
57 date_dom = (\(_, _, x) -> x) . date_gregorian
59 date_tod :: Date -> Time.TimeOfDay
60 date_tod = Time.timeToTimeOfDay . Time.utctDayTime
62 date_hour :: Date -> Hour
63 date_hour = (\(Time.TimeOfDay x _ _) -> x) . date_tod
65 date_minute :: Date -> Minute
66 date_minute = (\(Time.TimeOfDay _ x _) -> x) . date_tod
68 date_second :: Date -> Second
69 date_second = (\(Time.TimeOfDay _ _ x) -> x) . date_tod
71 date_next_year_start :: Date -> Date
72 date_next_year_start d =
73 date_epoch{ Time.utctDay =
74 Time.addGregorianYearsClip
75 (date_year d + 1 - 1970)
79 date_utc :: Date -> Time.UTCTime
82 date_bench :: IO a -> IO (a, Time.NominalDiffTime)
87 return (r, Time.diffUTCTime t1 t0)