c++ - 将字符串转换为文件时间

标签 c++

转换形式为 ""1997-01-08 03:04:01:463"的字符串的最快方法是什么 归档时间? 是否有执行此操作的函数?

最佳答案

我猜你说的是 Windows FILETIME,它包含自 1/1/1600 以来的 100 纳秒滴答数。

  1. 使用 sscanf() 或 std::istringstream 将字符串解析为其组件。 并填充 SYSTEMTIME 结构
  2. 使用 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/

相关文章:

c++ - MySQL C++ 连接器更新

c++ - 具有快速插入和索引的容器?

c++ - add_edge 导致 "no function to call error"

c++ - 使用 Boost.Asio 进行混合 tcp::iostream 和套接字操作

c++ - 我可以在 Visual Basic 中使用 C++ 函数吗?

c++ - 梅森扭曲随机生成器函数模板的 Xcode 编译器错误

c++ - 二叉搜索树

c++ - 为什么使用指向函数的指针调用虚函数时不需要 "this"指针?

c++ - WaitForSingleObject - 每个线程一次

c++ - stdin 的长度有限制吗?