brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.3 KiB · bbd01d1 Raw
99 lines · c
1// -*- C++ -*-2//===----------------------------------------------------------------------===//3//4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.5// See https://llvm.org/LICENSE.txt for license information.6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef TEST_STD_TIME_TIME_ZONE_TIME_ZONE_ZONEDTIME_TEST_OFFSET_TIME_ZONE_H11#define TEST_STD_TIME_TIME_ZONE_TIME_ZONE_ZONEDTIME_TEST_OFFSET_TIME_ZONE_H12 13#include <cassert>14#include <charconv>15#include <chrono>16#include <format>17#include <string_view>18#include <system_error>19#include <type_traits>20 21enum class offset_time_zone_flags {22  none             = 0,23  has_default_zone = 1,24  has_locate_zone  = 2,25  both             = has_default_zone | has_locate_zone26};27 28// The enforcement of the flags is done in the zoned_traits29template <offset_time_zone_flags flags = offset_time_zone_flags::both>30class offset_time_zone {31public:32  offset_time_zone() : offset_{std::chrono::seconds{0}} {}33  explicit offset_time_zone(std::string_view name) {34    int count;35    const char* begin             = name.data();36    const char* end               = begin + name.size();37    std::from_chars_result result = std::from_chars(begin, end, count);38    assert(result == std::from_chars_result(end, std::errc{}));39 40    offset_ = std::chrono::seconds(count);41  }42 43  std::chrono::seconds offset() const { return offset_; }44 45  offset_time_zone* operator->() { return this; }46 47  const offset_time_zone* operator->() const { return this; }48 49  template <class Duration>50  std::chrono::sys_time<std::common_type_t<Duration, std::chrono::seconds>>51  to_sys(const std::chrono::local_time<Duration>& local) const {52    return std::chrono::sys_time<std::common_type_t<Duration, std::chrono::seconds>>{53        local.time_since_epoch() + offset_};54  }55 56  template <class Duration>57  std::chrono::local_time<std::common_type_t<Duration, std::chrono::seconds>>58  to_local(const std::chrono::sys_time<Duration>& sys) const {59    return std::chrono::local_time<std::common_type_t<Duration, std::chrono::seconds>>{60        sys.time_since_epoch() - offset_};61  }62 63  template <class Duration>64  std::chrono::sys_info get_info(const std::chrono::sys_time<Duration>&) const {65    return {std::chrono::sys_seconds::min(),66            std::chrono::sys_seconds::max(),67            offset_,68            std::chrono::minutes{0},69            std::format("{:+03d}s", offset_.count())};70  }71 72private:73  std::chrono::seconds offset_;74};75 76template <>77struct std::chrono::zoned_traits<offset_time_zone<offset_time_zone_flags::has_default_zone>> {78  using type = offset_time_zone<offset_time_zone_flags::has_default_zone>;79 80  static type default_zone() { return {}; }81};82 83template <>84struct std::chrono::zoned_traits<offset_time_zone<offset_time_zone_flags::has_locate_zone>> {85  using type = offset_time_zone<offset_time_zone_flags::has_locate_zone>;86 87  static type locate_zone(std::string_view name) { return type{name}; }88};89 90template <>91struct std::chrono::zoned_traits<offset_time_zone<offset_time_zone_flags::both>> {92  using type = offset_time_zone<offset_time_zone_flags::both>;93 94  static type default_zone() { return {}; }95  static type locate_zone(std::string_view name) { return type{name}; }96};97 98#endif // TEST_STD_TIME_TIME_ZONE_TIME_ZONE_ZONEDTIME_TEST_OFFSET_TIME_ZONE_H99