module QuantLib.Time.Date
( BusinessDayConvention(..)
, Day(..)
, Holiday(..)
, getDaysBetween
, isWeekEnd
, getNextBusinessDay
) where
import Data.Time.Calendar (Day(..), addDays, toGregorian)
import Data.Time.Calendar.WeekDate (dayOfWeek, DayOfWeek(..))
data BusinessDayConvention = Following
| ModifiedFollowing
| Preceding
| ModifiedPreceding
| Unadjusted
deriving (Int -> BusinessDayConvention -> ShowS
[BusinessDayConvention] -> ShowS
BusinessDayConvention -> String
(Int -> BusinessDayConvention -> ShowS)
-> (BusinessDayConvention -> String)
-> ([BusinessDayConvention] -> ShowS)
-> Show BusinessDayConvention
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BusinessDayConvention -> ShowS
showsPrec :: Int -> BusinessDayConvention -> ShowS
$cshow :: BusinessDayConvention -> String
show :: BusinessDayConvention -> String
$cshowList :: [BusinessDayConvention] -> ShowS
showList :: [BusinessDayConvention] -> ShowS
Show, BusinessDayConvention -> BusinessDayConvention -> Bool
(BusinessDayConvention -> BusinessDayConvention -> Bool)
-> (BusinessDayConvention -> BusinessDayConvention -> Bool)
-> Eq BusinessDayConvention
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BusinessDayConvention -> BusinessDayConvention -> Bool
== :: BusinessDayConvention -> BusinessDayConvention -> Bool
$c/= :: BusinessDayConvention -> BusinessDayConvention -> Bool
/= :: BusinessDayConvention -> BusinessDayConvention -> Bool
Eq, Int -> BusinessDayConvention
BusinessDayConvention -> Int
BusinessDayConvention -> [BusinessDayConvention]
BusinessDayConvention -> BusinessDayConvention
BusinessDayConvention
-> BusinessDayConvention -> [BusinessDayConvention]
BusinessDayConvention
-> BusinessDayConvention
-> BusinessDayConvention
-> [BusinessDayConvention]
(BusinessDayConvention -> BusinessDayConvention)
-> (BusinessDayConvention -> BusinessDayConvention)
-> (Int -> BusinessDayConvention)
-> (BusinessDayConvention -> Int)
-> (BusinessDayConvention -> [BusinessDayConvention])
-> (BusinessDayConvention
-> BusinessDayConvention -> [BusinessDayConvention])
-> (BusinessDayConvention
-> BusinessDayConvention -> [BusinessDayConvention])
-> (BusinessDayConvention
-> BusinessDayConvention
-> BusinessDayConvention
-> [BusinessDayConvention])
-> Enum BusinessDayConvention
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: BusinessDayConvention -> BusinessDayConvention
succ :: BusinessDayConvention -> BusinessDayConvention
$cpred :: BusinessDayConvention -> BusinessDayConvention
pred :: BusinessDayConvention -> BusinessDayConvention
$ctoEnum :: Int -> BusinessDayConvention
toEnum :: Int -> BusinessDayConvention
$cfromEnum :: BusinessDayConvention -> Int
fromEnum :: BusinessDayConvention -> Int
$cenumFrom :: BusinessDayConvention -> [BusinessDayConvention]
enumFrom :: BusinessDayConvention -> [BusinessDayConvention]
$cenumFromThen :: BusinessDayConvention
-> BusinessDayConvention -> [BusinessDayConvention]
enumFromThen :: BusinessDayConvention
-> BusinessDayConvention -> [BusinessDayConvention]
$cenumFromTo :: BusinessDayConvention
-> BusinessDayConvention -> [BusinessDayConvention]
enumFromTo :: BusinessDayConvention
-> BusinessDayConvention -> [BusinessDayConvention]
$cenumFromThenTo :: BusinessDayConvention
-> BusinessDayConvention
-> BusinessDayConvention
-> [BusinessDayConvention]
enumFromThenTo :: BusinessDayConvention
-> BusinessDayConvention
-> BusinessDayConvention
-> [BusinessDayConvention]
Enum)
class Holiday m where
isHoliday :: m->(Integer, Int, Int)->Bool
isBusinessDay :: m->Day->Bool
isBusinessDay m
m Day
d = Bool -> Bool
not (m -> (Integer, Int, Int) -> Bool
forall m. Holiday m => m -> (Integer, Int, Int) -> Bool
isHoliday m
m ((Integer, Int, Int) -> Bool) -> (Integer, Int, Int) -> Bool
forall a b. (a -> b) -> a -> b
$ Day -> (Integer, Int, Int)
toGregorian Day
d)
hBusinessDayBetween :: m->(Day, Day)->Int
hBusinessDayBetween m
m (Day
fd, Day
td) = (Int -> Day -> Int) -> Int -> [Day] -> Int
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl Int -> Day -> Int
countDays Int
0 [Day]
listOfDates
where countDays :: Int -> Day -> Int
countDays Int
counter Day
x = Int
counter Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Bool -> Int
forall a. Enum a => a -> Int
fromEnum (m -> Day -> Bool
forall m. Holiday m => m -> Day -> Bool
isBusinessDay m
m Day
x)
listOfDates :: [Day]
listOfDates = (Day, Day) -> [Day]
getDaysBetween (Day
fd, Day
td)
getDaysBetween :: (Day, Day) -> [Day]
getDaysBetween :: (Day, Day) -> [Day]
getDaysBetween (Day
fd, Day
td) = [Day] -> [Day]
forall a. [a] -> [a]
reverse ([Day] -> [Day]) -> [Day] -> [Day]
forall a b. (a -> b) -> a -> b
$ Day -> [Day] -> [Day]
generator Day
fd []
where generator :: Day -> [Day] -> [Day]
generator Day
date [Day]
x
| Day
date Day -> Day -> Bool
forall a. Ord a => a -> a -> Bool
< Day
td = Day -> [Day] -> [Day]
generator Day
nextDate (Day
nextDate Day -> [Day] -> [Day]
forall a. a -> [a] -> [a]
: [Day]
x)
| Bool
otherwise = [Day]
x
where nextDate :: Day
nextDate = Integer -> Day -> Day
addDays Integer
1 Day
date
isWeekEnd :: Day->Bool
isWeekEnd :: Day -> Bool
isWeekEnd Day
d = (DayOfWeek
weekday DayOfWeek -> DayOfWeek -> Bool
forall a. Eq a => a -> a -> Bool
== DayOfWeek
Saturday) Bool -> Bool -> Bool
|| (DayOfWeek
weekday DayOfWeek -> DayOfWeek -> Bool
forall a. Eq a => a -> a -> Bool
== DayOfWeek
Sunday)
where weekday :: DayOfWeek
weekday = Day -> DayOfWeek
dayOfWeek Day
d
getNextBusinessDay :: Holiday a => a->Day->Day
getNextBusinessDay :: forall a. Holiday a => a -> Day -> Day
getNextBusinessDay a
m Day
d
| a -> Day -> Bool
forall m. Holiday m => m -> Day -> Bool
isBusinessDay a
m Day
nextDay = Day
nextDay
| Bool
otherwise = a -> Day -> Day
forall a. Holiday a => a -> Day -> Day
getNextBusinessDay a
m Day
nextDay
where nextDay :: Day
nextDay = Integer -> Day -> Day
addDays Integer
1 Day
d