Commit 0093452f authored by Michael Ott's avatar Michael Ott
Browse files

Add new timestamp.h

parent 9493ece6
/*
* timestamp.h
*
* Created on: 10 Feb 2017
* Author: ottmi
*/
#ifndef INCLUDE_TIMESTAMP_H_
#define INCLUDE_TIMESTAMP_H_
#include <boost/date_time/posix_time/posix_time.hpp>
#include <string>
#include <sstream>
#define S_TO_NS(x) x*1000000000ll
#define MS_TO_NS(x) x*1000000ll
#define US_TO_NS(x) x*1000ll
static uint64_t getTimestamp() {
boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
boost::posix_time::time_duration diff = now - epoch;
return diff.total_nanoseconds();
}
static uint64_t ptime2Timestamp(const boost::posix_time::ptime t) {
boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
boost::posix_time::time_duration diff = t - epoch;
return diff.total_nanoseconds();
}
static boost::posix_time::ptime timestamp2ptime(const uint64_t ts) {
return boost::posix_time::ptime(boost::gregorian::date(1970,1,1), boost::posix_time::nanoseconds(ts));
}
static std::string prettyPrintTimestamp(const uint64_t ts) {
std::stringstream ss;
ss << ts / 1000000000 << "." << std::setw(9) << std::setfill('0') << ts % 1000000000;
return ss.str();
}
#endif /* INCLUDE_TIMESTAMP_H_ */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment