1 module Hcompta.Date where
3 import Control.Monad (Monad(..))
4 import Data.Function (($), (.), id)
6 import Data.Maybe (Maybe(..))
7 import Prelude (Integer, Num(..))
9 import qualified Data.Fixed
10 import qualified Data.Time.Calendar as Time
11 import qualified Data.Time.Clock as Time
12 import qualified Data.Time.Clock.POSIX as Time (posixSecondsToUTCTime)
13 import qualified Data.Time.Format as Time ()
14 import qualified Data.Time.LocalTime as Time
27 gregorianOf :: d -> (Date_Year d, Date_Month d, Date_DoM d)
28 yearOf :: d -> Date_Year d
29 monthOf :: d -> Date_Month d
30 domOf :: d -> Date_DoM d
31 todOf :: d -> (Date_Hour d, Date_Minute d, Date_Second d)
32 hourOf :: d -> Date_Hour d
33 minuteOf :: d -> Date_Minute d
34 secondOf :: d -> Date_Second d
35 utcOf :: d -> Time.UTCTime
45 yearOf = (\(x, _, _) -> x) . gregorianOf
46 monthOf = (\(_, x, _) -> x) . gregorianOf
47 domOf = (\(_, _, x) -> x) . gregorianOf
48 hourOf = (\(x, _, _) -> x) . todOf
49 minuteOf = (\(_, x, _) -> x) . todOf
50 secondOf = (\(_, _, x) -> x) . todOf
52 instance Date Time.UTCTime where
53 type Date_Year Time.UTCTime = Integer
54 type Date_Month Time.UTCTime = Int
55 type Date_DoM Time.UTCTime = Int
56 type Date_Hour Time.UTCTime = Int
57 type Date_Minute Time.UTCTime = Int
58 type Date_Second Time.UTCTime = Data.Fixed.Pico
59 epoch = Time.posixSecondsToUTCTime 0
60 now = Time.getCurrentTime
61 gregorianOf = Time.toGregorian . Time.utctDay
63 case Time.timeToTimeOfDay $ Time.utctDayTime d of
64 Time.TimeOfDay h m s -> (h, m, s)
65 -- date_day = Time.utctDay
67 dateOf y m d h mn s = do
68 gday <- Time.fromGregorianValid y m d
69 hod <- Time.makeTimeOfDayValid h mn s
72 Time.timeOfDayToTime hod
74 date_next_year_start :: Time.UTCTime -> Time.UTCTime
75 date_next_year_start d =
77 Time.addGregorianYearsClip
82 date_bench :: IO a -> IO (a, Time.NominalDiffTime)
87 return (r, Time.diffUTCTime t1 t0)