Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:15

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___CHRONO_UTC_CLOCK_H
0011 #define _LIBCPP___CHRONO_UTC_CLOCK_H
0012 
0013 #include <version>
0014 // Enable the contents of the header only when libc++ was built with experimental features enabled.
0015 #if _LIBCPP_HAS_EXPERIMENTAL_TZDB
0016 
0017 #  include <__chrono/duration.h>
0018 #  include <__chrono/leap_second.h>
0019 #  include <__chrono/system_clock.h>
0020 #  include <__chrono/time_point.h>
0021 #  include <__chrono/tzdb.h>
0022 #  include <__chrono/tzdb_list.h>
0023 #  include <__config>
0024 #  include <__type_traits/common_type.h>
0025 
0026 #  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0027 #    pragma GCC system_header
0028 #  endif
0029 
0030 _LIBCPP_BEGIN_NAMESPACE_STD
0031 
0032 #  if _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION
0033 
0034 namespace chrono {
0035 
0036 class utc_clock;
0037 
0038 template <class _Duration>
0039 using utc_time    = time_point<utc_clock, _Duration>;
0040 using utc_seconds = utc_time<seconds>;
0041 
0042 class utc_clock {
0043 public:
0044   using rep                       = system_clock::rep;
0045   using period                    = system_clock::period;
0046   using duration                  = chrono::duration<rep, period>;
0047   using time_point                = chrono::time_point<utc_clock>;
0048   static constexpr bool is_steady = false; // The system_clock is not steady.
0049 
0050   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static time_point now() { return from_sys(system_clock::now()); }
0051 
0052   template <class _Duration>
0053   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static sys_time<common_type_t<_Duration, seconds>>
0054   to_sys(const utc_time<_Duration>& __time);
0055 
0056   template <class _Duration>
0057   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static utc_time<common_type_t<_Duration, seconds>>
0058   from_sys(const sys_time<_Duration>& __time) {
0059     using _Rp = utc_time<common_type_t<_Duration, seconds>>;
0060     // TODO TZDB investigate optimizations.
0061     //
0062     // The leap second database stores all transitions, this mean to calculate
0063     // the current number of leap seconds the code needs to iterate over all
0064     // leap seconds to accumulate the sum. Then the sum can be used to determine
0065     // the sys_time. Accessing the database involves acquiring a mutex.
0066     //
0067     // The historic entries in the database are immutable. Hard-coding these
0068     // values in a table would allow:
0069     // - To store the sum, allowing a binary search on the data.
0070     // - Avoid acquiring a mutex.
0071     // The disadvantage are:
0072     // - A slightly larger code size.
0073     //
0074     // There are two optimization directions
0075     // - hard-code the database and do a linear search for future entries. This
0076     //   search can start at the back, and should probably contain very few
0077     //   entries. (Adding leap seconds is quite rare and new release of libc++
0078     //   can add the new entries; they are announced half a year before they are
0079     //   added.)
0080     // - During parsing the leap seconds store an additional database in the
0081     //   dylib with the list of the sum of the leap seconds. In that case there
0082     //   can be a private function __get_utc_to_sys_table that returns the
0083     //   table.
0084     //
0085     // Note for to_sys there are no optimizations to be done; it uses
0086     // get_leap_second_info. The function get_leap_second_info could benefit
0087     // from optimizations as described above; again both options apply.
0088 
0089     // Both UTC and the system clock use the same epoch. The Standard
0090     // specifies from 1970-01-01 even when UTC starts at
0091     // 1972-01-01 00:00:10 TAI. So when the sys_time is before epoch we can be
0092     // sure there both clocks return the same value.
0093 
0094     const tzdb& __tzdb = chrono::get_tzdb();
0095     _Rp __result{__time.time_since_epoch()};
0096     for (const auto& __leap_second : __tzdb.leap_seconds) {
0097       if (__leap_second > __time)
0098         return __result;
0099 
0100       __result += __leap_second.value();
0101     }
0102     return __result;
0103   }
0104 };
0105 
0106 struct leap_second_info {
0107   bool is_leap_second;
0108   seconds elapsed;
0109 };
0110 
0111 template <class _Duration>
0112 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI leap_second_info get_leap_second_info(const utc_time<_Duration>& __time) {
0113   const tzdb& __tzdb = chrono::get_tzdb();
0114   if (__tzdb.leap_seconds.empty()) [[unlikely]]
0115     return {false, chrono::seconds{0}};
0116 
0117   sys_seconds __sys{chrono::floor<seconds>(__time).time_since_epoch()};
0118   seconds __elapsed{0};
0119   for (const auto& __leap_second : __tzdb.leap_seconds) {
0120     if (__sys == __leap_second.date() + __elapsed)
0121       // A time point may only be a leap second during a positive leap second
0122       // insertion, since time points that occur during a (theoretical)
0123       // negative leap second don't exist.
0124       return {__leap_second.value() > 0s, __elapsed + __leap_second.value()};
0125 
0126     if (__sys < __leap_second.date() + __elapsed)
0127       return {false, __elapsed};
0128 
0129     __elapsed += __leap_second.value();
0130   }
0131 
0132   return {false, __elapsed};
0133 }
0134 
0135 template <class _Duration>
0136 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI sys_time<common_type_t<_Duration, seconds>>
0137 utc_clock::to_sys(const utc_time<_Duration>& __time) {
0138   using _Dp               = common_type_t<_Duration, seconds>;
0139   leap_second_info __info = chrono::get_leap_second_info(__time);
0140 
0141   // [time.clock.utc.members]/2
0142   //   Returns: A sys_time t, such that from_sys(t) == u if such a mapping
0143   //   exists. Otherwise u represents a time_point during a positive leap
0144   //   second insertion, the conversion counts that leap second as not
0145   //   inserted, and the last representable value of sys_time prior to the
0146   //   insertion of the leap second is returned.
0147   sys_time<common_type_t<_Duration, seconds>> __result{__time.time_since_epoch() - __info.elapsed};
0148   if (__info.is_leap_second)
0149     return chrono::floor<seconds>(__result) + chrono::seconds{1} - _Dp{1};
0150 
0151   return __result;
0152 }
0153 
0154 } // namespace chrono
0155 
0156 #  endif // _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM &&
0157          // _LIBCPP_HAS_LOCALIZATION
0158 
0159 _LIBCPP_END_NAMESPACE_STD
0160 
0161 #endif // _LIBCPP_HAS_EXPERIMENTAL_TZDB
0162 
0163 #endif // _LIBCPP___CHRONO_UTC_CLOCK_H