【发布时间】:2013-10-25 11:13:45
【问题描述】:
我正在尝试使用 boost 库检索当前时间(以毫秒为单位)。下面是我用来获取当前时间(以毫秒为单位)的代码。
boost::posix_time::ptime time = boost::posix_time::microsec_clock::local_time();
boost::posix_time::time_duration duration( time.time_of_day() );
std::cout << duration.total_milliseconds() << std::endl;
uint64_t timestampInMilliseconds = duration.total_milliseconds() // will this work or not?
std::cout << timestampInMilliseconds << std::endl;
但这以 10 位数字打印出来,就像 17227676.. 我在我的 ubuntu 机器上运行我的代码.. 我相信它总是 13 位长值?不是吗?
在以毫秒为单位计算时间戳后,我需要使用下面的公式 -
int end = (timestampInMilliseconds / (60 * 60 * 1000 * 24)) % 14
但不知何故,我不确定我得到的 timestampInMilliseconds 是否正确?
首先我应该使用 boost::posix 还是不使用?我假设可能有更好的方法..我在我的 ubuntu 机器上运行代码..
更新:-
因为这段 bash 脚本会打印出 13 位的 timestampInMilliseconds ..
date +%s%N | cut -b1-13
【问题讨论】:
-
如果你有
1234的值,你是否希望它被打印为例如0000001234?除非要求,否则从不打印前导零。 -
我明白了。所以我在代码中得到的这个时间戳(以毫秒为单位)将是自纪元以来的毫秒数?
-
该值
17227676是一个持续时间,而不是时间戳。只有大约 4 小时 47 分钟。 -
hmmm.. 那我的 C++ 代码做错了吗?
标签: c++ date boost time milliseconds