How do I convert boost::posix_time::ptime to time_t?

后端 未结 5 1977
孤独总比滥情好
孤独总比滥情好 2020-12-05 06:49

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

5条回答
  •  自闭症患者
    2020-12-05 07:18

    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());
    }
    

提交回复
热议问题