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