【发布时间】:2020-05-01 02:05:24
【问题描述】:
2018-10-01 00:06:16.700000000
我有一个带有上述时间戳的时间序列数据文件。 我需要将其转换为从纪元开始的纳秒,然后我需要将毫秒、微米或纳秒添加到时间戳(移位)。最后,对于选择记录,将其恢复为上述格式。
我在创建时间点时遇到问题 - 以及如何表示纳秒......它适用于微秒。
我能否就下面的代码片段请求一些帮助......以及一旦我从纪元获得纳秒 - 我如何回到上面的时间戳。
std::string ts("23 01 2020 20:59:59.123456789");
XXXX (ts);
void XXXXX (string timestamp)
{
stringstream temp_ss(timestamp);
tm temp_time_object = {};
temp_ss >> get_time (&temp_time_object, "%Y/%m/%d %H:%M:%S");
chrono::system_clock::time_point temp_time_point = system_clock::from_time_t(mktime(&temp_time_object));
// chrono::high_resolution_clock::time_point temp_time_point1 = temp_time_point;
auto nsecs = stol (timestamp.substr (timestamp.find_first_of('.')+1,9));
// +++ This is where I GET stuck forming the time_point....
// I've tried this so many different ways ....
// Is it that the system_clock cannot accept nanos?
temp_time_point += nanoseconds (nsecs);
auto micro_from_epoch = duration_cast<nanoseconds> (temp_time_point.time_since_epoch()).count();
cout << micro_from_epoch << endl;
}
【问题讨论】:
-
请发布您收到的错误消息
-
上述代码的问题是纳秒内没有 += 运算符。如果我尝试以 1:100000000 的比率显式添加使用持续时间 - 它仍然抱怨我无法将其添加到时间点。
-
您的基于字符串的时间戳是本地时间还是 UTC?如果是本地时间,是您计算机当前的本地时区设置,还是其他时区?
-
假设所有时间戳都是 GMT, UTC + 0