-
Notifications
You must be signed in to change notification settings - Fork 13.7k
[libc++][TZDB] Implements zoned_traits. #91059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// -*- 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// For information see https://libcxx.llvm.org/DesignDocs/TimeZone.html | ||
|
||
#ifndef _LIBCPP___CHRONO_ZONED_TIME_H | ||
#define _LIBCPP___CHRONO_ZONED_TIME_H | ||
|
||
#include <version> | ||
// Enable the contents of the header only when libc++ was built with experimental features enabled. | ||
#if !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_TZDB) | ||
|
||
# include <__chrono/time_zone.h> | ||
# include <__chrono/tzdb_list.h> | ||
# include <__config> | ||
# include <__fwd/string_view.h> | ||
|
||
# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||
# pragma GCC system_header | ||
# endif | ||
|
||
_LIBCPP_BEGIN_NAMESPACE_STD | ||
|
||
# if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM) && \ | ||
!defined(_LIBCPP_HAS_NO_LOCALIZATION) | ||
|
||
namespace chrono { | ||
|
||
template <class> | ||
struct zoned_traits {}; | ||
|
||
template <> | ||
struct zoned_traits<const time_zone*> { | ||
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI static const time_zone* default_zone() { return chrono::locate_zone("UTC"); } | ||
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI static const time_zone* locate_zone(string_view __name) { | ||
return chrono::locate_zone(__name); | ||
} | ||
}; | ||
|
||
} // namespace chrono | ||
|
||
# endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM) | ||
// && !defined(_LIBCPP_HAS_NO_LOCALIZATION) | ||
|
||
_LIBCPP_END_NAMESPACE_STD | ||
|
||
#endif // !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_TZDB) | ||
|
||
#endif // _LIBCPP___CHRONO_ZONED_TIME_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
libcxx/test/std/time/time.zone/time.zone.zonedtraits/const_time_zone_default_zone.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// UNSUPPORTED: c++03, c++11, c++14, c++17 | ||
// UNSUPPORTED: no-filesystem, no-localization, no-tzdb | ||
|
||
// XFAIL: libcpp-has-no-experimental-tzdb | ||
// XFAIL: availability-tzdb-missing | ||
|
||
// <chrono> | ||
|
||
// template<> struct zoned_traits<const time_zone*>; | ||
|
||
// static const time_zone* default_zone(); | ||
|
||
#include <chrono> | ||
#include <cassert> | ||
|
||
int main(int, char**) { | ||
std::same_as<const std::chrono::time_zone*> decltype(auto) tz = | ||
std::chrono::zoned_traits<const std::chrono::time_zone*>::default_zone(); | ||
assert(tz); | ||
|
||
// The time zone "UTC" can be a link, this means tz->name() can be something | ||
// differently. For example, "Etc/UTC". Instead validate whether same time | ||
// zone is returned by comparing the addresses. | ||
const std::chrono::time_zone* expected = std::chrono::locate_zone("UTC"); | ||
assert(tz == expected); | ||
|
||
return 0; | ||
} |
45 changes: 45 additions & 0 deletions
45
libcxx/test/std/time/time.zone/time.zone.zonedtraits/const_time_zone_locate_zone.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// UNSUPPORTED: c++03, c++11, c++14, c++17 | ||
// UNSUPPORTED: no-filesystem, no-localization, no-tzdb | ||
|
||
// XFAIL: libcpp-has-no-experimental-tzdb | ||
// XFAIL: availability-tzdb-missing | ||
|
||
// <chrono> | ||
|
||
// template<> struct zoned_traits<const time_zone*>; | ||
|
||
// static const time_zone* locate_zone(string_view name); | ||
|
||
#include <chrono> | ||
#include <cassert> | ||
#include <concepts> | ||
|
||
#include "assert_macros.h" | ||
|
||
static void test(std::string_view name) { | ||
std::same_as<const std::chrono::time_zone*> decltype(auto) tz = | ||
std::chrono::zoned_traits<const std::chrono::time_zone*>::locate_zone(name); | ||
|
||
const std::chrono::time_zone* expected = std::chrono::locate_zone(name); | ||
assert(tz == expected); | ||
} | ||
|
||
int main(int, char**) { | ||
test("UTC"); | ||
test("Europe/Berlin"); | ||
test("Asia/Hong_Kong"); | ||
|
||
TEST_THROWS_TYPE(std::runtime_error, | ||
TEST_IGNORE_NODISCARD std::chrono::zoned_traits<const std::chrono::time_zone*>::locate_zone( | ||
"there_is_no_time_zone_with_this_name")); | ||
|
||
return 0; | ||
} |
33 changes: 33 additions & 0 deletions
33
libcxx/test/std/time/time.zone/time.zone.zonedtraits/types.compile.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// UNSUPPORTED: c++03, c++11, c++14, c++17 | ||
// UNSUPPORTED: no-filesystem, no-localization, no-tzdb | ||
|
||
// XFAIL: libcpp-has-no-experimental-tzdb | ||
// XFAIL: availability-tzdb-missing | ||
|
||
// <chrono> | ||
|
||
// template<class T> struct zoned_traits {}; | ||
// | ||
// A specialization for const time_zone* is provided by the implementation: | ||
// template<> struct zoned_traits<const time_zone*> { ... } | ||
|
||
#include <chrono> | ||
#include <type_traits> | ||
|
||
// This test test whether non-specialized versions exhibit the expected | ||
// behavior. (Note these specializations are not really useful.) | ||
static_assert(std::is_trivial_v<std::chrono::zoned_traits<int>>); | ||
static_assert(std::is_trivial_v<std::chrono::zoned_traits<float>>); | ||
static_assert(std::is_trivial_v<std::chrono::zoned_traits<void*>>); | ||
|
||
struct foo {}; | ||
static_assert(std::is_empty_v<std::chrono::zoned_traits<foo>>); | ||
static_assert(std::is_trivial_v<std::chrono::zoned_traits<foo>>); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.