Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
dateutilities.cpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2021 Quaternion Risk Management Ltd
5 All rights reserved.
6
7 This file is part of ORE, a free-software/open-source library
8 for transparent pricing and risk analysis - http://opensourcerisk.org
9
10 ORE is free software: you can redistribute it and/or modify it
11 under the terms of the Modified BSD License. You should have received a
12 copy of the license along with this program.
13 The license is also available online at <http://opensourcerisk.org>
14
15 This program is distributed on the basis that it will form a useful
16 contribution to risk analytics and model standardisation, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
22
23namespace QuantExt {
24namespace DateUtilities {
25
26using QuantLib::Date;
27using QuantLib::Month;
28using QuantLib::Weekday;
29using QuantLib::Year;
30
31Date lastWeekday(Weekday dayOfWeek, Month m, Year y) {
32 Date endOfMonth = Date::endOfMonth(Date(1, m, y));
33 Weekday lastWeekDayOfTheMonth = endOfMonth.weekday();
34 auto lastDayOfMonth = endOfMonth.dayOfMonth();
35 if (lastWeekDayOfTheMonth >= dayOfWeek) {
36 return Date(lastDayOfMonth - (lastWeekDayOfTheMonth - dayOfWeek), m, y);
37 } else {
38 return Date(lastDayOfMonth - 7 + (dayOfWeek - lastWeekDayOfTheMonth), m, y);
39 }
40}
41
42} // namespace DateUtilities
43} // namespace QuantExt
helper functions for date operations
Date lastWeekday(Weekday dayOfWeek, Month m, Year y)