问题描述
我需要获取系统时间(以纳秒为单位),并将其作为时间戳发送到influxdb。我知道
std::chrono::high_resolution_clock::Now()
以纳秒级给出当前系统时间,但是我无法将其转换为原始类型。
当我通过下面的代码时,我在数据库中看到类似1970-01-09T ...的内容
std::chrono::high_resolution_clock::Now().time_since_epoch().count()
解决方法
<div id="p5-2" class="flex-item">
<script>
// script 2
let sketch2 = function(p) {
p.setup = function() {
p.createCanvas(100,100);
p.background("orange");
}
//The p.draw function should in theory be positioned here
};
let node2 = document.createElement('div');
window.document.getElementById('p5-2').appendChild(node2);
new p5(sketch2,node2);
// script 3
</script>
</div>
<div id="p5-3" class="flex-item">
<script>
var sketch3 = function(p) {
p.setup = function() {
p.createCanvas(100,100);
p.background("green");
}
//The p.draw function should in theory be positioned here
};
let node3 = document.createElement('div');
window.document.getElementById('p5-3').appendChild(node3);
new p5(sketch3,node3);
</script>
返回time_point。您需要将其转换为持续时间,以获取纳秒数。
您可能正在寻找的是自大纪元以来的时间,因此您可以执行类似的操作
now()
,
您需要使用std::chrono::system_clock
而不是high_resolution_clock
。自1970年以来(这正是influxdb的期望值),只有system_clock
能可靠地测量时间。
第二,不能保证system_clock
以纳秒为单位测量时间,但是您可以对此进行校正。
这里是如何使平台最佳地估计自1970-01-01 00:00:00 UTC以来的纳秒数(不包括leap秒,这是正常且可以的),并将其放入带符号的64位整数中类型:
using namespace std::chrono;
long long t = time_point_cast<nanoseconds>(system_clock::now()).time_since_epoch().count();