// -*- C++ -*- //===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #ifndef TEST_STD_TIME_TIME_ZONE_TIME_ZONE_ZONEDTIME_TEST_OFFSET_TIME_ZONE_H #define TEST_STD_TIME_TIME_ZONE_TIME_ZONE_ZONEDTIME_TEST_OFFSET_TIME_ZONE_H #include #include #include #include #include #include #include enum class offset_time_zone_flags { none = 0, has_default_zone = 1, has_locate_zone = 2, both = has_default_zone | has_locate_zone }; // The enforcement of the flags is done in the zoned_traits template class offset_time_zone { public: offset_time_zone() : offset_{std::chrono::seconds{0}} {} explicit offset_time_zone(std::string_view name) { int count; const char* begin = name.data(); const char* end = begin + name.size(); std::from_chars_result result = std::from_chars(begin, end, count); assert(result == std::from_chars_result(end, std::errc{})); offset_ = std::chrono::seconds(count); } std::chrono::seconds offset() const { return offset_; } offset_time_zone* operator->() { return this; } const offset_time_zone* operator->() const { return this; } template std::chrono::sys_time> to_sys(const std::chrono::local_time& local) const { return std::chrono::sys_time>{ local.time_since_epoch() + offset_}; } template std::chrono::local_time> to_local(const std::chrono::sys_time& sys) const { return std::chrono::local_time>{ sys.time_since_epoch() - offset_}; } template std::chrono::sys_info get_info(const std::chrono::sys_time&) const { return {std::chrono::sys_seconds::min(), std::chrono::sys_seconds::max(), offset_, std::chrono::minutes{0}, std::format("{:+03d}s", offset_.count())}; } private: std::chrono::seconds offset_; }; template <> struct std::chrono::zoned_traits> { using type = offset_time_zone; static type default_zone() { return {}; } }; template <> struct std::chrono::zoned_traits> { using type = offset_time_zone; static type locate_zone(std::string_view name) { return type{name}; } }; template <> struct std::chrono::zoned_traits> { using type = offset_time_zone; static type default_zone() { return {}; } static type locate_zone(std::string_view name) { return type{name}; } }; #endif // TEST_STD_TIME_TIME_ZONE_TIME_ZONE_ZONEDTIME_TEST_OFFSET_TIME_ZONE_H