【问题标题】:C++ Poco Epoch(Unix) time convert to LocalTime stringC ++ Poco Epoch(Unix)时间转换为LocalTime字符串
【发布时间】:2017-10-08 10:48:41
【问题描述】:
我有一个带有 Epoch(Unix) 时间的字符串。
需要将其转换为本地时间的字符串。
下面的代码有效,但给我UTC时间,而不是本地时间。
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");
【问题讨论】:
标签:
c++
datetime
poco
unix-timestamp
epoch
【解决方案1】:
您可以使用Poco::LocalDateTime,它采用Poco::DateTime 和tzd 并转换为本地日期时间。
【解决方案2】:
不需要为此目的使用 Poco。
#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);
}