Hegel 0.18.3
Property-based testing for C++
Loading...
Searching...
No Matches
zone_source.h
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <functional>
6#include <memory>
7#include <optional>
8#include <string>
9#include <string_view>
10#include <variant>
11#include <vector>
12
13#include "hegel/generators/temporal_types.h"
14
16
21 struct ZoneType {
23 int32_t offset_seconds = 0;
27 bool is_dst = false;
29 std::string abbreviation;
30 };
31
37 int64_t instant = 0;
40 };
41
48 struct RuleDate {
60
63 uint16_t day = 0;
67 uint8_t week = 1;
72 int32_t time = 7200;
73
79 static RuleDate julian(uint16_t day, int32_t time = 7200) {
82 }
83
88 static RuleDate zero_based(uint16_t day, int32_t time = 7200) {
91 }
92
102 Weekday weekday, int32_t time = 7200) {
104 }
105 };
106
116
121 struct ZoneRule {
125 std::optional<DaylightRule> daylight;
126 };
127
132 struct HoldLast {};
133
142 struct InferRule {
144 explicit InferRule(int32_t year) : last_year(year) {}
145
147 int32_t last_year;
148 };
149
167 struct ZoneData {
171 std::vector<ZoneTransition> transitions;
173 std::variant<HoldLast, ZoneRule, InferRule> after;
174 };
175
178 enum class AfterProbe {
186 };
187
202 struct ZoneOracle {
205 std::function<ZoneType(int64_t instant)> offset_at;
208 std::function<std::optional<int64_t>(int64_t after)> next_transition;
210 int32_t first_year = 1900;
213 int32_t last_year = 2100;
216 };
217
218 class ZoneSource;
219
239 std::function<void(ZoneSource& source, const std::string& name)>;
240
291 public:
294
300
310 ZoneSource& add_tzif(std::string_view name,
311 const std::vector<uint8_t>& bytes);
312
321 ZoneSource& add_tzif(std::string_view name, const uint8_t* data,
322 size_t size);
323
330 ZoneSource& add_zone(std::string_view name, const ZoneData& data);
331
341 ZoneSource& add_oracle(std::string_view name, const ZoneOracle& oracle);
342
349 ZoneSource& add_link(std::string_view name, std::string_view target);
350
358 ZoneSource& declare(const std::vector<std::string>& names);
359
369
374 ZoneSource& set_version(std::string_view version);
375
379 std::string version() const;
380
389 std::optional<ZoneInfo> info(std::string_view name) const;
390
398 ZonedDateTime resolve(std::string_view name,
399 const DateTime& civil) const;
400
408 ZoneOffset offset_at(std::string_view name,
409 const Instant& instant) const;
410
412 struct Impl;
413 explicit ZoneSource(std::shared_ptr<Impl> impl);
414 const std::shared_ptr<Impl>& impl() const { return impl_; }
416
417 private:
418 std::shared_ptr<Impl> impl_;
419 };
420
433
453
454} // namespace hegel::generators::temporal
A source of time zone data: the database the zone and zoned datetime generators classify,...
Definition zone_source.h:290
ZoneSource & add_link(std::string_view name, std::string_view target)
ZonedDateTime resolve(std::string_view name, const DateTime &civil) const
std::optional< ZoneInfo > info(std::string_view name) const
ZoneSource & add_tzif(std::string_view name, const uint8_t *data, size_t size)
ZoneOffset offset_at(std::string_view name, const Instant &instant) const
ZoneSource & add_tzif(std::string_view name, const std::vector< uint8_t > &bytes)
ZoneSource()
An empty source, to be filled with the add_* functions.
ZoneSource & add_oracle(std::string_view name, const ZoneOracle &oracle)
ZoneSource & set_version(std::string_view version)
ZoneSource & declare(const std::vector< std::string > &names)
ZoneSource & add_zone(std::string_view name, const ZoneData &data)
ZoneSource & set_loader(ZoneLoader loader)
Date and time generators, and the plain values they draw.
Definition temporal.h:17
std::function< void(ZoneSource &source, const std::string &name)> ZoneLoader
Supplies a zone source's data for a name the first time a draw or a query needs it.
Definition zone_source.h:238
AfterProbe
Definition zone_source.h:178
@ Infer
Definition zone_source.h:185
@ Hold
Definition zone_source.h:181
Weekday
A day of the week, for restricting a date draw.
Definition temporal_types.h:736
@ Sunday
Sunday.
Definition temporal_types.h:743
Month
A month of the year, for restricting a date draw.
Definition temporal_types.h:747
@ January
January.
Definition temporal_types.h:748
ZoneSource default_zone_source()
The process-wide default zone source: the one zones(), zoned_datetimes() and the info(),...
ZoneSource set_default_zone_source(const ZoneSource &source)
Replace the process-wide default zone source.
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
Daylight-saving time of a yearly rule: its type, and the dates it starts and ends on each year.
Definition zone_source.h:111
RuleDate start
When the daylight-saving type comes in.
Definition zone_source.h:113
ZoneType type
The daylight-saving type.
Definition zone_source.h:112
RuleDate end
When the standard type comes back.
Definition zone_source.h:114
The last type in force, held for ever after the explicit transitions.
Definition zone_source.h:132
InferRule(int32_t year)
Definition zone_source.h:144
int32_t last_year
The last year the transitions cover, in [MIN_YEAR, MAX_YEAR].
Definition zone_source.h:147
An instant.
Definition temporal_types.h:288
The day and time of day a yearly rule changes the clocks on, as a POSIX TZ string names them (RFC 963...
Definition zone_source.h:48
Month month
The month, for Kind::MonthWeekDay.
Definition zone_source.h:65
uint16_t day
The day of the year, for Kind::Julian and Kind::ZeroBased.
Definition zone_source.h:63
Weekday weekday
The weekday, for Kind::MonthWeekDay.
Definition zone_source.h:69
static RuleDate zero_based(uint16_t day, int32_t time=7200)
Definition zone_source.h:88
Kind kind
How the day is named.
Definition zone_source.h:61
Kind
How the day is named.
Definition zone_source.h:50
@ MonthWeekDay
Mm.w.d: weekday of week week of month.
Definition zone_source.h:58
@ Julian
Definition zone_source.h:53
@ ZeroBased
Definition zone_source.h:56
int32_t time
Definition zone_source.h:72
uint8_t week
The week in [1, 5], 5 meaning the last, for Kind::MonthWeekDay.
Definition zone_source.h:67
static RuleDate julian(uint16_t day, int32_t time=7200)
Definition zone_source.h:79
static RuleDate month_week_day(Month month, uint8_t week, Weekday weekday, int32_t time=7200)
Definition zone_source.h:101
A zone's data given as fields: the TZif model of RFC 9636.
Definition zone_source.h:167
std::variant< HoldLast, ZoneRule, InferRule > after
What is in force after the last transition.
Definition zone_source.h:173
std::vector< ZoneTransition > transitions
The transitions, strictly ascending by instant.
Definition zone_source.h:171
ZoneType initial
The type in force before the first transition.
Definition zone_source.h:169
The offset a time zone has in force at an instant; see offset_at().
Definition temporal_types.h:867
A library that answers offset lookups but does not expose its zone data, probed into explicit transit...
Definition zone_source.h:202
int32_t last_year
Definition zone_source.h:213
AfterProbe after
What is in force after last_year.
Definition zone_source.h:215
std::function< std::optional< int64_t >(int64_t after)> next_transition
Definition zone_source.h:208
int32_t first_year
The first year probed.
Definition zone_source.h:210
std::function< ZoneType(int64_t instant)> offset_at
Definition zone_source.h:205
A yearly rule in force after a zone's explicit transitions: the footer of a TZif file.
Definition zone_source.h:121
ZoneType standard
The standard type.
Definition zone_source.h:123
std::optional< DaylightRule > daylight
Daylight-saving time, or none for a fixed offset.
Definition zone_source.h:125
An explicit transition: from instant on, type is in force.
Definition zone_source.h:35
int64_t instant
Seconds since the Unix epoch.
Definition zone_source.h:37
ZoneType type
The type in force from instant on.
Definition zone_source.h:39
A local time type of a zone: an offset, its daylight-saving flag and its abbreviation.
Definition zone_source.h:21
int32_t offset_seconds
Seconds east of UT, less than two days in magnitude.
Definition zone_source.h:23
std::string abbreviation
The abbreviation, such as EST; no NUL bytes.
Definition zone_source.h:29
bool is_dst
Definition zone_source.h:27
A wall-clock time in a time zone, resolved to an instant.
Definition temporal_types.h:401