d5c69b8901af6bd23be35004eb1db7b811c1b8ca
[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) && !MIN_VERSION_base(4,11,0)
12 import Data.Semigroup hiding (option)
13 #endif
14 import Data.Typeable
15 import Data.Data
16
17 data CalendarDiffDays = CalendarDiffDays
18 { cdMonths :: Integer
19 , cdDays :: Integer
20 } deriving (Eq,
21 Data
22 #if __GLASGOW_HASKELL__ >= 802
23 -- ^ @since 1.9.2
24 #endif
25 ,Typeable
26 #if __GLASGOW_HASKELL__ >= 802
27 -- ^ @since 1.9.2
28 #endif
29 )
30
31 #if MIN_VERSION_base(4,9,0)
32 -- | Additive
33 instance Semigroup CalendarDiffDays where
34 CalendarDiffDays m1 d1 <> CalendarDiffDays m2 d2 = CalendarDiffDays (m1 + m2) (d1 + d2)
35 #endif
36
37 -- | Additive
38 instance Monoid CalendarDiffDays where
39 mempty = CalendarDiffDays 0 0
40 #if MIN_VERSION_base(4,9,0)
41 mappend = (<>)
42 #else
43 mappend (CalendarDiffDays m1 d1) (CalendarDiffDays m2 d2) = CalendarDiffDays (m1 + m2) (d1 + d2)
44 #endif
45
46 instance Show CalendarDiffDays where
47 show (CalendarDiffDays m d) = "P" ++ show m ++ "M" ++ show d ++ "D"
48
49 calendarDay :: CalendarDiffDays
50 calendarDay = CalendarDiffDays 0 1
51
52 calendarWeek :: CalendarDiffDays
53 calendarWeek = CalendarDiffDays 0 7
54
55 calendarMonth :: CalendarDiffDays
56 calendarMonth = CalendarDiffDays 1 0
57
58 calendarYear :: CalendarDiffDays
59 calendarYear = CalendarDiffDays 12 0
60
61 -- | Scale by a factor. Note that @scaleCalendarDiffDays (-1)@ will not perfectly invert a duration, due to variable month lengths.
62 scaleCalendarDiffDays :: Integer -> CalendarDiffDays -> CalendarDiffDays
63 scaleCalendarDiffDays k (CalendarDiffDays m d) = CalendarDiffDays (k * m) (k * d)