master
 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-exception
 7//
 8//===----------------------------------------------------------------------===//
 9
10// For information see https://libcxx.llvm.org/DesignDocs/TimeZone.html
11
12#ifndef _LIBCPP___CHRONO_TZDB_H
13#define _LIBCPP___CHRONO_TZDB_H
14
15#include <version>
16// Enable the contents of the header only when libc++ was built with experimental features enabled.
17#if _LIBCPP_HAS_EXPERIMENTAL_TZDB
18
19#  include <__algorithm/ranges_lower_bound.h>
20#  include <__chrono/leap_second.h>
21#  include <__chrono/time_zone.h>
22#  include <__chrono/time_zone_link.h>
23#  include <__config>
24#  include <__memory/addressof.h>
25#  include <__vector/vector.h>
26#  include <stdexcept>
27#  include <string>
28#  include <string_view>
29
30#  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
31#    pragma GCC system_header
32#  endif
33
34_LIBCPP_PUSH_MACROS
35#  include <__undef_macros>
36
37_LIBCPP_BEGIN_NAMESPACE_STD
38
39#  if _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION
40
41namespace chrono {
42
43struct tzdb {
44  string version;
45  vector<time_zone> zones;
46  vector<time_zone_link> links;
47
48  vector<leap_second> leap_seconds;
49
50  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI const time_zone* __locate_zone(string_view __name) const {
51    if (const time_zone* __result = __find_in_zone(__name))
52      return __result;
53
54    if (auto __it = ranges::lower_bound(links, __name, {}, &time_zone_link::name);
55        __it != links.end() && __it->name() == __name)
56      if (const time_zone* __result = __find_in_zone(__it->target()))
57        return __result;
58
59    return nullptr;
60  }
61
62  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI const time_zone* locate_zone(string_view __name) const {
63    if (const time_zone* __result = __locate_zone(__name))
64      return __result;
65
66    std::__throw_runtime_error("tzdb: requested time zone not found");
67  }
68
69  [[nodiscard]] _LIBCPP_AVAILABILITY_TZDB _LIBCPP_HIDE_FROM_ABI const time_zone* current_zone() const {
70    return __current_zone();
71  }
72
73private:
74  _LIBCPP_HIDE_FROM_ABI const time_zone* __find_in_zone(string_view __name) const noexcept {
75    if (auto __it = ranges::lower_bound(zones, __name, {}, &time_zone::name);
76        __it != zones.end() && __it->name() == __name)
77      return std::addressof(*__it);
78
79    return nullptr;
80  }
81
82  [[nodiscard]] _LIBCPP_AVAILABILITY_TZDB _LIBCPP_EXPORTED_FROM_ABI const time_zone* __current_zone() const;
83};
84
85} // namespace chrono
86
87#  endif // _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM &&
88         // _LIBCPP_HAS_LOCALIZATION
89
90_LIBCPP_END_NAMESPACE_STD
91
92_LIBCPP_POP_MACROS
93
94#endif // _LIBCPP_HAS_EXPERIMENTAL_TZDB
95
96#endif // _LIBCPP___CHRONO_TZDB_H