In this article we will discuss how to calculate difference between two dates and how to iterate over a date range using C++ Boost Date Time Library.
date_duration
Boost Date Time Library provides a duration type to represent the duration between two dates i.e.
boost::gregorian::date_duration
With this date_duration object we can get the duration between 2 dates in days i.e.
// Initializing Date object with 21st March 2016 boost::gregorian::date dateObj1 { 2016, 3, 21 }; // Initializing Date object with 1st October 2016 boost::gregorian::date dateObj2 { 2016, 10, 1 }; // Get the Duration between 2 Dates boost::gregorian::date_duration dur = dateObj2 - dateObj1; // Fetch Days from date_duration object std::cout<<"Difference b/w 2 Dates in days : "<<dur.days()<<std::endl;
We can also add and subtract this date_duration object from a date object. For example, let’s add 3 week duration in a date object i.e.
Frequently Asked:
// Create Duration object representing 3 week duration boost::gregorian::weeks_duration weekdur(3); // Add 3 week Duration in Date (1-Oct-2016) boost::gregorian::date dateObj3 = dateObj2 + weekdur;
Complete Example is as follows,
#include <boost/date_time/gregorian/gregorian.hpp> #include <iostream> int main() { // Initializing Date object with 21st March 2016 boost::gregorian::date dateObj1 { 2016, 3, 21 }; // Initializing Date object with 1st October 2016 boost::gregorian::date dateObj2 { 2016, 10, 1 }; // Get the Duration between 2 Dates boost::gregorian::date_duration dur = dateObj2 - dateObj1; std::cout << "Date 1 : " << dateObj1 << std::endl; std::cout << "Date 2 : " << dateObj2 << std::endl; // Fetch Days from date_duration object std::cout << "Difference b/w 2 Dates in days : " << dur.days() << std::endl; // Create Duration object representing 3 week duration boost::gregorian::weeks_duration weekdur(3); // Add 3 week Duration in Date (1-Oct-2016) boost::gregorian::date dateObj3 = dateObj2 + weekdur; std::cout << "Date after adding 3 weeks in 1-Oct-2016 : " << dateObj3 << std::endl; }
Output:
Date 1 : 2016-Mar-21 Date 2 : 2016-Oct-01 Difference b/w 2 Dates in days : 194 Date after adding 3 weeks in 1-Oct-2016 : 2016-Oct-22
Date Period
Boost Date Time Library provides a type boost::gregorian::date_period to represents a period between two Dates i.e.
// Initializing Date object with 21st March 2016 boost::gregorian::date dateObj1 { 2016, 3, 21 }; // Initializing Date object with 1st October 2016 boost::gregorian::date dateObj2 { 2016, 10, 1 }; // Creating a date period from 03/21/2016 to 10/1/2016 boost::gregorian::date_period dp(dateObj1, dateObj2);
We can fetch days, months and years in this date period and can also iterate over it.
Best Resources to Learn C++:
Fetching date_duration object from date_period,
dp.length() ;
Iterate over the Date Period
date_period provides month, week and day iterators to iterate over a date period. Let’s see a a monthly iterator to iterate between 2 dates i.e.
// Iterate Over the Date Period using Month Iterator boost::gregorian::month_iterator it = dp.begin(); while (*it < dp.end()) { ++it; std::cout << (*it).month() << std::endl; }
Similarly date_period provides week and day iterators to iterate over a date period i.e.
boost::gregorian::week_iterator
boost::gregorian::day_iterator
Complete Example is as follows,
#include <boost/date_time/gregorian/gregorian.hpp> #include <iostream> int main() { // Initializing Date object with 21st March 2016 boost::gregorian::date dateObj1 { 2016, 3, 21 }; // Initializing Date object with 1st October 2016 boost::gregorian::date dateObj2 { 2016, 10, 1 }; // Creating a date period from 03/21/2016 to 10/1/2016 boost::gregorian::date_period dp(dateObj1, dateObj2); // Get the Duration object from date period // It will print Number of Days in Date Period std::cout << dp.length() << std::endl; std::cout << "Months in Date period : " << std::endl; // Iterate Over the Date Period using Month Iterator boost::gregorian::month_iterator it = dp.begin(); while (*it < dp.end()) { ++it; std::cout << (*it).month() << std::endl; } }
Output:
194 Months in Date period : Apr May Jun Jul Aug Sep Oct
Let’s see a practical example of day_iterator of date_period i.e.
Iterate Over last 20 Years and print how many times 1st Jan was on Sunday
#include <boost/date_time/gregorian/gregorian.hpp> #include <iostream> #include <algorithm> int main() { // Initializing Date object with 1 Jan 1996 boost::gregorian::date dateObj1 { 1996, 1, 1 }; // Initializing Date object with 1st Jan 2016 boost::gregorian::date dateObj2 { 2016, 1, 1 }; // Creating a date period from 1/1/1996 to 1/1/2016 boost::gregorian::date_period dp(dateObj1, dateObj2); std::cout << "All Sundays on 1st Jan : " << std::endl; boost::gregorian::day_iterator dayit = dp.begin(); while (*dayit <= dp.end()) { // Check if day of week is SUNDAY and Date is 1 and months is January if (dayit->day_of_week() == boost::gregorian::Sunday && dayit->day() == 1 && dayit->month() == 1) std::cout << "Sunday = " << (*dayit) << std::endl; ++dayit; } }
Output:
All Sundays on 1st Jan : Sunday = 2006-Jan-01 Sunday = 2012-Jan-01
To compile the above examples in Linux, use following command,
g++ –std=c++11 example.cpp -lboost_date_time
Thanks for sharing this awesome thrick. Bookmarked your blog to catch more from you.