Nespl NS3
Nespl NS3

Reputation: 52

Convert UTCTime to Local Time

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.

Upvotes: 1

Views: 84

Answers (1)

sehe
sehe

Reputation: 392863

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

Upvotes: 1

Related Questions