Hegel 0.18.3
Property-based testing for C++
Loading...
Searching...
No Matches
chrono_tzdb.h
1#pragma once
2
32
33#include <chrono>
34#include <cstdint>
35#include <optional>
36#include <stdexcept>
37#include <string>
38#include <vector>
39
40#if defined(__GLIBCXX__) && (_GLIBCXX_RELEASE < 13 || __cplusplus < 202002L)
41#error \
42 "hegel/extras/chrono_tzdb.h needs a standard library that implements std::chrono::get_tzdb(): libstdc++ has it from GCC 13 on, in C++20 mode"
43#endif
44
45#include "hegel/generators/zone_source.h"
46
52
63 oracle(const std::chrono::time_zone& zone, int32_t first_year = 1800,
64 int32_t last_year = 2100) {
65 const std::chrono::time_zone* tz = &zone;
67 result.offset_at = [tz](int64_t instant) {
68 std::chrono::sys_info info = tz->get_info(
69 std::chrono::sys_seconds(std::chrono::seconds(instant)));
71 static_cast<int32_t>(info.offset.count()),
72 info.save != std::chrono::minutes(0), info.abbrev};
73 };
74 result.next_transition = [tz](int64_t after) -> std::optional<int64_t> {
75 std::chrono::sys_info info = tz->get_info(
76 std::chrono::sys_seconds(std::chrono::seconds(after)));
77 constexpr std::chrono::sys_seconds last_day = std::chrono::sys_days{
78 std::chrono::year::max() / std::chrono::December / 31};
79 if (info.end >= last_day) {
80 return std::nullopt;
81 }
82 return info.end.time_since_epoch().count();
83 };
84 result.first_year = first_year;
85 result.last_year = last_year;
87 return result;
88 }
89
106 const std::chrono::tzdb& db = std::chrono::get_tzdb();
107 std::vector<std::string> names;
108 names.reserve(db.zones.size());
109 for (const std::chrono::time_zone& zone : db.zones) {
110 names.emplace_back(zone.name());
111 }
113 source.declare(names).set_version(db.version);
114 for (const std::chrono::time_zone_link& link : db.links) {
115 source.add_link(link.name(), link.target());
116 }
117 source.set_loader(
118 [](generators::temporal::ZoneSource& s, const std::string& name) {
119 const std::chrono::time_zone* zone = nullptr;
120 try {
121 zone = std::chrono::locate_zone(name);
122 } catch (const std::runtime_error&) {
123 return;
124 }
125 s.add_oracle(name, oracle(*zone));
126 });
127 return source;
128 }
129
130} // namespace hegel::extras::chrono_tzdb
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)
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 & set_loader(ZoneLoader loader)
Zone sources from std::chrono::tzdb; see hegel/extras/chrono_tzdb.h.
Definition chrono_tzdb.h:51
generators::temporal::ZoneSource zone_source()
A zone source over the standard library's time zone database, std::chrono::get_tzdb().
Definition chrono_tzdb.h:105
generators::temporal::ZoneOracle oracle(const std::chrono::time_zone &zone, int32_t first_year=1800, int32_t last_year=2100)
An offset oracle over a standard library time zone.
Definition chrono_tzdb.h:63
@ Infer
Definition zone_source.h:185
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 local time type of a zone: an offset, its daylight-saving flag and its abbreviation.
Definition zone_source.h:21