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# if _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION
38
39_LIBCPP_BEGIN_NAMESPACE_STD
40_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
41
42namespace chrono {
43
44struct tzdb {
45 string version;
46 vector<time_zone> zones;
47 vector<time_zone_link> links;
48
49 vector<leap_second> leap_seconds;
50
51 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI const time_zone* __locate_zone(string_view __name) const {
52 if (const time_zone* __result = __find_in_zone(__name))
53 return __result;
54
55 if (auto __it = ranges::lower_bound(links, __name, {}, &time_zone_link::name);
56 __it != links.end() && __it->name() == __name)
57 if (const time_zone* __result = __find_in_zone(name: __it->target()))
58 return __result;
59
60 return nullptr;
61 }
62
63 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI const time_zone* locate_zone(string_view __name) const {
64 if (const time_zone* __result = __locate_zone(__name))
65 return __result;
66
67 std::__throw_runtime_error("tzdb: requested time zone not found");
68 }
69
70 [[nodiscard]] _LIBCPP_AVAILABILITY_TZDB _LIBCPP_HIDE_FROM_ABI const time_zone* current_zone() const {
71 return __current_zone();
72 }
73
74private:
75 _LIBCPP_HIDE_FROM_ABI const time_zone* __find_in_zone(string_view __name) const noexcept {
76 if (auto __it = ranges::lower_bound(zones, __name, {}, &time_zone::name);
77 __it != zones.end() && __it->name() == __name)
78 return std::addressof(x: *__it);
79
80 return nullptr;
81 }
82
83 [[nodiscard]] _LIBCPP_AVAILABILITY_TZDB _LIBCPP_EXPORTED_FROM_ABI const time_zone* __current_zone() const;
84};
85
86} // namespace chrono
87
88_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
89_LIBCPP_END_NAMESPACE_STD
90
91# endif // _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM &&
92 // _LIBCPP_HAS_LOCALIZATION
93
94_LIBCPP_POP_MACROS
95
96#endif // _LIBCPP_HAS_EXPERIMENTAL_TZDB
97
98#endif // _LIBCPP___CHRONO_TZDB_H
99