如何用纳秒实例化 boost::date_time::time_duration?
How do I instantiate a boost::date_time::time_duration with nanoseconds?
我觉得我在这里遗漏了一些明显的东西。我可以通过执行以下操作轻松地以毫秒、微秒或秒创建 time_duration:
time_duration t1 = seconds(1);
time_duration t2 = milliseconds(1000);
time_duration t3 = microseconds(1000000);
但是没有纳秒的功能。将普通整数纳秒值转换为 time_duration 的技巧是什么?
我在 Debian 上使用 amd64 架构 Linux。升压版本 1.55.
boost::posix_time::microseconds
其实就是subsecond_duration<boost::posix_time::time_duration, 1000000>
。所以...
#include <boost/date_time/posix_time/posix_time.hpp>
using nanoseconds = boost::date_time::subsecond_duration<boost::posix_time::time_duration, 1000000000>;
int main() {
boost::posix_time::time_duration t = nanoseconds(1000000000);
std::cout << t << "\n";
}
版画
00:00:01
更新
确实,在 Boost DateTime 库的 Compile Options 中,您可以看到 select 纳秒分辨率的选项:
By default the posix_time system uses a single 64 bit integer
internally to provide a microsecond level resolution. As an
alternative, a combination of a 64 bit integer and a 32 bit integer
(96 bit resolution) can be used to provide nano-second level
resolutions. The default implementation may provide better performance
and more compact memory usage for many applications that do not
require nano-second resolutions.
To use the alternate resolution (96 bit nanosecond) the variable
BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
must be defined in the library
users project files (ie Makefile, Jamfile, etc). This macro is not
used by the Gregorian system and therefore has no effect when building
the library.
确实如此,您可以使用以下方式进行检查:
#define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
#include <boost/date_time/posix_time/posix_time.hpp>
int main() {
using namespace boost::posix_time;
std::cout << nanoseconds(1000000000) << "\n";
}
我觉得我在这里遗漏了一些明显的东西。我可以通过执行以下操作轻松地以毫秒、微秒或秒创建 time_duration:
time_duration t1 = seconds(1);
time_duration t2 = milliseconds(1000);
time_duration t3 = microseconds(1000000);
但是没有纳秒的功能。将普通整数纳秒值转换为 time_duration 的技巧是什么?
我在 Debian 上使用 amd64 架构 Linux。升压版本 1.55.
boost::posix_time::microseconds
其实就是subsecond_duration<boost::posix_time::time_duration, 1000000>
。所以...
#include <boost/date_time/posix_time/posix_time.hpp>
using nanoseconds = boost::date_time::subsecond_duration<boost::posix_time::time_duration, 1000000000>;
int main() {
boost::posix_time::time_duration t = nanoseconds(1000000000);
std::cout << t << "\n";
}
版画
00:00:01
更新
确实,在 Boost DateTime 库的 Compile Options 中,您可以看到 select 纳秒分辨率的选项:
By default the posix_time system uses a single 64 bit integer internally to provide a microsecond level resolution. As an alternative, a combination of a 64 bit integer and a 32 bit integer (96 bit resolution) can be used to provide nano-second level resolutions. The default implementation may provide better performance and more compact memory usage for many applications that do not require nano-second resolutions.
To use the alternate resolution (96 bit nanosecond) the variable
BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
must be defined in the library users project files (ie Makefile, Jamfile, etc). This macro is not used by the Gregorian system and therefore has no effect when building the library.
确实如此,您可以使用以下方式进行检查:
#define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
#include <boost/date_time/posix_time/posix_time.hpp>
int main() {
using namespace boost::posix_time;
std::cout << nanoseconds(1000000000) << "\n";
}