如何根据 RFC 3339 格式化 boost::date_time-object

发布于 2024-07-12 13:06:29 字数 732 浏览 7 评论 0原文

我想在 boost 中使用 date_time 库来表示我的应用程序中的时间。 此应用程序将生成 Atom 提要,而 Atom 提要又要求采用 RFC 3339< 中指定的格式的时间戳/a>,例如“1990-12-31T23:59:60Z”或“1990-12-31T15:59:60-08:00”。

那么,如何根据这个 RFC 格式化时间呢?

我一直在阅读 日期时间输入/输出文档< /a> 一整天,我似乎不知道如何在需要时将 Z 放在末尾。 此外,RFC 支持可选的小数秒,但仅支持一位数字(例如“1990-12-31T23:59:60.5Z”)(*)。 我似乎也不知道如何做到这一点。

我总是可以编写自己的格式化例程来读出不同的所需字段,但在我看来,这违背了 date_time 库的原则。

有为这个库编写格式化程序的经验吗? 或者我做错了事?

(*):在我看来,RFC 中给出的 ABNF 只允许一位数的小数秒,但同一 RFC 中的示例具有两位数的小数秒。 那是什么意思?

I want to use the date_time library in boost to represent time in my application. This application will generate Atom feeds, which in turn mandates time-stamps in the format specified in RFC 3339, for example "1990-12-31T23:59:60Z" or "1990-12-31T15:59:60-08:00".

So, how do I format time according to this RFC?

I have been reading the Date Time Input/Output documentation all day, and I can't seem to find out how to put the Z at the end when I need it. Also, the RFC supports an optional fractional second, but only one digit of it (eg. "1990-12-31T23:59:60.5Z") (*). I can't seem to find out how to do this either.

I could always write my own formatting routine that reads out the different needed fields, but that seems to me to be working against the grain of the date_time library.

Any experience with writing formatters for this library? Or am I doing the wrong thing?

(*): It seems to me that the ABNF given in the RFC only allows one-digit fractional seconds, but the examples in the same RFC have two-digit fractional seconds. What is that supposed to mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坚持沉默 2024-07-19 13:06:29
  1. RFC 中的 ABNF 表示点后必须至少有一位数字,没有定义最大值。

  2. 没有真正需要 Z,您可以使用 00:00 代替,这可以通过构面实现

  3. 在极少数情况下,date_time 将生成“Z”。 请参阅 boost (local_date_time.hpp) 中的代码快照,该快照显示如下:


    std::string zone_name(bool as_offset=false) const
    {
      if(zone_ == boost::shared_ptr()) {
        if(as_offset) {
          return std::string("Z");
        }
        else {
          return std::string("Coordinated Universal Time");
        }
    ...

zone_abbrev 函数中有类似的 if ...

的示例用法

以及此

slimak@daradei:~/store/kodowanie/moje/test$ cat boost_date_time.cpp
#include "boost/date_time.hpp"
#include "boost/date_time/local_time/local_time.hpp"

using namespace boost::posix_time;
using namespace boost::local_time;

int main()
{
        local_date_time t = local_sec_clock::local_time(time_zone_ptr());
        local_time_facet* lf(new local_time_facet("%Y-%m-%dT%H:%M:%S%F%Q"));
        std::cout.imbue(std::locale(std::cout.getloc(), lf));
        std::cout << t << std::endl;
        return 0;
}
slimak@daradei:~/store/kodowanie/moje/test$ g++ boost_date_time.cpp && ./a.out
2009-01-30T12:15:56Z
slimak@daradei:~/store/kodowanie/moje/test$

  1. ABNF from RFC says that there must be at least one digit after dot, there is no defined maximum.

  2. There is no real need for the Z, you can use 00:00 instead, and this is possible with facets

  3. In some rare circumstances date_time will generate a "Z". See code snapshot from boost (local_date_time.hpp) that suggests this is below:


    std::string zone_name(bool as_offset=false) const
    {
      if(zone_ == boost::shared_ptr()) {
        if(as_offset) {
          return std::string("Z");
        }
        else {
          return std::string("Coordinated Universal Time");
        }
    ...

There is similar if in zone_abbrev function...

And example usage of this

slimak@daradei:~/store/kodowanie/moje/test$ cat boost_date_time.cpp
#include "boost/date_time.hpp"
#include "boost/date_time/local_time/local_time.hpp"

using namespace boost::posix_time;
using namespace boost::local_time;

int main()
{
        local_date_time t = local_sec_clock::local_time(time_zone_ptr());
        local_time_facet* lf(new local_time_facet("%Y-%m-%dT%H:%M:%S%F%Q"));
        std::cout.imbue(std::locale(std::cout.getloc(), lf));
        std::cout << t << std::endl;
        return 0;
}
slimak@daradei:~/store/kodowanie/moje/test$ g++ boost_date_time.cpp && ./a.out
2009-01-30T12:15:56Z
slimak@daradei:~/store/kodowanie/moje/test$

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文