52#include <boost/date_time/local_time/local_time.hpp>
53#include <boost/date_time/local_time/tz_database.hpp>
63#include "hegel/generators/temporal_types.h"
64#include "hegel/generators/zone_source.h"
75 inline int32_t seconds_of(const ::boost::posix_time::time_duration& d) {
76 return static_cast<int32_t
>(d.total_seconds());
79 inline std::vector<std::string> split(
const std::string& text,
81 std::vector<std::string> parts;
83 size_t end = text.find(separator);
84 while (end != std::string::npos) {
85 parts.push_back(text.substr(start, end - start));
87 end = text.find(separator, start);
89 parts.push_back(text.substr(start));
93 inline int32_t rule_time(
const std::string& text) {
94 std::vector<std::string> fields = split(text,
':');
97 for (
const std::string& field : fields) {
98 seconds += std::stoi(field) * unit;
108 rule_date(
const std::string& text) {
109 std::vector<std::string> rule_and_time = split(text,
'/');
110 int32_t time = rule_time(rule_and_time.at(1));
111 const std::string& rule = rule_and_time[0];
112 if (rule.at(0) ==
'M') {
113 std::vector<std::string> fields = split(rule.substr(1),
'.');
116 std::stoi(fields.at(0)) - 1),
117 static_cast<uint8_t
>(std::stoi(fields.at(1))),
119 (std::stoi(fields.at(2)) + 6) % 7),
122 int leap_day = std::stoi(rule);
123 constexpr int february_29 = 59;
124 if (leap_day == february_29) {
125 throw std::invalid_argument(
126 "a Boost time zone whose daylight-saving time starts or "
127 "ends on February 29 has no date in common years");
130 static_cast<uint16_t
>(leap_day < february_29 ? leap_day + 1
155 zone_data(const ::boost::local_time::time_zone_ptr& zone) {
157 throw std::invalid_argument(
"zone_data: the time zone is null");
160 detail::seconds_of(zone->base_utc_offset()),
false,
161 zone->std_zone_abbrev()};
163 if (zone->has_dst()) {
164 std::vector<std::string> parts =
165 detail::split(zone->to_posix_string(),
',');
168 detail::seconds_of(zone->base_utc_offset() +
170 true, zone->dst_zone_abbrev()},
171 detail::rule_date(parts.at(1)), detail::rule_date(parts.at(2))};
189 for (
const std::string& region : db.region_list()) {
191 zone_data(db.time_zone_from_region(region)));
A source of time zone data: the database the zone and zoned datetime generators classify,...
Definition zone_source.h:290
ZoneSource & add_zone(std::string_view name, const ZoneData &data)
Weekday
A day of the week, for restricting a date draw.
Definition temporal_types.h:736
Month
A month of the year, for restricting a date draw.
Definition temporal_types.h:747
Daylight-saving time of a yearly rule: its type, and the dates it starts and ends on each year.
Definition zone_source.h:111
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
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
A yearly rule in force after a zone's explicit transitions: the footer of a TZif file.
Definition zone_source.h:121
std::optional< DaylightRule > daylight
Daylight-saving time, or none for a fixed offset.
Definition zone_source.h:125
A local time type of a zone: an offset, its daylight-saving flag and its abbreviation.
Definition zone_source.h:21