【问题标题】:Log4cpp: Print date in UTC/GMT time zoneLog4cpp:在 UTC/GMT 时区打印日期
【发布时间】:2025-12-26 21:40:12
【问题描述】:

我正在使用 log4cpp。有没有办法打印转换为 GMT/UTC 时区的 log4cpp 中的时间戳/日期?

目前我使用 %d 打印本地时间,但我希望使用 UTC/GMT。

【问题讨论】:

    标签: c++ log4cpp


    【解决方案1】:

    根据this old thread,没有支持:

    ankit tandon пишет:

    大家好, 目前有什么方法可以在 log4cpp 中打印时间戳/日期 转换为 GMT/UTC 时区。我在某处读到使用“%d”打印 UTC 日期,但我们仍在获取本地系统(美国)时间。 对此的任何帮助将不胜感激。

    不,无法使用 PatternLayout 打印 UTC 时间。但是您可以编写自己的附加程序来打印 UTC 时间戳/日期。

    这与 documentation 声称 %d 打印 UTC 和 %D 本地时间不一致。

    所以我检查了 log4cpp-1.1.3.tar.gz 中的代码以了解事实。好像一直是localtime

    std::time_t t = event.timeStamp.getSeconds();
    localtime(&t, &currentTime);
    

    也许您可以简单地修补该行以改用gmtime

    【讨论】: