Get Current Date & Time in C++ Example using Boost Date Time Library

In this articlew we will discuss how to get Current Date & Time in C++ using Boost Date Time Library.

Boost Date Time Library provides a type,

boost::posix_time::ptime

to specify a particular time point. It includes a date and timestamp representing a specific time point.

For example,

boost::posix_time::ptime timeObj = boost::posix_time::time_from_string("2016/2/19 9:01:33.10");

Here timeObj represents a time point “2016/2/19 9:01:33.10”

It basically included a date and a time i.e.

// Get Date object from ptime object
boost::gregorian::date dateObj = timeLocal.date();

std::cout<<"Date Object = "<<dateObj<<std::endl;

// Get Time object from ptime object
boost::posix_time::time_duration durObj = timeLocal.time_of_day();

std::cout<<"Time Object = "<<durObj<<std::endl;

Getting Current Time

// Get current system time
boost::posix_time::ptime timeLocal = boost::posix_time::second_clock::local_time();

We can also get the date, month, year, hours, minute & Seconds from the ptime object using its member functions i.e.

// Get year Field from time object
timeLocal.date().year();
// Get Month Field from time Object
timeLocal.date().month();
// Get Day Field from time Object
timeLocal.date().day();
// Get Hours Field from time Object
timeLocal.time_of_day().hours();
// Get Minutes Field from time Object
timeLocal.time_of_day().minutes();
// Get Seconds Field from time Object
timeLocal.time_of_day().seconds();

Getting the Current Time in UTC Timezone

boost::posix_time::ptime timeUTC = boost::posix_time::second_clock::universal_time();

Complete example is as follows,

#include <boost/date_time.hpp>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

int main() {

	// Get current system time
	boost::posix_time::ptime timeLocal =
			boost::posix_time::second_clock::local_time();

	std::cout << "Current System Time = " << timeLocal << std::endl;

	// Get Date object from ptime object
	boost::gregorian::date dateObj = timeLocal.date();

	std::cout << "Date Object = " << dateObj << std::endl;

	// Get Time object from ptime object
	boost::posix_time::time_duration durObj = timeLocal.time_of_day();

	std::cout << "Time Object = " << durObj << std::endl;

	std::cout << "Year = " << timeLocal.date().year() << std::endl;
	std::cout << "Month = " << timeLocal.date().month() << std::endl;
	std::cout << "Day of Month = " << timeLocal.date().day() << std::endl;
	std::cout << "Hour of the Day = " << timeLocal.time_of_day().hours()<< std::endl;
	std::cout << "Minute : = " << timeLocal.time_of_day().minutes() << std::endl;
	std::cout << "Seconds : = " << timeLocal.time_of_day().seconds() << std::endl;

	// Get the current time in UTC Timezone
	boost::posix_time::ptime timeUTC =
			boost::posix_time::second_clock::universal_time();

	std::cout << "Current Time in UTC Timezone : " << timeUTC << std::endl;

}

Output:

Current System Time = 2016-Nov-14 22:58:03
Date Object = 2016-Nov-14
Time Object = 22:58:03
Year = 2016
Month = Nov
Day of Month = 14
Hour of the Day = 22
Minute : = 58
Seconds : = 3
Current Time in UTC Timezone : 2016-Nov-14 17:28:03

To Compile the above code, use following command in linux,

g++ –std=c++11 example.cpp -lboost_date_time

 

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top