C++ std::字符串到std::时钟时间点
我有一个以下时间格式的字符串:C++ std::字符串到std::时钟时间点,c++,c++11,casting,chrono,C++,C++11,Casting,Chrono,我有一个以下时间格式的字符串: %Y-%m-%d%H:%m:%S.%f“ 其中%f是毫秒,例如:14:31:23.946571 我希望这是一个计时点。是否有转换来执行此操作?没有从std::string转换到std::chrono::time\u point的转换。您必须建立std::chrono::time\u point对象 使用除微秒以外的所有时间构造std::tm对象()。 年份应以1900年为基础,而不是0。月份应基于0,而不是1 使用std::mktime()创建std::time
%Y-%m-%d%H:%m:%S.%f“
其中%f是毫秒,例如:14:31:23.946571
我希望这是一个
计时点
。是否有转换来执行此操作?没有从std::string
转换到std::chrono::time\u point
的转换。您必须建立std::chrono::time\u point
对象
- 使用除微秒以外的所有时间构造
对象(std::tm
)。 年份应以1900年为基础,而不是0。月份应基于0,而不是1李> - 使用
创建std::mktime()
对象李>std::time\t
- 使用
创建一个from\u time\u t()
std::chrono::time\u点李>
- 将剩余的小数部分(视为
)作为int
持续时间添加到std::chrono::microsecond()
时间点
- 将剩余的小数部分(视为
函数std::ctime()
和std::put_time()
不知道精度低于一秒。如果您想要打印该级别的精度,则需要编写一个函数
#include <chrono>
#include <ctime>
#include <iomanip>
#include <iostream>
struct Tm : std::tm {
int tm_usecs; // [0, 999999] micros after the sec
Tm(const int year, const int month, const int mday, const int hour,
const int min, const int sec, const int usecs, const int isDST = -1)
: tm_usecs{usecs} {
tm_year = year - 1900; // [0, 60] since 1900
tm_mon = month - 1; // [0, 11] since Jan
tm_mday = mday; // [1, 31]
tm_hour = hour; // [0, 23] since midnight
tm_min = min; // [0, 59] after the hour
tm_sec = sec; // [0, 60] after the min
// allows for 1 positive leap second
tm_isdst = isDST; // [-1...] -1 for unknown, 0 for not DST,
// any positive value if DST.
}
template <typename Clock_t = std::chrono::high_resolution_clock,
typename MicroSecond_t = std::chrono::microseconds>
auto to_time_point() -> typename Clock_t::time_point {
auto time_c = mktime(this);
return Clock_t::from_time_t(time_c) + MicroSecond_t{tm_usecs};
}
};
int main() {
using namespace std::chrono;
auto tp_nomicro = Tm(2014, 8, 19, 14, 31, 23, 0).to_time_point();
auto tp_micro = Tm(2014, 8, 19, 14, 31, 23, 946571).to_time_point();
std::cout << duration_cast<microseconds>(tp_micro - tp_nomicro).count()
<< " microseconds apart.\n";
auto time_c = high_resolution_clock::to_time_t(tp_micro);
std::cout << std::ctime(&time_c) << '\n';
}
#包括
#包括
#包括
#包括
struct Tm:std::Tm{
秒后的int tm_usecs;//[099999]微秒
Tm(常数整年、常数整月、常数整日、常数整时、,
const int min,const int sec,const int usecs,const int isDST=-1)
:tm_usecs{usecs}{
tm_year=年份-1900;自1900年起/[0,60]
tm_mon=自1月起的第1个月;//[0,11]
tm_mday=mday;//[1,31]
tm_hour=小时;自午夜起//[0,23]
tm_min=min;//[0,59]小时后
tm_sec=sec;//[0,60]在最小值之后
//允许1个正闰秒
tm_isdst=isdst;//[-1…]-1表示未知,0表示非DST,
//如果是DST,则为任何正值。
}
模板
自动到时间点()->typename时钟:时间点{
自动时间_c=mktime(此);
返回时钟:from_time_t(time_c)+微秒{tm_usecs};
}
};
int main(){
使用名称空间std::chrono;
自动tp_nomicro=Tm(2014,8,19,14,31,23,0).to_time_point();
自动tp_micro=Tm(2014,8,19,14,31,23946571)。到时间点();
std::cout我个人会在这里使用日期算法:特别是days\u from\u civil
,然后加上H:M:S。你必须决定它是否在UTC时区内,如果不在,就加上时区偏移量。H:M:S不是问题所在。毫秒才是问题所在。否则我会把它转换成时间_t@HowardHinnant怎样今天,您是否会使用date.h将ISO 8061日期字符串(包括毫秒)转换为可操纵的数据类型?@BrunoBieri:using I willin>>date::parse(“%F%T”,tp)
其中tp
是一个std::chrono::system\u clock::time\u point
,或者是一个基于的时间点,精度为毫秒或更高。也可以使用更详细的字符串:%Y-%m-%d%H:%m:%S
这是等效的。通常ISO 8061格式要求在日期和时间之间使用T
分隔符,而不是空格,如果需要,只需将T
放在格式字符串中即可。谢谢,这正是我要找的。但是我注意到LLVM在高分辨率上没有from\u time\T_clock@bge0:The标准规定了到时间和从时间到系统时钟,高分辨率时钟被允许(但不是必需)作为系统时钟的类型别名。