86 inline constexpr int64_t nanos_per_second = 1'000'000'000;
91 template <
typename Period>
struct period_nanos {
92 using ratio = std::ratio_divide<Period, std::nano>;
93 static_assert(ratio::den == 1,
94 "the period must be a whole number of nanoseconds");
95 static constexpr int64_t value = ratio::num;
96 static_assert(value >= nanos_per_second
97 ? value % nanos_per_second == 0
98 : nanos_per_second % value == 0,
99 "the period must divide a second or be a multiple "
107 std::optional<int64_t> units_from_parts(int64_t seconds, int64_t nanos,
112 std::optional<uint64_t> unsigned_units_from_parts(int64_t seconds,
120 std::optional<std::pair<int64_t, int64_t>>
121 parts_from_units(int64_t count, int64_t unit_nanos,
bool floor);
124 std::optional<std::pair<int64_t, int64_t>>
125 parts_from_unsigned_units(uint64_t count, int64_t unit_nanos);
127 template <
typename Duration>
128 std::optional<Duration> duration_from_parts(int64_t seconds,
130 using Rep =
typename Duration::rep;
131 static_assert(std::is_integral_v<Rep>,
132 "a chrono conversion needs an integral Rep");
133 constexpr int64_t unit_nanos =
134 period_nanos<typename Duration::period>::value;
135 if constexpr (std::is_unsigned_v<Rep>) {
136 std::optional<uint64_t> count =
137 unsigned_units_from_parts(seconds, nanos, unit_nanos);
138 if (!count || *count > std::numeric_limits<Rep>::max()) {
141 return Duration(
static_cast<Rep
>(*count));
143 std::optional<int64_t> count =
144 units_from_parts(seconds, nanos, unit_nanos);
145 if (!count || *count < std::numeric_limits<Rep>::min() ||
146 *count > std::numeric_limits<Rep>::max()) {
149 return Duration(
static_cast<Rep
>(*count));
153 template <
typename Rep,
typename Period>
154 std::optional<std::pair<int64_t, int64_t>>
155 parts_from_duration(std::chrono::duration<Rep, Period> d,
bool floor) {
156 static_assert(std::is_integral_v<Rep>,
157 "a chrono conversion needs an integral Rep");
158 if constexpr (std::is_unsigned_v<Rep>) {
159 return parts_from_unsigned_units(
160 static_cast<uint64_t
>(d.count()),
161 period_nanos<Period>::value);
163 return parts_from_units(
static_cast<int64_t
>(d.count()),
164 period_nanos<Period>::value, floor);
168 template <
typename T>
struct is_system_time_point : std::false_type {};
169 template <
typename Duration>
170 struct is_system_time_point<
171 std::chrono::time_point<std::chrono::system_clock, Duration>>
175 template <
typename Period>
struct is_instant_period {
176 using ratio = std::ratio_divide<Period, std::nano>;
177 static constexpr bool value =
178 ratio::den == 1 && (ratio::num >= nanos_per_second
179 ? ratio::num % nanos_per_second == 0
180 : nanos_per_second % ratio::num == 0);
186 template <
typename T>
struct holds_instants : std::false_type {};
187 template <
typename Rep,
typename Period>
188 struct holds_instants<std::chrono::time_point<
189 std::chrono::system_clock, std::chrono::duration<Rep, Period>>>
190 : std::integral_constant<bool,
191 std::is_integral_v<Rep> &&
192 sizeof(Rep) <= sizeof(int64_t) &&
193 is_instant_period<Period>::value> {};
223 template <typename YearMonthDay>
224 std::optional<YearMonthDay> to_chrono() const {
225 using Year = decltype(std::declval<const YearMonthDay&>().year());
226 using Month = decltype(std::declval<const YearMonthDay&>().month());
227 using Day = decltype(std::declval<const YearMonthDay&>().day());
228 if (year < static_cast<int>((Year::min)()) ||
229 year > static_cast<int>((Year::max)())) {
232 return YearMonthDay{Year{year}, Month{month}, Day{day}};
244 template <typename YearMonthDay>
245 static std::optional<Date> from_chrono(const YearMonthDay& ymd) {
250 static_cast<int32_t>(static_cast<int>(ymd.year())),
251 static_cast<uint8_t>(static_cast<unsigned>(ymd.month())),
252 static_cast<uint8_t>(static_cast<unsigned>(ymd.day()))};
267 uint32_t nanosecond = 0;
290 uint32_t nanosecond = 0;
313 template <typename Duration>
315 std::chrono::time_point<std::chrono::system_clock, Duration>>
317 std::optional<Duration> since_epoch =
318 detail::duration_from_parts<Duration>(second, nanosecond);
322 return std::chrono::time_point<std::chrono::system_clock, Duration>(
334 template <typename Rep, typename Period>
335 static std::optional<Instant>
336 from_chrono(std::chrono::time_point<std::chrono::system_clock,
337 std::chrono::duration<Rep, Period>>
339 std::optional<std::pair<int64_t, int64_t>> parts =
340 detail::parts_from_duration(time_point.time_since_epoch(),
342 if (!parts || parts->first < MIN_INSTANT ||
346 return Instant{parts->first,
static_cast<uint32_t
>(parts->second)};
433 template <
typename Duration = std::chrono::nanoseconds>
445 template <
typename Rep,
typename Period>
446 static std::optional<SignedDuration>
448 std::optional<std::pair<int64_t, int64_t>> parts =
449 detail::parts_from_duration(duration,
false);
454 static_cast<int32_t
>(parts->second)};
515 constexpr auto key(
const Date& v) {
518 constexpr auto key(
const Time& v) {
519 return std::tie(v.hour, v.minute, v.second, v.nanosecond);
521 constexpr auto key(
const DateTime& v) {
522 return std::tuple_cat(key(v.date), key(v.time));
524 constexpr auto key(
const Instant& v) {
525 return std::tie(v.second, v.nanosecond);
527 constexpr std::tuple<bool, int32_t> key(
const UtcOffset& v) {
528 return {v.known, v.known ? v.seconds : 0};
530 constexpr auto key(
const TimeZone& v) {
531 return std::tie(v.name, v.primary_name);
533 constexpr auto key(
const ZonedDateTime& v) {
534 return std::tuple_cat(key(v.instant), key(v.zone), key(v.civil),
535 std::tie(v.kind, v.fold),
536 key(v.offset_before), key(v.offset_after),
539 constexpr auto key(
const SignedDuration& v) {
540 return std::tie(v.seconds, v.nanoseconds);
542 constexpr auto key(
const Span& v) {
543 return std::tie(v.years, v.months, v.weeks, v.days, v.hours,
544 v.minutes, v.seconds, v.milliseconds,
545 v.microseconds, v.nanoseconds);
548 template <
typename T,
typename =
void>
549 struct is_value : std::false_type {};
550 template <
typename T>
552 T, std::void_t<decltype(detail::key(std::declval<const T&>()))>>
556 template <
typename T,
typename R>
557 using if_value = std::enable_if_t<is_value<T>::value, R>;
559 template <
typename F>
560 void hash_field(std::size_t& seed,
const F& field) {
561 seed ^= std::hash<F>{}(
field) +
562 static_cast<std::size_t
>(0x9e3779b97f4a7c15ULL) +
563 (seed << 6) + (seed >> 2);
566 template <
typename T>
struct value_hash {
567 std::size_t operator()(
const T& value)
const noexcept {
568 std::size_t seed = 0;
570 [&seed](
const auto&... field) {
571 (hash_field(seed, field), ...);
604 template <
typename T>
605 constexpr detail::if_value<T, bool>
operator==(
const T& a,
const T& b) {
606 return detail::key(a) == detail::key(b);
613 template <
typename T>
614 constexpr detail::if_value<T, bool>
operator!=(
const T& a,
const T& b) {
622 template <
typename T>
623 constexpr detail::if_value<T, bool>
operator<(
const T& a,
const T& b) {
624 return detail::key(a) < detail::key(b);
631 template <
typename T>
632 constexpr detail::if_value<T, bool>
operator>(
const T& a,
const T& b) {
640 template <
typename T>
641 constexpr detail::if_value<T, bool>
operator<=(
const T& a,
const T& b) {
649 template <
typename T>
650 constexpr detail::if_value<T, bool>
operator>=(
const T& a,
const T& b) {
709 template <
typename T>
710 detail::if_value<T, std::ostream&>
operator<<(std::ostream& os,
842 return std::find(
tags.begin(),
tags.end(), tag) !=
tags.end();
920 std::optional<ZoneInfo>
info(std::string_view name);
956 : hegel::generators::temporal::detail::value_hash<
957 hegel::generators::temporal::Date> {};
960 : hegel::generators::temporal::detail::value_hash<
961 hegel::generators::temporal::Time> {};
963 struct hash<
hegel::generators::temporal::DateTime>
964 : hegel::generators::temporal::detail::value_hash<
965 hegel::generators::temporal::DateTime> {};
967 struct hash<hegel::generators::temporal::Instant>
968 : hegel::generators::temporal::detail::value_hash<
969 hegel::generators::temporal::Instant> {};
971 struct hash<hegel::generators::temporal::UtcOffset>
972 : hegel::generators::temporal::detail::value_hash<
973 hegel::generators::temporal::UtcOffset> {};
975 struct hash<hegel::generators::temporal::TimeZone>
976 : hegel::generators::temporal::detail::value_hash<
977 hegel::generators::temporal::TimeZone> {};
979 struct hash<hegel::generators::temporal::ZonedDateTime>
980 : hegel::generators::temporal::detail::value_hash<
981 hegel::generators::temporal::ZonedDateTime> {};
983 struct hash<hegel::generators::temporal::SignedDuration>
984 : hegel::generators::temporal::detail::value_hash<
985 hegel::generators::temporal::SignedDuration> {};
987 struct hash<hegel::generators::temporal::Span>
988 : hegel::generators::temporal::detail::value_hash<
989 hegel::generators::temporal::Span> {};
Date and time generators, and the plain values they draw.
Definition temporal.h:17
detail::if_value< T, std::ostream & > operator<<(std::ostream &os, const T &value)
Definition temporal_types.h:710
constexpr detail::if_value< T, bool > operator!=(const T &a, const T &b)
Definition temporal_types.h:614
constexpr detail::if_value< T, bool > operator<=(const T &a, const T &b)
Definition temporal_types.h:641
constexpr UtcOffset UNKNOWN_OFFSET
The unknown offset, RFC 3339's -00:00.
Definition temporal_types.h:365
constexpr std::array< Tag, 16 > ALL_TAGS
Every Tag, in the order of the engine's bit set.
Definition temporal_types.h:819
TransitionKind
How a wall-clock time maps to instants in its zone.
Definition temporal_types.h:381
@ Gap
None: the clocks skipped it.
Definition temporal_types.h:383
@ Fold
Two: the clocks repeated it.
Definition temporal_types.h:384
@ Unique
Exactly one instant.
Definition temporal_types.h:382
SpanSign
How the signs of a span's units are drawn.
Definition temporal_types.h:772
@ NonNegative
Definition temporal_types.h:775
@ Mixed
Each unit signed on its own.
Definition temporal_types.h:774
@ Uniform
One sign for every unit.
Definition temporal_types.h:773
constexpr detail::if_value< T, bool > operator>(const T &a, const T &b)
Definition temporal_types.h:632
constexpr int32_t MAX_OFFSET
The largest magnitude of a known UtcOffset in seconds, 25:59:59.
Definition temporal_types.h:78
constexpr int32_t MAX_YEAR
The highest year of a Date.
Definition temporal_types.h:72
constexpr int32_t MIN_YEAR
The lowest year of a Date.
Definition temporal_types.h:70
constexpr int64_t MIN_INSTANT
The first second of MIN_YEAR, the lowest second of an Instant.
Definition temporal_types.h:74
Weekday
A day of the week, for restricting a date draw.
Definition temporal_types.h:736
@ Tuesday
Tuesday.
Definition temporal_types.h:738
@ Monday
Monday.
Definition temporal_types.h:737
@ Thursday
Thursday.
Definition temporal_types.h:740
@ Wednesday
Wednesday.
Definition temporal_types.h:739
@ Saturday
Saturday.
Definition temporal_types.h:742
@ Sunday
Sunday.
Definition temporal_types.h:743
@ Friday
Friday.
Definition temporal_types.h:741
std::optional< ZoneInfo > info(std::string_view name)
What the default zone source knows about name, matched exactly as the source spells it,...
Tag
A feature of a time zone, derived from its offsets and name.
Definition temporal_types.h:785
@ Legacy
Definition temporal_types.h:811
@ InvertedSign
Definition temporal_types.h:813
@ Dst
Definition temporal_types.h:793
@ Fixed
No transition after 1970.
Definition temporal_types.h:786
@ SameOffset
Definition temporal_types.h:806
@ DstOther
Definition temporal_types.h:795
@ Fractional
Definition temporal_types.h:788
@ Seconds
Definition temporal_types.h:790
@ WholeDay
Definition temporal_types.h:804
@ Link
The name is a link to another zone.
Definition temporal_types.h:810
@ BaseChange
Definition temporal_types.h:798
@ Midnight
Definition temporal_types.h:800
@ Multi
Some calendar year has more than two offset changes.
Definition temporal_types.h:797
@ Undefined
Definition temporal_types.h:808
@ SpansMidnight
Definition temporal_types.h:802
@ WholeHour
Every offset after 1970 is a whole number of hours.
Definition temporal_types.h:787
constexpr int64_t MAX_INSTANT
The last second of MAX_YEAR, the highest second of an Instant.
Definition temporal_types.h:76
Month
A month of the year, for restricting a date draw.
Definition temporal_types.h:747
@ May
May.
Definition temporal_types.h:752
@ July
July.
Definition temporal_types.h:754
@ April
April.
Definition temporal_types.h:751
@ August
August.
Definition temporal_types.h:755
@ February
February.
Definition temporal_types.h:749
@ June
June.
Definition temporal_types.h:753
@ November
November.
Definition temporal_types.h:758
@ December
December.
Definition temporal_types.h:759
@ January
January.
Definition temporal_types.h:748
@ September
September.
Definition temporal_types.h:756
@ October
October.
Definition temporal_types.h:757
@ March
March.
Definition temporal_types.h:750
Ambiguous
Definition temporal_types.h:764
@ Both
Definition temporal_types.h:765
@ Later
The later instant, with no fold drawn.
Definition temporal_types.h:768
@ Earlier
The earlier instant, with no fold drawn.
Definition temporal_types.h:767
constexpr std::array< SpanUnit, 10 > ALL_SPAN_UNITS
Every SpanUnit, largest first.
Definition temporal_types.h:473
std::string tzdb_version()
The IANA release of the time zone database bundled in the engine, such as "2026e".
SpanUnit
A unit of a Span, largest first.
Definition temporal_types.h:459
@ Milliseconds
Milliseconds.
Definition temporal_types.h:467
@ Microseconds
Microseconds.
Definition temporal_types.h:468
@ Hours
Hours.
Definition temporal_types.h:464
@ Seconds
Seconds.
Definition temporal_types.h:466
@ Weeks
Weeks.
Definition temporal_types.h:462
@ Years
Calendar years.
Definition temporal_types.h:460
@ Days
Days.
Definition temporal_types.h:463
@ Months
Calendar months.
Definition temporal_types.h:461
@ Minutes
Minutes.
Definition temporal_types.h:465
@ Nanoseconds
Nanoseconds.
Definition temporal_types.h:469
constexpr std::string_view UNKNOWN_ZONE
Definition temporal_types.h:81
Granularity
The values a UTC offset draw restricts itself to.
Definition temporal_types.h:728
@ QuarterHours
Whole quarter hours.
Definition temporal_types.h:730
Precision
Definition temporal_types.h:718
ZonedDateTime resolve(std::string_view name, const DateTime &civil)
Classify the wall-clock time civil in the zone name and resolve it to an instant, meaning the earlier...
constexpr detail::if_value< T, bool > operator>=(const T &a, const T &b)
Definition temporal_types.h:650
std::string to_string(const Date &date)
constexpr detail::if_value< T, bool > operator<(const T &a, const T &b)
Definition temporal_types.h:623
constexpr detail::if_value< T, bool > operator==(const T &a, const T &b)
Definition temporal_types.h:605
ZoneOffset offset_at(std::string_view name, const Instant &instant)
The offset the zone name has in force at instant.
Field< MemberPtr, Gen > field(Gen gen)
Create a field specification for builds_agg().
Definition builds.h:119
Main namespace.
Definition config.h:29
A wall-clock date and time with no zone or offset, in the range of Date and Time. Prints as 2024-03-1...
Definition temporal_types.h:274
A proleptic Gregorian calendar date.
Definition temporal_types.h:205
uint8_t month
The month, 1 for January.
Definition temporal_types.h:207
uint8_t day
The day of the month, from 1.
Definition temporal_types.h:208
int32_t year
The year, negative before year 0.
Definition temporal_types.h:206
An instant.
Definition temporal_types.h:288
A signed duration: seconds plus nanoseconds of the same sign, the latter within ±999999999,...
Definition temporal_types.h:420
int64_t seconds
Whole seconds.
Definition temporal_types.h:421
std::optional< Duration > to_chrono() const
The same duration as a std::chrono::duration, or std::nullopt when it is not a whole number of the du...
Definition temporal_types.h:434
static std::optional< SignedDuration > from_chrono(std::chrono::duration< Rep, Period > duration)
The SignedDuration of a std::chrono::duration, or std::nullopt when its seconds do not fit in int64_t...
Definition temporal_types.h:447
int32_t nanoseconds
Nanoseconds, of the sign of seconds.
Definition temporal_types.h:422
A calendar span: a signed count of each unit from years to nanoseconds, never normalised (90 minutes ...
Definition temporal_types.h:492
int64_t microseconds
Microseconds.
Definition temporal_types.h:501
int64_t hours
Hours.
Definition temporal_types.h:497
int64_t nanoseconds
Nanoseconds.
Definition temporal_types.h:502
int64_t weeks
Weeks.
Definition temporal_types.h:495
int64_t months
Calendar months.
Definition temporal_types.h:494
int64_t milliseconds
Milliseconds.
Definition temporal_types.h:500
int64_t years
Calendar years.
Definition temporal_types.h:493
int64_t seconds
Seconds.
Definition temporal_types.h:499
int64_t days
Days.
Definition temporal_types.h:496
int64_t get(SpanUnit unit) const
int64_t minutes
Minutes.
Definition temporal_types.h:498
A time zone of a zone source, or UNKNOWN_ZONE.
Definition temporal_types.h:375
std::string name
The name as written.
Definition temporal_types.h:376
std::string primary_name
The zone whose data applies.
Definition temporal_types.h:377
A time of day from 00:00:00 to 23:59:59.999999999.
Definition temporal_types.h:263
A UTC offset: a known number of seconds east of UTC within ±25:59:59 (MAX_OFFSET),...
Definition temporal_types.h:359
int32_t seconds
Seconds east of UTC; 0 when not known.
Definition temporal_types.h:360
bool known
False for the unknown offset.
Definition temporal_types.h:361
What a zone source knows about a time zone name; see info().
Definition temporal_types.h:827
bool has(Tag tag) const
Definition temporal_types.h:841
uint16_t rank
The name's position on the shrink ladder; UTC is 0.
Definition temporal_types.h:834
std::string primary_name
Definition temporal_types.h:830
std::vector< Tag > tags
The zone's features, in ALL_TAGS order.
Definition temporal_types.h:836
bool is_link
Whether the name is a link to primary_name.
Definition temporal_types.h:832
The offset a time zone has in force at an instant; see offset_at().
Definition temporal_types.h:867
bool is_dst
Definition temporal_types.h:871
std::string abbreviation
Definition temporal_types.h:874
UtcOffset offset
Definition temporal_types.h:868
A wall-clock time in a time zone, resolved to an instant.
Definition temporal_types.h:401
UtcOffset offset
The offset in force at instant.
Definition temporal_types.h:409
DateTime civil
Definition temporal_types.h:403
UtcOffset offset_after
The offset after the transition.
Definition temporal_types.h:407
bool fold
Which repeat of a fold is meant.
Definition temporal_types.h:408
Instant instant
The resolved instant.
Definition temporal_types.h:410
UtcOffset offset_before
The offset before the transition.
Definition temporal_types.h:406
TimeZone zone
The zone the wall time is read in.
Definition temporal_types.h:402
TransitionKind kind
How the wall time maps to instants.
Definition temporal_types.h:405