如何将 boost::posix_time::ptime 转换为 time_t?

发布于 2024-10-08 02:53:02 字数 70 浏览 5 评论 0原文

是否有一些“标准”方法或者我能做的最好的方法是通过从 gregorian::date(1970,1,1) 中减去来直接计算它?

Is there some "standard" way or the best I can do is to compute it directly by subtracting from gregorian::date(1970,1,1)?

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

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

发布评论

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

评论(5

我不在是我 2024-10-15 02:53:02

由于@icecrime的方法转换两次(ptime内部使用线性表示),我决定使用直接计算。这是:

time_t to_time_t(boost::posix_time::ptime t)
{
    using namespace boost::posix_time;
    ptime epoch(boost::gregorian::date(1970,1,1));
    time_duration::sec_type x = (t - epoch).total_seconds();

    // ... check overflow here ...

    return time_t(x);
}

编辑:感谢@jaaw 让我注意到这一点。自 boost 1.58 起,此函数包含在 date_time/posix_time/conversion.hppstd::time_t to_time_t(ptime pt) 中。

Since @icecrime's method converts twice (ptime uses linear representation internally), I've decided to use direct computation instead. Here it is:

time_t to_time_t(boost::posix_time::ptime t)
{
    using namespace boost::posix_time;
    ptime epoch(boost::gregorian::date(1970,1,1));
    time_duration::sec_type x = (t - epoch).total_seconds();

    // ... check overflow here ...

    return time_t(x);
}

EDIT: Thanks @jaaw for bringing this to my attention. Since boost 1.58 this function is included in date_time/posix_time/conversion.hpp, std::time_t to_time_t(ptime pt).

南冥有猫 2024-10-15 02:53:02

time_t 是用于保存以秒为单位的时间(通常是纪元时间)的类型。我猜你是在纪元时间之后,如果是这样,除了你已经进行的减法之外,我不知道有任何方法可以直接直接获得纪元时间。获得 time_duration(减法结果)后,您可以对持续时间调用 total_seconds() 并将其存储在 time_t 中。

顺便提一句。如果您在纪元时间之后,您可以简单地使用 gettimeofday() 来避免一些头痛!

time_t is the type used to hold time in seconds (typically epoch time). I'm guessing you are after epoch time, if so I'm not aware of any way in boost of actually getting epoch time directly, aside from the subtraction you have already. Once you have a time_duration (result of the subtraction), you can call total_seconds() on the duration and store that in time_t.

btw. if you are after epoch time, you could simple use gettimeofday() and save yourself some headache!

剧终人散尽 2024-10-15 02:53:02

这是 @ybungalobill 方法的一个变体,可以让你度过 2038 年,以防万一。 :)

int64_t rax::ToPosix64(const boost::posix_time::ptime& pt)
{
  using namespace boost::posix_time;
  static ptime epoch(boost::gregorian::date(1970, 1, 1));
  time_duration diff(pt - epoch);
  return (diff.ticks() / diff.ticks_per_second());
}

Here's a variation of @ybungalobill's method that will get you past 2038, just in case. :)

int64_t rax::ToPosix64(const boost::posix_time::ptime& pt)
{
  using namespace boost::posix_time;
  static ptime epoch(boost::gregorian::date(1970, 1, 1));
  time_duration diff(pt - epoch);
  return (diff.ticks() / diff.ticks_per_second());
}
森林迷了鹿 2024-10-15 02:53:02

我相信你能做的最好的事情就是使用 to_tm 获取 tmmktime tm 转换为 time_t

I believe the best you can do is using to_tm to get a tm and mktime to convert the tm to a time_t.

祁梦 2024-10-15 02:53:02

这两行应该可以做到。

tm td_tm = to_tm(pt);
time_t tt = mktime(&td_tm);

These 2 lines should do it.

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