Как записать/прочитать std::chrono::zoned_секунды в/из потока с помощью Chronic::parse? ⇐ C++
-
Anonymous
Как записать/прочитать std::chrono::zoned_секунды в/из потока с помощью Chronic::parse?
I am trying to write a chrono::zoned_seconds object as text to a file and then retrieve it and construct another chrono::zoned_seconds object later. How can this be done in a fairly efficient way?
I don't think the code snippet below shows the correct result:
#include #include #include #include #include #include int main() { { std::ofstream file("data.txt"); if (!file) { std::println( "Unable to open file.\n" ); return 1; } auto now = std::chrono::system_clock::now(); const std::chrono::zoned_seconds zs { "America/New_York", std::chrono::time_point_cast( now ) }; std::format_to(std::ostreambuf_iterator(file), "{:%F %T %Z}", zs); std::println( "{:%F %T %Z}", zs ); // correct time } { std::ifstream file("data.txt"); if (!file) { std::println( "Unable to open file.\n" ); return 1; } std::string str; std::getline(file, str); std::istringstream iss { str }; std::chrono::sys_seconds tp; std::string zone_name; iss >> std::chrono::parse("%F %T %Z", tp, zone_name); std::chrono::zoned_seconds zs { zone_name, tp }; std::println( "{:%F %T %Z}", zs ); // incorrect time!! } } As can be seen, I used std::chrono::parse but the outputs don't match:
2024-03-01 13:35:20 EST 2024-03-01 08:35:20 EST
Источник: https://stackoverflow.com/questions/780 ... chronopars
I am trying to write a chrono::zoned_seconds object as text to a file and then retrieve it and construct another chrono::zoned_seconds object later. How can this be done in a fairly efficient way?
I don't think the code snippet below shows the correct result:
#include #include #include #include #include #include int main() { { std::ofstream file("data.txt"); if (!file) { std::println( "Unable to open file.\n" ); return 1; } auto now = std::chrono::system_clock::now(); const std::chrono::zoned_seconds zs { "America/New_York", std::chrono::time_point_cast( now ) }; std::format_to(std::ostreambuf_iterator(file), "{:%F %T %Z}", zs); std::println( "{:%F %T %Z}", zs ); // correct time } { std::ifstream file("data.txt"); if (!file) { std::println( "Unable to open file.\n" ); return 1; } std::string str; std::getline(file, str); std::istringstream iss { str }; std::chrono::sys_seconds tp; std::string zone_name; iss >> std::chrono::parse("%F %T %Z", tp, zone_name); std::chrono::zoned_seconds zs { zone_name, tp }; std::println( "{:%F %T %Z}", zs ); // incorrect time!! } } As can be seen, I used std::chrono::parse but the outputs don't match:
2024-03-01 13:35:20 EST 2024-03-01 08:35:20 EST
Источник: https://stackoverflow.com/questions/780 ... chronopars
Мобильная версия