You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于C++14跨平台实现按用户区域设置格式化Epoch时间

跨平台C++14实现Epoch转本地化日期时间

一、手动跨平台实现

通过分别调用各平台原生API,结合预编译指令实现跨平台兼容:

Windows平台

利用Windows系统API完成Epoch转换与本地化格式化:

#include <windows.h>
#include <string>

std::string epoch_to_local_time(uint64_t epoch_sec) {
    // Epoch秒数转FILETIME格式
    uint64_t file_time = (epoch_sec + 11644473600ULL) * 10000000ULL;
    FILETIME ft;
    ft.dwLowDateTime = static_cast<DWORD>(file_time & 0xFFFFFFFF);
    ft.dwHighDateTime = static_cast<DWORD>(file_time >> 32);

    // 转本地时区SYSTEMTIME
    SYSTEMTIME st_utc, st_local;
    FileTimeToSystemTime(&ft, &st_utc);
    SystemTimeToTzSpecificLocalTime(nullptr, &st_utc, &st_local);

    // 获取用户区域的短日期、短时间格式
    wchar_t date_format[256], time_format[256];
    GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SSHORTDATE, date_format, sizeof(date_format)/sizeof(wchar_t));
    GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SSHORTTIME, time_format, sizeof(time_format)/sizeof(wchar_t));

    // 格式化日期时间
    wchar_t date_buf[256], time_buf[256];
    GetDateFormatEx(LOCALE_NAME_USER_DEFAULT, 0, &st_local, date_format, date_buf, sizeof(date_buf)/sizeof(wchar_t), nullptr);
    GetTimeFormatEx(LOCALE_NAME_USER_DEFAULT, 0, &st_local, time_format, time_buf, sizeof(time_buf)/sizeof(wchar_t), nullptr);

    // 转换为std::string返回
    std::wstring w_result = std::wstring(date_buf) + L" " + std::wstring(time_buf);
    return std::string(w_result.begin(), w_result.end());
}

Linux/macOS平台

基于POSIX标准API实现,类Unix系统通用:

#include <ctime>
#include <string>
#include <clocale>

std::string epoch_to_local_time(uint64_t epoch_sec) {
    // 设置为用户当前区域
    setlocale(LC_ALL, "");

    struct tm tm_local;
    time_t epoch = static_cast<time_t>(epoch_sec);
    // 线程安全的本地时间转换
    localtime_r(&epoch, &tm_local);

    char buf[256];
    // 用区域默认的短日期+短时间格式格式化
    strftime(buf, sizeof(buf), "%x %X", &tm_local);
    return std::string(buf);
}

跨平台整合代码

通过预编译指令区分平台:

#include <string>

#ifdef _WIN32
#include <windows.h>
std::string epoch_to_local_time(uint64_t epoch_sec) {
    // 上述Windows平台实现代码
}
#elif __APPLE__ || __linux__
#include <ctime>
#include <clocale>
std::string epoch_to_local_time(uint64_t epoch_sec) {
    // 上述Linux/macOS平台实现代码
}
#endif

二、第三方库方案

1. Howard Hinnant的date库

轻量级头文件库,支持C11+,是C20 <chrono> 特性的前身,自动处理区域设置:

#include "date/date.h"
#include <chrono>
#include <string>
#include <sstream>

std::string epoch_to_local_time(uint64_t epoch_sec) {
    using namespace date;
    using namespace std::chrono;

    // 转换为系统时钟时间点
    auto tp = system_clock::time_point{seconds{epoch_sec}};
    // 绑定当前时区
    zoned_time zt{current_zone(), tp};
    
    std::ostringstream oss;
    oss << zt;
    return oss.str();
}

2. Boost.DateTime

适合已引入Boost库的项目,跨平台支持完善:

#include <boost/date_time/local_time/local_time.hpp>
#include <string>
#include <sstream>

std::string epoch_to_local_time(uint64_t epoch_sec) {
    using namespace boost::local_time;
    using namespace boost::posix_time;

    ptime epoch(boost::gregorian::date(1970, 1, 1));
    ptime utc_time = epoch + seconds(epoch_sec);

    // 获取本地时区
    time_zone_ptr tz = local_time_zone_ptr();
    local_date_time local_time(utc_time, tz);

    std::ostringstream oss;
    oss << local_time;
    return oss.str();
}

3. Qt的QDateTime

Qt项目专属方案,内置系统区域适配:

#include <QDateTime>
#include <QString>

QString epoch_to_local_time(uint64_t epoch_sec) {
    QDateTime dt = QDateTime::fromSecsSinceEpoch(static_cast<qint64>(epoch_sec));
    return dt.toString(Qt::SystemLocaleShortDate);
}

内容的提问来源于stack exchange,提问作者Kaustubh Dwivedi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 16:00:35