Debian Codegraph @

Hledger/Interest/DayCountConvention.hs

raw
1 module Hledger.Interest.DayCountConvention
2 ( DayCountConvention
3 , diffAct
4 , diff30_360
5 , diff30E_360
6 , diff30E_360isda
7 )
8 where
9
10 import Control.Exception ( assert )
11 import Data.Time.Calendar
12
13 type DayCountConvention = Day -> Day -> Integer
14
15 diffAct :: DayCountConvention
16 diffAct date1 date2 = assert (date1 <= date2) $ fromInteger (date2 `diffDays` date1)
17
18 mkDiff30_360 :: (Integer,Int,Int) -> (Integer,Int,Int) -> Integer
19 mkDiff30_360 (y1,m1,d1) (y2,m2,d2) = 360*(y2-y1) + 30*toInteger (m2-m1) + toInteger (d2-d1)
20
21 -- The un-corrected naked formular.
22
23 diff30_360 :: DayCountConvention
24 diff30_360 date1 date2 = assert (date1 <= date2) $ mkDiff30_360 (toGregorian date1) (toGregorian date2)
25
26 -- No month has more than 30 days, but February may have 28 or 29; i.e.
27 -- there are 32 days between 2003-02-28 and 2003-03-31. Commonly known
28 -- as "Deutsche Zinsmethode 30 / 360".
29
30 diff30E_360 :: DayCountConvention
31 diff30E_360 date1 date2 = assert (date1 <= date2) $ mkDiff30_360 (y1, m1, min 30 d1) (y2, m2, min 30 d2)
32 where
33 (y1,m1,d1) = toGregorian date1
34 (y2,m2,d2) = toGregorian date2
35
36 -- This variant additionally normalizes end-of-months to 30, i.e. there
37 -- are 30 days between 2003-02-28 and 2003-03-31.
38
39 diff30E_360isda :: DayCountConvention
40 diff30E_360isda date1 date2 = assert (date1 <= date2) $ mkDiff30_360 (y1, m1, d1') (y2, m2, d2')
41 where
42 (y1,m1,d1) = toGregorian date1
43 (y2,m2,d2) = toGregorian date2
44 d1' = if d1 > 30 || d1 == gregorianMonthLength y1 m1 then 30 else d1
45 d2' = if d1 > 30 || d2 == gregorianMonthLength y2 m2 then 30 else d2