1 {-# LANGUAGE TypeSynonymInstances #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 module Hcompta.Date.Interval where
5 import qualified Data.Fixed
6 import qualified Data.List
7 import Data.Maybe (fromJust)
8 import qualified Data.Time.Calendar as Time
9 import qualified Data.Time.Clock as Time
10 import qualified Data.Time.Format as Time ()
11 import qualified Data.Time.LocalTime as Time
13 import Hcompta.Date hiding (year, month, dom, hour, minute, second)
14 import qualified Hcompta.Date as Date
15 import Hcompta.Lib.Interval (Interval)
16 import qualified Hcompta.Lib.Interval as Interval
21 year :: Year -> Interval Date
23 let ly = Time.addGregorianYearsClip (y - 1970) (day nil) in
24 let hy = Time.addGregorianYearsClip 1 ly in
25 fromJust $ (Interval.<=..<)
29 month :: Year -> Month -> Interval Date
31 let ly = Time.addGregorianYearsClip (y - 1970) (day nil) in
32 let lm = Time.addGregorianMonthsClip (toInteger m - 1) ly in
33 let hm = Time.addGregorianMonthsClip 1 lm in
34 fromJust $ (Interval.<=..<)
38 dom :: Year -> Month -> DoM -> Interval Date
40 let ly = Time.addGregorianYearsClip (y - 1970) (day nil) in
41 let lm = Time.addGregorianMonthsClip (toInteger m - 1) ly in
42 let ld = Time.addDays (toInteger d - 1) lm in
43 let hd = Time.addDays 1 ld in
44 fromJust $ (Interval.<=..<)
48 hour :: Year -> Month -> DoM -> Hour -> Interval Date
50 let ly = Time.addGregorianYearsClip (y - 1970) (day nil) in
51 let lm = Time.addGregorianMonthsClip (toInteger m - 1) ly in
52 let ld = nil{Time.utctDay=Time.addDays (toInteger d - 1) lm} in
53 let lh = Time.addUTCTime (fromInteger $ toInteger (h * 3600)) ld in
54 let hh = Time.addUTCTime 3600 lh in
55 fromJust $ (Interval.<=..<) lh hh
57 minute :: Year -> Month -> DoM -> Hour -> Minute -> Interval Date
59 let ly = Time.addGregorianYearsClip (y - 1970) (day nil) in
60 let lm = Time.addGregorianMonthsClip (toInteger m - 1) ly in
61 let ld = nil{Time.utctDay=Time.addDays (toInteger d - 1) lm} in
62 let lh = Time.addUTCTime (fromInteger $ toInteger (h * 3600)) ld in
63 let ln = Time.addUTCTime (fromInteger $ toInteger (n * 60)) lh in
64 let hn = Time.addUTCTime 60 ln in
65 fromJust $ (Interval.<=..<) ln hn
67 second :: Year -> Month -> DoM -> Hour -> Minute -> Second -> Interval Date
68 second y m d h n (Data.Fixed.MkFixed s) =
69 let ly = Time.addGregorianYearsClip (y - 1970) (day nil) in
70 let lm = Time.addGregorianMonthsClip (toInteger m - 1) ly in
71 let ld = nil{Time.utctDay=Time.addDays (toInteger d - 1) lm} in
72 let lh = Time.addUTCTime (fromInteger $ toInteger (h * 3600)) ld in
73 let ln = Time.addUTCTime (fromInteger $ toInteger (n * 60)) lh in
74 let ls = Time.addUTCTime (fromInteger $ s) ln in
75 let hs = Time.addUTCTime 1 ls in
76 fromJust $ (Interval.<=..<) ls hs
80 slice :: (Date -> Interval Date) -> Interval Date -> [Interval Date]
83 (\last_slice -> case Interval.intersection i last_slice of
85 Just n -> Just (n, slicer (Interval.limit $ Interval.high last_slice))
87 slicer (Interval.limit $ Interval.low i)
89 year_slice :: Date -> Interval Date
91 let (y, _, _) = Date.gregorian d in
94 month_slice :: Date -> Interval Date
96 let (y, m, _) = Date.gregorian d in
99 dom_slice :: Date -> Interval Date
101 let (y, m, o) = Date.gregorian d in
104 hour_slice :: Date -> Interval Date
106 let (y, m, o) = Date.gregorian d in
107 let Time.TimeOfDay h _ _ = tod d in
110 minute_slice :: Date -> Interval Date
112 let (y, m, o) = Date.gregorian d in
113 let Time.TimeOfDay h n _ = tod d in
116 second_slice :: Date -> Interval Date
118 let (y, m, o) = Date.gregorian d in
119 let Time.TimeOfDay h n s = tod d in