greenif
greenif

Reputation: 1093

C++ Poco Epoch(Unix) time convert to LocalTime string

I have a string with Epoch(Unix) time. Need to convert it to string with local time. Code bellow works, but give me UTC time, not local. 09-05-2017 21:55:44

using Poco::Timestamp;
using Poco::DateTimeFormatter;

Timestamp timeStamp;
timeStamp = Timestamp::fromEpochTime(jrnTime);
timeFormatted = DateTimeFormatter::format(timeStamp, "%d-%m-%Y %H:%M:%S");

Upvotes: 0

Views: 1718

Answers (2)

Alno
Alno

Reputation: 51

You can use Poco::LocalDateTime which takes Poco::DateTime and tzd and convert to the local date time.

Upvotes: 1

greenif
greenif

Reputation: 1093

Don't need to use Poco for this purpose.

#include <ctime>
using std::time_t;
using std::wstring;
using std::string;

string TimeStamp(time_t rawtime) {
    char buffer[80];
    struct tm timeinfo;
    localtime_s(&timeinfo, &rawtime);
    strftime(buffer, sizeof(buffer), "%d-%m-%Y %H:%M:%S", &timeinfo);
    return string(buffer);
}

wstring TimeStampW(time_t rawtime) {
    wchar_t buffer[80];
    struct tm timeinfo;
    localtime_s(&timeinfo, &rawtime);
    wcsftime(buffer, sizeof(buffer), L"%d-%m-%Y %H:%M:%S", &timeinfo);
    return wstring(buffer);
}

Upvotes: -1

Related Questions