Path: blob/main/contrib/llvm-project/libcxx/include/__chrono/tai_clock.h
213766 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___CHRONO_TAI_CLOCK_H10#define _LIBCPP___CHRONO_TAI_CLOCK_H1112#include <version>13// Enable the contents of the header only when libc++ was built with experimental features enabled.14#if _LIBCPP_HAS_EXPERIMENTAL_TZDB1516# include <__assert>17# include <__chrono/duration.h>18# include <__chrono/time_point.h>19# include <__chrono/utc_clock.h>20# include <__config>21# include <__type_traits/common_type.h>2223# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)24# pragma GCC system_header25# endif2627_LIBCPP_PUSH_MACROS28# include <__undef_macros>2930_LIBCPP_BEGIN_NAMESPACE_STD3132# if _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION3334namespace chrono {3536class tai_clock;3738template <class _Duration>39using tai_time = time_point<tai_clock, _Duration>;40using tai_seconds = tai_time<seconds>;4142// [time.clock.tai.overview]/143// The clock tai_clock measures seconds since 1958-01-01 00:00:00 and is44// offset 10s ahead of UTC at this date. That is, 1958-01-01 00:00:00 TAI is45// equivalent to 1957-12-31 23:59:50 UTC. Leap seconds are not inserted into46// TAI. Therefore every time a leap second is inserted into UTC, UTC shifts47// another second with respect to TAI. For example by 2000-01-01 there had48// been 22 positive and 0 negative leap seconds inserted so 2000-01-0149// 00:00:00 UTC is equivalent to 2000-01-01 00:00:32 TAI (22s plus the50// initial 10s offset).51//52// Note this does not specify what the UTC offset before 1958-01-01 00:00:0053// TAI is, nor does it follow the "real" TAI clock between 1958-01-01 and the54// start of the UTC epoch. So while the member functions are fully specified in55// the standard, they do not technically follow the "real-world" TAI clock with56// 100% accuracy.57//58// https://koka-lang.github.io/koka/doc/std_time_utc.html contains more59// information and references.60class tai_clock {61public:62using rep = utc_clock::rep;63using period = utc_clock::period;64using duration = chrono::duration<rep, period>;65using time_point = chrono::time_point<tai_clock>;66static constexpr bool is_steady = false; // The utc_clock is not steady.6768// The static difference between UTC and TAI time.69static constexpr chrono::seconds __offset{378691210};7071[[nodiscard]] _LIBCPP_HIDE_FROM_ABI static time_point now() { return from_utc(utc_clock::now()); }7273template <class _Duration>74[[nodiscard]] _LIBCPP_HIDE_FROM_ABI static utc_time<common_type_t<_Duration, seconds>>75to_utc(const tai_time<_Duration>& __time) noexcept {76using _Rp = common_type_t<_Duration, seconds>;77_Duration __time_since_epoch = __time.time_since_epoch();78_LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(__time_since_epoch >= utc_time<_Rp>::min().time_since_epoch() + __offset,79"the TAI to UTC conversion would underflow");8081return utc_time<_Rp>{__time_since_epoch - __offset};82}8384template <class _Duration>85[[nodiscard]] _LIBCPP_HIDE_FROM_ABI static tai_time<common_type_t<_Duration, seconds>>86from_utc(const utc_time<_Duration>& __time) noexcept {87using _Rp = common_type_t<_Duration, seconds>;88_Duration __time_since_epoch = __time.time_since_epoch();89_LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(__time_since_epoch <= utc_time<_Rp>::max().time_since_epoch() - __offset,90"the UTC to TAI conversion would overflow");9192return tai_time<_Rp>{__time_since_epoch + __offset};93}94};9596} // namespace chrono9798# endif // _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM &&99// _LIBCPP_HAS_LOCALIZATION100101_LIBCPP_END_NAMESPACE_STD102103_LIBCPP_POP_MACROS104105#endif // _LIBCPP_HAS_EXPERIMENTAL_TZDB106107#endif // _LIBCPP___CHRONO_TAI_CLOCK_H108109110