boost::date_time 和 std::chrono 之间的互操作性

发布于 2024-10-16 05:05:32 字数 156 浏览 2 评论 0原文

boost::date_time 和 std::chrono 的互操作性如何?

例如,有没有办法在 boost::posix_time::ptime 和 std::chrono::time_point 之间进行转换?

我尝试搜索有关此类转换的文档,但找不到任何文档。

How interoperable are boost::date_time and std::chrono?

For example, is there a way to convert between boost::posix_time::ptime and std::chrono::time_point?

I tried searching for documentation on such conversions but couldn't find any.

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

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

发布评论

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

评论(2

隐诗 2024-10-23 05:05:32

您可以将 time_t 与 std::chrono::system_clock::time_point: 相互转换,

class system_clock
{
public:
    ...
    static time_t     to_time_t  (const time_point& __t);
    static time_point from_time_t(time_t __t);
};

并且可以将 time_t 转换为 ptime:

ptime from_time_t(time_t t);

但是我没有找到将 ptime 转换为 time_t 的方法。

You can convert a time_t to and from a std::chrono::system_clock::time_point:

class system_clock
{
public:
    ...
    static time_t     to_time_t  (const time_point& __t);
    static time_point from_time_t(time_t __t);
};

And you can convert a time_t to a ptime:

ptime from_time_t(time_t t);

However I don't see a way to convert a ptime to a time_t.

对风讲故事 2024-10-23 05:05:32

我在 boost 提交邮件列表中找到了这个: http://lists.boost .org/boost-commit/2009/04/15209.php

以下是相关函数:

template < class Clock, class Duration> 
struct convert_to<posix_time::ptime, chrono::time_point<Clock, Duration> > { 
    inline static posix_time::ptime apply(const chrono::time_point<Clock, Duration>& from) 
    { 
        typedef chrono::time_point<Clock, Duration> time_point_t; 
        typedef chrono::nanoseconds duration_t; 
        typedef duration_t::rep rep_t; 
        rep_t d = chrono::duration_cast<duration_t>(from.time_since_epoch()).count(); 
        rep_t sec = d/1000000000; 
        rep_t nsec = d%1000000000; 
        return boost::posix_time::from_time_t(0)+ 
        boost::posix_time::seconds(static_cast<long>(sec))+ 
        #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS 
        boost::posix_time::nanoseconds(nsec); 
        #else 
        boost::posix_time::microseconds((nsec+500)/1000); 
        #endif 
    } 
}; 

template < class Clock, class Duration> 
struct convert_to<chrono::time_point<Clock, Duration>, posix_time::ptime> { 
    inline static chrono::time_point<Clock, Duration> apply(const posix_time::ptime& from) 
    { 
        boost::posix_time::time_duration const time_since_epoch=from-boost::posix_time::from_time_t(0); 
        chrono::time_point<Clock, Duration> t=chrono::system_clock::from_time_t(time_since_epoch.total_seconds()); 
        long nsec=time_since_epoch.fractional_seconds()*(1000000000/time_since_epoch.ticks_per_second()); 
        return t+chrono::nanoseconds(nsec); 

    } 
}; 

我不确定它们何时会成为 boost 版本的一部分。他们现在好像不在后备箱里……

I found this on the boost commits mailing list: http://lists.boost.org/boost-commit/2009/04/15209.php

Here are the relevant functions:

template < class Clock, class Duration> 
struct convert_to<posix_time::ptime, chrono::time_point<Clock, Duration> > { 
    inline static posix_time::ptime apply(const chrono::time_point<Clock, Duration>& from) 
    { 
        typedef chrono::time_point<Clock, Duration> time_point_t; 
        typedef chrono::nanoseconds duration_t; 
        typedef duration_t::rep rep_t; 
        rep_t d = chrono::duration_cast<duration_t>(from.time_since_epoch()).count(); 
        rep_t sec = d/1000000000; 
        rep_t nsec = d%1000000000; 
        return boost::posix_time::from_time_t(0)+ 
        boost::posix_time::seconds(static_cast<long>(sec))+ 
        #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS 
        boost::posix_time::nanoseconds(nsec); 
        #else 
        boost::posix_time::microseconds((nsec+500)/1000); 
        #endif 
    } 
}; 

template < class Clock, class Duration> 
struct convert_to<chrono::time_point<Clock, Duration>, posix_time::ptime> { 
    inline static chrono::time_point<Clock, Duration> apply(const posix_time::ptime& from) 
    { 
        boost::posix_time::time_duration const time_since_epoch=from-boost::posix_time::from_time_t(0); 
        chrono::time_point<Clock, Duration> t=chrono::system_clock::from_time_t(time_since_epoch.total_seconds()); 
        long nsec=time_since_epoch.fractional_seconds()*(1000000000/time_since_epoch.ticks_per_second()); 
        return t+chrono::nanoseconds(nsec); 

    } 
}; 

I'm not sure when they're going to become part of a boost release. They don't seem to be in boost trunk right now...

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