-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[libc++][chrono] implements GPS clock. #125921
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
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
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,90 @@ | ||
// -*- 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef _LIBCPP___CHRONO_GPS_CLOCK_H | ||
#define _LIBCPP___CHRONO_GPS_CLOCK_H | ||
|
||
#include <version> | ||
// Enable the contents of the header only when libc++ was built with experimental features enabled. | ||
#if _LIBCPP_HAS_EXPERIMENTAL_TZDB | ||
|
||
# include <__assert> | ||
# include <__chrono/duration.h> | ||
# include <__chrono/time_point.h> | ||
# include <__chrono/utc_clock.h> | ||
# include <__config> | ||
# include <__type_traits/common_type.h> | ||
|
||
# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||
# pragma GCC system_header | ||
# endif | ||
|
||
_LIBCPP_PUSH_MACROS | ||
# include <__undef_macros> | ||
|
||
_LIBCPP_BEGIN_NAMESPACE_STD | ||
|
||
# if _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION | ||
|
||
namespace chrono { | ||
|
||
class gps_clock; | ||
|
||
template <class _Duration> | ||
using gps_time = time_point<gps_clock, _Duration>; | ||
using gps_seconds = gps_time<seconds>; | ||
|
||
class gps_clock { | ||
public: | ||
using rep = utc_clock::rep; | ||
using period = utc_clock::period; | ||
using duration = chrono::duration<rep, period>; | ||
using time_point = chrono::time_point<gps_clock>; | ||
static constexpr bool is_steady = false; // The utc_clock is not steady. | ||
|
||
// The static difference between UTC and GPS time as specified in the Standard. | ||
static constexpr chrono::seconds __offset{315964809}; | ||
|
||
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI static time_point now() { return from_utc(utc_clock::now()); } | ||
|
||
template <class _Duration> | ||
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI static utc_time<common_type_t<_Duration, seconds>> | ||
to_utc(const gps_time<_Duration>& __time) noexcept { | ||
using _Rp = common_type_t<_Duration, seconds>; | ||
_Duration __time_since_epoch = __time.time_since_epoch(); | ||
_LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(__time_since_epoch >= utc_time<_Rp>::min().time_since_epoch() + __offset, | ||
"the GPS to UTC conversion would underflow"); | ||
|
||
return utc_time<_Rp>{__time_since_epoch + __offset}; | ||
} | ||
|
||
template <class _Duration> | ||
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI static gps_time<common_type_t<_Duration, seconds>> | ||
from_utc(const utc_time<_Duration>& __time) noexcept { | ||
using _Rp = common_type_t<_Duration, seconds>; | ||
_Duration __time_since_epoch = __time.time_since_epoch(); | ||
_LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(__time_since_epoch <= utc_time<_Rp>::max().time_since_epoch() - __offset, | ||
"the UTC to GPS conversion would overflow"); | ||
|
||
return gps_time<_Rp>{__time_since_epoch - __offset}; | ||
} | ||
}; | ||
|
||
} // namespace chrono | ||
|
||
# endif // _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM && | ||
// _LIBCPP_HAS_LOCALIZATION | ||
|
||
_LIBCPP_END_NAMESPACE_STD | ||
|
||
_LIBCPP_POP_MACROS | ||
|
||
#endif // _LIBCPP_HAS_EXPERIMENTAL_TZDB | ||
|
||
#endif // _LIBCPP___CHRONO_GPS_CLOCK_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
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
73 changes: 73 additions & 0 deletions
73
...est/libcxx/time/time.clock/time.clock.gps/time.clock.gps.members/assert.from_utc.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,73 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// REQUIRES: std-at-least-c++20 | ||
// UNSUPPORTED: no-filesystem, no-localization, no-tzdb | ||
|
||
// XFAIL: libcpp-has-no-experimental-tzdb | ||
// XFAIL: availability-tzdb-missing | ||
|
||
// REQUIRES: libcpp-hardening-mode={{extensive|debug}} | ||
// REQUIRES: has-unix-headers | ||
// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing | ||
|
||
// <chrono> | ||
// | ||
// class gps_clock; | ||
|
||
// static gps_time<common_type_t<_Duration, seconds>> | ||
// from_utc(const utc_time<_Duration>& t) noexcept; | ||
|
||
#include <chrono> | ||
|
||
#include "check_assertion.h" | ||
|
||
// The function is specified as | ||
// gps_time<common_type_t<Duration, seconds>>{t.time_since_epoch()} + 378691210s | ||
// When t == t.max() there will be a signed integral overflow (other values too). | ||
int main(int, char**) { | ||
using namespace std::literals::chrono_literals; | ||
constexpr std::chrono::seconds offset{315964809}; | ||
|
||
(void)std::chrono::gps_clock::from_utc(std::chrono::utc_time<std::chrono::nanoseconds>::max() - offset); | ||
TEST_LIBCPP_ASSERT_FAILURE( | ||
std::chrono::gps_clock::from_utc(std::chrono::utc_time<std::chrono::nanoseconds>::max() - offset + 1ns), | ||
"the UTC to GPS conversion would overflow"); | ||
|
||
(void)std::chrono::gps_clock::from_utc(std::chrono::utc_time<std::chrono::microseconds>::max() - offset); | ||
TEST_LIBCPP_ASSERT_FAILURE( | ||
std::chrono::gps_clock::from_utc(std::chrono::utc_time<std::chrono::microseconds>::max() - offset + 1us), | ||
"the UTC to GPS conversion would overflow"); | ||
|
||
(void)std::chrono::gps_clock::from_utc(std::chrono::utc_time<std::chrono::milliseconds>::max() - offset); | ||
TEST_LIBCPP_ASSERT_FAILURE( | ||
std::chrono::gps_clock::from_utc(std::chrono::utc_time<std::chrono::milliseconds>::max() - offset + 1ms), | ||
"the UTC to GPS conversion would overflow"); | ||
|
||
(void)std::chrono::gps_clock::from_utc(std::chrono::utc_seconds::max() - offset); | ||
TEST_LIBCPP_ASSERT_FAILURE(std::chrono::gps_clock::from_utc(std::chrono::utc_seconds::max() - offset + 1s), | ||
"the UTC to GPS conversion would overflow"); | ||
|
||
// The conversion uses `common_type_t<Duration, seconds>` so types "larger" | ||
// than seconds are converted to seconds. Types "larger" than seconds are | ||
// stored in "smaller" intergral and the overflow can never occur. | ||
|
||
// Validate the types can never overflow on all current (and future) supported platforms. | ||
static_assert(std::chrono::utc_time<std::chrono::days>::max() <= std::chrono::utc_seconds::max() - offset); | ||
static_assert(std::chrono::utc_time<std::chrono::weeks>::max() <= std::chrono::utc_seconds::max() - offset); | ||
static_assert(std::chrono::utc_time<std::chrono::months>::max() <= std::chrono::utc_seconds::max() - offset); | ||
static_assert(std::chrono::utc_time<std::chrono::years>::max() <= std::chrono::utc_seconds::max() - offset); | ||
|
||
// Validate the run-time conversion works. | ||
(void)std::chrono::gps_clock::from_utc(std::chrono::utc_time<std::chrono::days>::max()); | ||
(void)std::chrono::gps_clock::from_utc(std::chrono::utc_time<std::chrono::weeks>::max()); | ||
(void)std::chrono::gps_clock::from_utc(std::chrono::utc_time<std::chrono::months>::max()); | ||
(void)std::chrono::gps_clock::from_utc(std::chrono::utc_time<std::chrono::years>::max()); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
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.