1

I have UTCTimeStamp Value {1507272767979279596} and I want to convert into human readable format.

for example UTCTimestamp = 1507272767979279596 and Human readable form is = Friday, October 6, 2017 12:22:47.979 PM

is there any function in C++ STL or Boost, which can get me above output.

Nespl NS3
  • 42
  • 9
  • 1
    `strftime` could be a solution (possible [duplicate](https://stackoverflow.com/questions/14436462/c-time-stamp-to-human-readable-datetime-function)?) – Vivick Oct 06 '17 at 07:27

1 Answers1

1

Fair warning: you look to have a weird timezone (+05:30?). Other than that:

Live On Coliru

#include <ctime>
#include <string>

std::string usingStrftime(const time_t rawtime)
{
    struct tm * dt = localtime(&rawtime);
    char buffer[30];
    strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", dt);
    return std::string(buffer);
}

#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/conversion.hpp>
#include <boost/date_time/posix_time/ptime.hpp>

boost::posix_time::ptime as_ptime(uintmax_t ns) {
    return {{1970,1,1}, boost::posix_time::microseconds(ns/1000)};
}

#include <iostream>

int main() {
    std::cout << usingStrftime(1507272767) << "\n";

    auto timestamp = as_ptime(1507272767979279596ull);

    std::cout << timestamp << "\n";

    std::cout.imbue(std::locale(std::cout.getloc(), new boost::posix_time::time_facet("%A %b %d, %Y %I:%M:%S %p")));
    std::cout << timestamp << "\n";
}

Prints

2017-10-06 06:52:47
2017-Oct-06 06:52:47.979279
Friday Oct 06, 2017 06:52:47 AM
sehe
  • 350,152
  • 45
  • 431
  • 590