In this example we will discuss how to represent Date as an object in c++ using Boost Library.
C++ – Boost Library provides a type to represent date in Georgian Calendar i.e.
boost::gregorian::date
Representing a Date with boost::gregorian::date type
Let’s see how to create a Date Object and initialize it with a date 21’s March 2016 i.e.
boost::gregorian::date dateObj { 2016, 3, 21 };
Accessing day, month & year from a date object
Access day of month using day() member function i.e.
std::cout << dateObj.day() << '\n';
Access month using month() member function i.e.
std::cout << dateObj.month() << '\n';
Access year using year() member function i.e.
Frequently Asked:
std::cout << dateObj.year() << '\n';
Access day of weak using year() member function i.e.
std::cout << dateObj.day_of_week() << '\n';
Let’s see a complete example,
#include <boost/date_time/gregorian/gregorian.hpp> #include <iostream> int main() { // Initializing Date object with 21st March 2016 boost::gregorian::date dateObj { 2016, 3, 21 }; // Accessing Day of Month from Date object std::cout << "Day of Month = " << dateObj.day() << '\n'; // Accessing Month from Date object std::cout << "Month = " << dateObj.month() << '\n'; // Accessing Year from Date object std::cout << "Year = " << dateObj.year() << '\n'; // Accessing Day of week from Date object std::cout << "Day of Week = " << dateObj.day_of_week() << '\n'; // Printing complete Date object on console std::cout << "Date = " << dateObj << '\n'; }
Output:
Day of Month = 21 Month = Mar Year = 2016 Day of Week = Mon Date = 2016-Mar-21
Initializing Date object with Current Date
Let’s see how to initialize a date object with todays date instead of hard coded dates i.e.
Initializing Date object with current Date in Local Timezone,
boost::gregorian::date todaysDateInLocalTZ = boost::gregorian::day_clock::local_day();
Initializing Date object with current Date in UTC Timezone,
boost::gregorian::date todaysDateInUTCTZ = boost::gregorian::day_clock::local_day();
Checkout the example as follows,
#include <boost/date_time/gregorian/gregorian.hpp> #include <iostream> int main() { // Initializing Date object with today's in Local Time Zone boost::gregorian::date todaysDateInLocalTZ = boost::gregorian::day_clock::local_day(); // Printing complete Date object on console std::cout << "Date in System's Time Zone= " << todaysDateInLocalTZ << '\n'; // Initializing Date object with today's in UTC Time Zone boost::gregorian::date todaysDateInUtcTZ = boost::gregorian::day_clock::universal_day(); // Printing complete Date object on console std::cout << "Date in UTC Time Zone = " << todaysDateInUtcTZ << '\n'; }
Output:
Date in System's Time Zone= 2016-Oct-21 Date in UTC Time Zone = 2016-Oct-22
My System is in Anchorage Time Zone where Date is 21st October at this point of time, therefore according to UTC Timezone Date is 22nd October.
Pointers in C/C++ [Full Course]
To compile the above examples use following command in linux,
g++ –std=c++11 example.cpp -lboost_date_time