如何将 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!
相关文章