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 !defined(_LIBCPP_HAS_NO_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 <string>
25# include <vector>
26
27# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28# pragma GCC system_header
29# endif
30
31_LIBCPP_PUSH_MACROS
32# include <__undef_macros>
33
34_LIBCPP_BEGIN_NAMESPACE_STD
35
36# if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM) && \
37 !defined(_LIBCPP_HAS_NO_LOCALIZATION)
38
39namespace chrono {
40
41struct tzdb {
42 string version;
43 vector<time_zone> zones;
44 vector<time_zone_link> links;
45
46 vector<leap_second> leap_seconds;
47
48 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI const time_zone* __locate_zone(string_view __name) const {
49 if (const time_zone* __result = __find_in_zone(__name))
50 return __result;
51
52 if (auto __it = ranges::lower_bound(links, __name, {}, &time_zone_link::name);
53 __it != links.end() && __it->name() == __name)
54 if (const time_zone* __result = __find_in_zone(name: __it->target()))
55 return __result;
56
57 return nullptr;
58 }
59
60 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI const time_zone* locate_zone(string_view __name) const {
61 if (const time_zone* __result = __locate_zone(__name))
62 return __result;
63
64 std::__throw_runtime_error("tzdb: requested time zone not found");
65 }
66
67 [[nodiscard]] _LIBCPP_AVAILABILITY_TZDB _LIBCPP_HIDE_FROM_ABI const time_zone* current_zone() const {
68 return __current_zone();
69 }
70
71private:
72 _LIBCPP_HIDE_FROM_ABI const time_zone* __find_in_zone(string_view __name) const noexcept {
73 if (auto __it = ranges::lower_bound(zones, __name, {}, &time_zone::name);
74 __it != zones.end() && __it->name() == __name)
75 return std::addressof(x: *__it);
76
77 return nullptr;
78 }
79
80 [[nodiscard]] _LIBCPP_AVAILABILITY_TZDB _LIBCPP_EXPORTED_FROM_ABI const time_zone* __current_zone() const;
81};
82
83} // namespace chrono
84
85# endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
86 // && !defined(_LIBCPP_HAS_NO_LOCALIZATION)
87
88_LIBCPP_END_NAMESPACE_STD
89
90_LIBCPP_POP_MACROS
91
92#endif // !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_TZDB)
93
94#endif // _LIBCPP___CHRONO_TZDB_H
95