| 1 | // { dg-do run { target c++20 } } |
| 2 | // { dg-require-effective-target tzdb } |
| 3 | |
| 4 | #include <chrono> |
| 5 | #include <set> |
| 6 | #include <stdexcept> |
| 7 | #include <testsuite_hooks.h> |
| 8 | |
| 9 | struct local_tz |
| 10 | { |
| 11 | local_tz() : name(std::chrono::current_zone()->name()) { } |
| 12 | |
| 13 | explicit local_tz(std::string_view name) : name(name) { } |
| 14 | |
| 15 | template<typename Dur> |
| 16 | std::chrono::sys_time<Dur> to_sys(const std::chrono::local_time<Dur>& d) |
| 17 | { return std::chrono::locate_zone(name)->to_sys(d); } |
| 18 | |
| 19 | template<typename Dur> |
| 20 | std::chrono::sys_time<Dur> to_local(const std::chrono::sys_time<Dur>& d) |
| 21 | { return std::chrono::locate_zone(name)->to_sys(d); } |
| 22 | |
| 23 | template<typename Dur> |
| 24 | std::chrono::sys_info get_info(const std::chrono::sys_time<Dur>& d) |
| 25 | { return std::chrono::locate_zone(name)->get_info(d); } |
| 26 | |
| 27 | struct indirect_cmp |
| 28 | { |
| 29 | bool operator()(const local_tz* lhs, const local_tz* rhs) const |
| 30 | { return lhs->name < rhs->name; } |
| 31 | }; |
| 32 | |
| 33 | bool eq(const std::chrono::time_zone* tz) const noexcept |
| 34 | { return name == tz->name(); } |
| 35 | |
| 36 | private: |
| 37 | std::string_view name; |
| 38 | }; |
| 39 | |
| 40 | template<> struct std::chrono::zoned_traits<const local_tz*> |
| 41 | { |
| 42 | static const local_tz* default_zone() |
| 43 | { |
| 44 | return locate_zone(std::chrono::current_zone()->name()); |
| 45 | } |
| 46 | |
| 47 | static const local_tz* locate_zone(std::string_view name) |
| 48 | { |
| 49 | static std::set<const local_tz*, local_tz::indirect_cmp> zones; |
| 50 | local_tz tz(name); |
| 51 | if (auto z = zones.find(&tz); z != zones.end()) |
| 52 | return *z; |
| 53 | if (std::chrono::locate_zone(name)) |
| 54 | return *zones.insert(new local_tz(tz)).first; |
| 55 | throw std::runtime_error("zone not found"); |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | void |
| 60 | test_custom_tzptr() |
| 61 | { |
| 62 | using namespace std::chrono; |
| 63 | |
| 64 | zoned_time<seconds, const local_tz*> z; |
| 65 | VERIFY( z.get_time_zone()->eq(std::chrono::current_zone()) ); |
| 66 | |
| 67 | zoned_time<seconds, const local_tz*> z2(std::string_view("Europe/London")); |
| 68 | VERIFY( z2.get_time_zone()->eq(std::chrono::locate_zone("Europe/London")) ); |
| 69 | } |
| 70 | |
| 71 | int main() |
| 72 | { |
| 73 | test_custom_tzptr(); |
| 74 | } |