转换形式为 ""1997-01-08 03:04:01:463"的字符串的最快方法是什么 归档时间? 是否有执行此操作的函数?
最佳答案
我猜你说的是 Windows FILETIME,它包含自 1/1/1600 以来的 100 纳秒滴答数。
- 使用 sscanf() 或 std::istringstream 将字符串解析为其组件。 并填充 SYSTEMTIME 结构
- 使用 SystemTimeToFileTime() 转换为 FILETIME
例如
FILETIME DecodeTime(const std::string &sTime)
{
std::istringstream istr(sTime);
SYSTEMTIME st = { 0 };
FILETIME ft = { 0 };
istr >> st.wYear;
istr.ignore(1, '-');
istr >> st.wMonth;
istr.ignore(1, '-');
istr >> st.wDay;
istr.ignore(1, ' ');
istr >> st.wHour;
istr.ignore(1, ':');
istr >> st.wMinute;
istr.ignore(1, ':');
istr >> st.wSecond;
istr.ignore(1, '.');
istr >> st.wMilliseconds;
// Do validation that istr has no errors and all fields
// are in sensible ranges
// ...
::SystemTimeToFileTime(&st, &ft);
return ft;
}
int main(int argc, char* argv[])
{
FILETIME ft = DecodeTime("1997-01-08 03:04:01.463");
return 0;
}
关于c++ - 将字符串转换为文件时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4983145/