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,
1 |
boost::posix_time::ptime |
to specify a particular time point. It includes a date and timestamp representing a specific time point.
For example,
1 |
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.
1 2 3 4 5 6 7 8 9 |
// 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
1 2 |
// 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.
1 2 3 4 5 6 7 8 9 10 11 12 |
// 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
1 |
boost::posix_time::ptime timeUTC = boost::posix_time::second_clock::universal_time(); |
Complete example is as follows,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#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:
1 2 3 4 5 6 7 8 9 10 |
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
If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.
Leave a Reply