1 {-# OPTIONS_GHC -fno-warn-orphans #-}
2 module Hcompta.Date.Interval where
4 import qualified Data.Fixed
5 import qualified Data.List
6 import Data.Maybe (fromJust)
7 import qualified Data.Time.Calendar as Time
8 import qualified Data.Time.Clock as Time
9 import qualified Data.Time.Format as Time ()
10 import qualified Data.Time.LocalTime as Time
12 import Hcompta.Date hiding (year, month, dom, hour, minute, second)
13 import qualified Hcompta.Date as Date
14 import Hcompta.Lib.Interval (Interval)
15 import qualified Hcompta.Lib.Interval as Interval
20 year :: Year -> Interval Date
22 let ly = Time.addGregorianYearsClip (y - 1970) (day nil) in
23 let hy = Time.addGregorianYearsClip 1 ly in
24 fromJust $ (Interval.<=..<)
28 month :: Year -> Month -> Interval Date
30 let ly = Time.addGregorianYearsClip (y - 1970) (day nil) in
31 let lm = Time.addGregorianMonthsClip (toInteger m - 1) ly in
32 let hm = Time.addGregorianMonthsClip 1 lm in
33 fromJust $ (Interval.<=..<)
37 dom :: Year -> Month -> DoM -> Interval Date
39 let ly = Time.addGregorianYearsClip (y - 1970) (day nil) in
40 let lm = Time.addGregorianMonthsClip (toInteger m - 1) ly in
41 let ld = Time.addDays (toInteger d - 1) lm in
42 let hd = Time.addDays 1 ld in
43 fromJust $ (Interval.<=..<)
47 hour :: Year -> Month -> DoM -> Hour -> Interval Date
49 let ly = Time.addGregorianYearsClip (y - 1970) (day nil) in
50 let lm = Time.addGregorianMonthsClip (toInteger m - 1) ly in
51 let ld = nil{Time.utctDay=Time.addDays (toInteger d - 1) lm} in
52 let lh = Time.addUTCTime (fromInteger $ toInteger (h * 3600)) ld in
53 let hh = Time.addUTCTime 3600 lh in
54 fromJust $ (Interval.<=..<) lh hh
56 minute :: Year -> Month -> DoM -> Hour -> Minute -> Interval Date
58 let ly = Time.addGregorianYearsClip (y - 1970) (day nil) in
59 let lm = Time.addGregorianMonthsClip (toInteger m - 1) ly in
60 let ld = nil{Time.utctDay=Time.addDays (toInteger d - 1) lm} in
61 let lh = Time.addUTCTime (fromInteger $ toInteger (h * 3600)) ld in
62 let ln = Time.addUTCTime (fromInteger $ toInteger (n * 60)) lh in
63 let hn = Time.addUTCTime 60 ln in
64 fromJust $ (Interval.<=..<) ln hn
66 second :: Year -> Month -> DoM -> Hour -> Minute -> Second -> Interval Date
67 second y m d h n (Data.Fixed.MkFixed s) =
68 let ly = Time.addGregorianYearsClip (y - 1970) (day nil) in
69 let lm = Time.addGregorianMonthsClip (toInteger m - 1) ly in
70 let ld = nil{Time.utctDay=Time.addDays (toInteger d - 1) lm} in
71 let lh = Time.addUTCTime (fromInteger $ toInteger (h * 3600)) ld in
72 let ln = Time.addUTCTime (fromInteger $ toInteger (n * 60)) lh in
73 let ls = Time.addUTCTime (fromInteger $ s) ln in
74 let hs = Time.addUTCTime 1 ls in
75 fromJust $ (Interval.<=..<) ls hs
79 slice :: (Date -> Interval Date) -> Interval Date -> [Interval Date]
82 (\last_slice -> case Interval.intersection i last_slice of
84 Just n -> Just (n, slicer (Interval.limit $ Interval.high last_slice))
86 slicer (Interval.limit $ Interval.low i)
88 year_slice :: Date -> Interval Date
90 let (y, _, _) = Date.gregorian d in
93 month_slice :: Date -> Interval Date
95 let (y, m, _) = Date.gregorian d in
98 dom_slice :: Date -> Interval Date
100 let (y, m, o) = Date.gregorian d in
103 hour_slice :: Date -> Interval Date
105 let (y, m, o) = Date.gregorian d in
106 let Time.TimeOfDay h _ _ = tod d in
109 minute_slice :: Date -> Interval Date
111 let (y, m, o) = Date.gregorian d in
112 let Time.TimeOfDay h n _ = tod d in
115 second_slice :: Date -> Interval Date
117 let (y, m, o) = Date.gregorian d in
118 let Time.TimeOfDay h n s = tod d in