【问题标题】:Poco DateTimeFormatter - Print timestamp with current timezonePoco DateTimeFormatter - 使用当前时区打印时间戳
【发布时间】:2019-01-07 11:16:09
【问题描述】:

如何根据当前时区将Poco::TimestampPoco::DateTimeFormatter 打印到格式化的日期时间?

我有一个print_pretty_datetime(const Poco::Timestamp &now),在那里我会收到一个Poco::Timestamp,所以很遗憾我不能使用Poco::LocalDateTime

MCVE:

#include "Poco/Timestamp.h"
#include "Poco/Timezone.h"
#include "Poco/DateTimeFormatter.h"
#include "Poco/DateTimeFormat.h"

#include <iostream>
#include <string>

// Cannot change the method signature. I will receive a Poco::Timestamp object
std::string print_pretty_datetime(const Poco::Timestamp &now)
{
    return Poco::DateTimeFormatter::format(
       now,
       Poco::DateTimeFormat::SORTABLE_FORMAT,
       Poco::Timezone::tzd()
     );
}


int main()
{
     Poco::Timestamp now;
     std::string dt_now = print_pretty_datetime(now);

     std::cout << dt_now << '\n';

     return 0;
}

例如:返回的字符串是2019-01-07 11:10:12(因此是UTC+0),而我的设备在UTC+1

其实date这个命令会返回Mon Jan 7 12.10.12 CET 2019

Poco::DateTimeFormatter::format 中的 tzd 的正确参数是什么,用于根据当前语言环境打印日期时间?


系统信息:

SMP Debian 4.9.130-2 (2018-10-27) x86_64 GNU/Linux
g++ (Debian 6.3.0-18+deb9u1) 6.3.0 20170516
Poco 1.9.0

【问题讨论】:

    标签: c++ datetime locale unix-timestamp poco-libraries


    【解决方案1】:

    通过Poco::LocalDateTime获取本地时间,然后读取timetemp并将其传递给格式化程序:

    Poco::LocalDateTime dateTime;
    Poco::Timestamp now = dateTime.timestamp();
    

    您需要定义自己的字符串格式,其中包含有关时区的信息,因为 SORTABLE_FORMAT 不处理它。

    提示(您可以打开DateTimeFormatter 的源代码并查看timeZoneDifferential 是如何在append 函数中处理的 - 此参数不会影响输出中的小时)。 因此,如果您更改为:

     std::string dt_now = Poco::DateTimeFormatter::format(
       now,
       "%H:%M:%S %z,%Z",
       Poco::Timezone::tzd()
     );
    

    你会在输出中看到HH::MM::SS +01:00,+0100

    【讨论】:

    • 嘿,好点(我在回答中也提到过)。我已经编辑了我的问题,因为它不够清楚。我无法控制Timestamp:这是我将在我的函数中收到的参数,因此我必须根据它打印当前日期时间
    【解决方案2】:

    您可以通过中间的Poco::DateTime 对象从Poco::Timestamp 在系统的当前时区创建Poco::LocalDateTime

    #include "Poco/LocalDateTime.h"
    #include "Poco/DateTime.h"
    #include "Poco/DateTimeFormat.h"
    #include "Poco/DateTimeFormatter.h"
    #include <iostream>
    
    int main(int argc, char** argv)
    {
        Poco::Timestamp ts;
        Poco::DateTime dt(ts);
        Poco::LocalDateTime ldt(dt);
    
        std::string str = Poco::DateTimeFormatter::format(ldt, Poco::DateTimeFormat::SORTABLE_FORMAT);
        std::cout << str << std::endl;
    
        return 0;
    }
    

    【讨论】:

    • 酷,想不通从TimestampLocalDateTime的段落
    【解决方案3】:

    好的,如果您不一定需要使用Poco::Timestamp,解决方案可以简单地使用Poco::LocalDateTime

    #include "Poco/LocalDateTime.h"
    #include "Poco/DateTimeFormatter.h"
    #include "Poco/DateTimeFormat.h"
    
    #include <iostream>
    #include <string>
    
    int main()
    {
        Poco::LocalDateTime now;
    
        std::string dt_now = Poco::DateTimeFormatter::format(
          now,
          Poco::DateTimeFormat::SORTABLE_FORMAT
        );
    
        std::cout << dt_now << '\n';
        return 0;
    }
    

    这将根据当前时区打印日期。

    我仍然必须使用Poco::Timestamp,因为我会将它作为我的print_pretty_datetime(const pc::Timestamp &amp;now) 的参数,所以这个答案还不适用于我的情况。

    【讨论】:

      猜你喜欢
      • 2011-05-08
      • 1970-01-01
      • 2017-06-07
      • 1970-01-01
      • 2020-09-07
      • 1970-01-01
      • 1970-01-01
      • 2012-03-23
      • 1970-01-01
      相关资源
      最近更新 更多