Hegel 0.18.3
Property-based testing for C++
Loading...
Searching...
No Matches
boost_date_time.h
1#pragma once
2
51
52#include <boost/date_time/local_time/local_time.hpp>
53#include <boost/date_time/local_time/tz_database.hpp>
54
55#include <cstddef>
56#include <cstdint>
57#include <optional>
58#include <stdexcept>
59#include <string>
60#include <utility>
61#include <vector>
62
63#include "hegel/generators/temporal_types.h"
64#include "hegel/generators/zone_source.h"
65
71
73 namespace detail {
74
75 inline int32_t seconds_of(const ::boost::posix_time::time_duration& d) {
76 return static_cast<int32_t>(d.total_seconds());
77 }
78
79 inline std::vector<std::string> split(const std::string& text,
80 char separator) {
81 std::vector<std::string> parts;
82 size_t start = 0;
83 size_t end = text.find(separator);
84 while (end != std::string::npos) {
85 parts.push_back(text.substr(start, end - start));
86 start = end + 1;
87 end = text.find(separator, start);
88 }
89 parts.push_back(text.substr(start));
90 return parts;
91 }
92
93 inline int32_t rule_time(const std::string& text) {
94 std::vector<std::string> fields = split(text, ':');
95 int32_t seconds = 0;
96 int32_t unit = 3600;
97 for (const std::string& field : fields) {
98 seconds += std::stoi(field) * unit;
99 unit /= 60;
100 }
101 return seconds;
102 }
103
104 // A rule of a Boost POSIX string, as `Mm.w.d/time` for a weekday of
105 // a month or `n/time` for a fixed month and day, which Boost writes
106 // as its zero-based day of the leap year 2004.
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), '.');
115 static_cast<generators::temporal::Month>(
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),
120 time);
121 }
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");
128 }
130 static_cast<uint16_t>(leap_day < february_29 ? leap_day + 1
131 : leap_day),
132 time);
133 }
134
135 } // namespace detail
137
155 zone_data(const ::boost::local_time::time_zone_ptr& zone) {
156 if (!zone) {
157 throw std::invalid_argument("zone_data: the time zone is null");
158 }
160 detail::seconds_of(zone->base_utc_offset()), false,
161 zone->std_zone_abbrev()};
162 generators::temporal::ZoneRule rule{standard, std::nullopt};
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() +
169 zone->dst_offset()),
170 true, zone->dst_zone_abbrev()},
171 detail::rule_date(parts.at(1)), detail::rule_date(parts.at(2))};
172 }
173 return generators::temporal::ZoneData{standard, {}, std::move(rule)};
174 }
175
187 zone_source(const ::boost::local_time::tz_database& db) {
189 for (const std::string& region : db.region_list()) {
190 source.add_zone(region,
191 zone_data(db.time_zone_from_region(region)));
192 }
193 return source;
194 }
195
196} // namespace hegel::extras::boost_date_time
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)
Zone sources from Boost.DateTime; see hegel/extras/boost_date_time.h.
Definition boost_date_time.h:70
generators::temporal::ZoneData zone_data(const ::boost::local_time::time_zone_ptr &zone)
The data of a Boost time zone: its rule for all time, with no transitions.
Definition boost_date_time.h:155
generators::temporal::ZoneSource zone_source(const ::boost::local_time::tz_database &db)
A zone source holding every region of a Boost time zone database, each under the name the database gi...
Definition boost_date_time.h:187
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