bbaca0ab78ade30e6c7f8946acf8a8740b75c128
[packages/time.git] / lib / Data / Time / Calendar / CalendarDiffDays.hs
1 module Data.Time.Calendar.CalendarDiffDays
2 (
3 -- * Calendar Duration
4 module Data.Time.Calendar.CalendarDiffDays
5 ) where
6
7 #if MIN_VERSION_base(4,8,0)
8 #else
9 import Data.Monoid
10 #endif
11 #if MIN_VERSION_base(4,9,0)
12 import Data.Semigroup hiding (option)
13 #endif
14
15 data CalendarDiffDays = CalendarDiffDays
16 { cdMonths :: Integer
17 , cdDays :: Integer
18 } deriving Eq
19
20 #if MIN_VERSION_base(4,9,0)
21 -- | Additive
22 instance Semigroup CalendarDiffDays where
23 CalendarDiffDays m1 d1 <> CalendarDiffDays m2 d2 = CalendarDiffDays (m1 + m2) (d1 + d2)
24 #endif
25
26 -- | Additive
27 instance Monoid CalendarDiffDays where
28 mempty = CalendarDiffDays 0 0
29 #if MIN_VERSION_base(4,9,0)
30 mappend = (<>)
31 #else
32 mappend (CalendarDiffDays m1 d1) (CalendarDiffDays m2 d2) = CalendarDiffDays (m1 + m2) (d1 + d2)
33 #endif
34
35 instance Show CalendarDiffDays where
36 show (CalendarDiffDays m d) = "P" ++ show m ++ "M" ++ show d ++ "D"
37
38 calendarDay :: CalendarDiffDays
39 calendarDay = CalendarDiffDays 0 1
40
41 calendarWeek :: CalendarDiffDays
42 calendarWeek = CalendarDiffDays 0 7
43
44 calendarMonth :: CalendarDiffDays
45 calendarMonth = CalendarDiffDays 1 0
46
47 calendarYear :: CalendarDiffDays
48 calendarYear = CalendarDiffDays 12 0
49
50 -- | Scale by a factor. Note that @scaleCalendarDiffDays (-1)@ will not perfectly invert a duration, due to variable month lengths.
51 scaleCalendarDiffDays :: Integer -> CalendarDiffDays -> CalendarDiffDays
52 scaleCalendarDiffDays k (CalendarDiffDays m d) = CalendarDiffDays (k * m) (k * d)