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

How do I convert boost::posix_time::ptime to time_t?(如何将 boost::posix_time::ptime 转换为 time_t?)

本文介绍了如何将 boost::posix_time::ptime 转换为 time_t?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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

顺便说一句.如果你在时代之后,你可以简单地使用 gettimeofday() 并为自己省去一些麻烦!

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

解决方案

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!

这篇关于如何将 boost::posix_time::ptime 转换为 time_t?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何将 boost::posix_time::ptime 转换为 time_t?

基础教程推荐