-
Notifications
You must be signed in to change notification settings - Fork 1
3.3 converting epoch seconds to dmyhms
Marshall Clow edited this page May 15, 2013
·
2 revisions
Given a number of seconds since the epoch date, calculate the year, month, day, hour, minute and second.
Boost DateTime makes this very easy. You create a boost::posix_time::ptime
from the epoch seconds, and then extract the information that you want from that.
#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <ctime>
using namespace boost::posix_time;
using namespace boost::gregorian;
int main ( int, char ** ) {
std::time_t t1(1368454200); // May 13, 2013 2:10 PM
ptime pt = from_time_t(t1);
// Split into date and time parts
time_duration td = pt.time_of_day ();
date d = pt.date ();
// Extract the bits
std::cout << "Date: " << d.year () << ' ' << d.month () << ' ' << d.day ()
<< ' ' << td.hours() << ':' << td.minutes() << ':' << td.seconds()
<< std::endl;
return 0;
}
This should produce the output Date: 2013 May 13 14:10:0