Created
November 24, 2023 10:42
-
-
Save Bktero/382552a54fffc06441256228f2a0acca to your computer and use it in GitHub Desktop.
[C++20] Examples of the new time zone features in <chrono>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <chrono> | |
#include <iostream> | |
// Requires clang 17 or gcc 13 | |
int main() | |
{ | |
// UTC (system_clock is guaranteed to be UTC since C++20) | |
const auto now = std::chrono::system_clock::now(); | |
std::cout << "UTC time = " << now << '\n'; | |
// Local | |
const auto local_now = std::chrono::zoned_time{std::chrono::current_zone(), now}; | |
std::cout << "Local time = " << local_now << "\n"; | |
// Somewhere | |
const auto somewhere_tz = std::chrono::locate_zone("Asia/Ho_Chi_Minh"); | |
const auto somewhere_now = std::chrono::zoned_time{somewhere_tz, now}; | |
std::cout << somewhere_tz->name() << " time = " << somewhere_now << '\n'; | |
// Possible time zone names | |
const auto &tzdb = std::chrono::get_tzdb(); | |
std::cout << "Database version = " << tzdb.version << '\n'; | |
for (auto &tz : tzdb.zones) | |
{ | |
std::cout << " - " << tz.name() << "\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment