简体   繁体   English

丢失了 std::filesystem 的时钟转换,在 C++17 中缺少 clock_cast

[英]Lost about std::filesystem's clock conversions, in C++17 lacking clock_cast

A while ago, I managed to do this, to go from filesystem to system clocks, thanks to SO:不久前,我设法做到了,从文件系统系统时钟到 go,这要归功于 SO:

int64_t toUSsecSinceEpochUTC(std::filesystem::file_time_type ftime) {
    // see https://stackoverflow.com/a/35282833/103724
    using namespace std::chrono;
    auto sys_now = system_clock::now();
    auto fil_now = decltype(ftime)::clock::now(); // i.e. C++20's file_clock
    auto sys_ftime = time_point_cast<system_clock::duration>(ftime - fil_now + sys_now);
    auto sys_ftime_usec = time_point_cast<microseconds>(sys_ftime);
    return sys_ftime_usec.time_since_epoch().count();
}

But now I want to do the reverse, and I'm struggling... here's my feeble attempt:但现在我想做相反的事情,我正在挣扎......这是我微弱的尝试:

std::filesystem::file_time_type fromUSsecSinceEpochUTC(int64_t usec_utc) {
    std::filesystem::file_time_type ftime;

    using namespace std::chrono;
    auto sys_now = system_clock::now();
    auto fil_now = decltype(ftime)::clock::now(); // i.e. C++20's file_clock

    std::chrono::microseconds usec(usec_utc);
    ftime = ????
    return ftime;
}

MSVC2019 complains on auto res = sys_now - usec + fil_now; MSVC2019 抱怨auto res = sys_now - usec + fil_now; with

error C2676: binary '+': 'std::chrono::time_point<std::chrono::system_clock,std::chrono::duration<std::chrono::system_clock::rep,std::chrono::system_clock::period>>' does not define this operator or a conversion to a type acceptable to the predefined operator

ie the code from https://stackoverflow.com/a/35282833/103724 between steady and system clocks does not appear to work for the filesystem clock.稳定时钟和系统时钟之间的https://stackoverflow.com/a/35282833/103724中的代码似乎不适用于文件系统时钟。 Although it could be just me not following.虽然可能只是我没有跟随。

Any <chrono> expert who might be able to help?任何<chrono>专家可以提供帮助吗?

To fill out fromUSsecSinceEpochUTC you would:要填写fromUSsecSinceEpochUTC ,您将:

std::filesystem::file_time_type
fromUSsecSinceEpochUTC(int64_t usec_utc) {
    std::filesystem::file_time_type ftime;

    using namespace std::chrono;
    auto sys_now = system_clock::now();
    auto fil_now = decltype(ftime)::clock::now(); // i.e. C++20's file_clock

    microseconds usec(usec_utc);
    time_point<system_clock, microseconds> tp_sys{usec};
    return tp_sys - sys_now + fil_now;
}

That being said, the relationship between the epoch of system_clock and file_clock is going to be a constant.话虽如此, system_clockfile_clock的时代之间的关系将是一个常数。 I believe the Windows file_clock epoch is 1601-01-01 00:00:00 UTC.我相信 Windows file_clock纪元是 1601-01-01 00:00:00 UTC。 The difference between that and the system_clock epoch (1970-01-01 00:00:00 UTC) is 13,4774 days or 3'234'576h .这与system_clock时期(1970-01-01 00:00:00 UTC)之间的差异是 13,4774 天或3'234'576h

This knowledge allows you to write conversions (for Windows) without calls to now() .这些知识使您无需调用now()

std::filesystem::file_time_type
to_file_time(std::chrono::system_clock::time_point sys_tp)
{
    using namespace std::literals;
    return std::filesystem::file_time_type{sys_tp.time_since_epoch() + 3'234'576h};
}

std::chrono::system_clock::time_point
to_sys_time(std::filesystem::file_time_type f_tp)
{
    using namespace std::literals;
    return std::chrono::system_clock::time_point{f_tp.time_since_epoch() - 3'234'576h};
}

Above I've taken advantage of the a-prior knowledge that file_clock::duration is the same type as system_clock::duration on Windows.上面我利用了先验知识,即file_clock::duration ::duration 与 Windows 上的system_clock::duration类型相同。 Add casts as necessary or desired.根据需要或需要添加演员表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 C++17 中检索 std::filesystem::file_time_type 的时钟类型 - How to retrieve the clock type of std::filesystem::file_time_type in C++17 如何使用C ++ 17 std :: filesystem获取用户的主目录? - How can one get user's home directory using C++17 std::filesystem? C ++ 17 std :: filesystem :: path中的本机路径分隔符错误? - Native path separator bug in C++17 std::filesystem::path? "c++14 std::experimental::filesystem::v1 和 c++17 std::filesystem 之间的区别?" - Differences between c++14 std::experimental::filesystem::v1 and c++17 std::filesystem? 为什么C ++ 17的std :: any不允许any_cast返回一个可移动的值? - Why does C++17's std::any not allow returning a movable value by any_cast? c++17 语言扩展和 std:c++17 标志 - c++17 language extension and std:c++17 flag c ++ 17 `filesystem` 不是命名空间名称 - c++17 `filesystem` is not a namespace-name C++17 的编译错误<filesystem>在 MinGW 上 - Compilation errors for C++17 <filesystem> on MinGW 错误使用<filesystem> C++17 - Errors using <filesystem> C++17 是C ++ 17 std :: filesystem :: recursive_directory_iterator :: pop等于boost :: filesystem :: recursive_directory_iterator :: no_push - is C++17 std::filesystem::recursive_directory_iterator::pop equal to boost::filesystem::recursive_directory_iterator::no_push
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM