Hegel 0.18.3
Property-based testing for C++
Loading...
Searching...
No Matches
hegel::generators::temporal::ZoneSource Class Reference

A source of time zone data: the database the zone and zoned datetime generators classify, target and resolve wall times against. More...

#include <zone_source.h>

Public Member Functions

 ZoneSource ()
 An empty source, to be filled with the add_* functions.
ZoneSource & add_tzif (std::string_view name, const std::vector< uint8_t > &bytes)
ZoneSource & add_tzif (std::string_view name, const uint8_t *data, size_t size)
ZoneSource & add_zone (std::string_view name, const ZoneData &data)
ZoneSource & add_oracle (std::string_view name, const ZoneOracle &oracle)
ZoneSource & add_link (std::string_view name, std::string_view target)
ZoneSource & declare (const std::vector< std::string > &names)
ZoneSource & set_loader (ZoneLoader loader)
ZoneSource & set_version (std::string_view version)
std::string version () const
std::optional< ZoneInfo > info (std::string_view name) const
ZonedDateTime resolve (std::string_view name, const DateTime &civil) const
ZoneOffset offset_at (std::string_view name, const Instant &instant) const

Static Public Member Functions

static ZoneSource bundled ()

Detailed Description

A source of time zone data: the database the zone and zoned datetime generators classify, target and resolve wall times against.

By default every draw and query uses the engine's bundled IANA database, ZoneSource::bundled(). When the code under test uses another database, give its data to a ZoneSource and either pass it to a generator (zones({.source = source}) or zoned_datetimes({.source = source})) or make it the default with set_default_zone_source(). The zone names, their link and legacy flags and the order they shrink in always come from the bundled catalogue, so a stored failure shrinks the same way everywhere; only what a wall time means in a zone comes from the source. A name the catalogue lacks is drawn too, after every catalogued name, and is never a legacy name, whatever its area (such as Mars/Olympus).

A source takes data in three shapes: TZif files (add_tzif()), fields (add_zone()) and an offset oracle (add_oracle()), plus links (add_link()) and names whose data a loader supplies on first use (declare() and set_loader()).

Build a source once, outside the test body, and share it between test cases: a source built in the body is built again for each one.

ZoneSource system_zones() {
ZoneSource source;
source.declare({"America/New_York", "Europe/London"})
.set_loader([](ZoneSource& s, const std::string& name) {
s.add_tzif(name, read_file("/usr/share/zoneinfo/" + name));
});
return source;
}
TEST(Meetings, StartInTheirZone) {
auto when = zoned_datetimes({.source = system_zones()});
ZonedDateTime start = tc.draw(when);
// ...
});
}
Handle to the currently-executing test case.
Definition test_case.h:46
T draw(const generators::Generator< T > &gen) const
Draw a random value from a printable generator.
Definition core.h:993
A source of time zone data: the database the zone and zoned datetime generators classify,...
Definition zone_source.h:290
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 & declare(const std::vector< std::string > &names)
ZoneSource & set_loader(ZoneLoader loader)
Generator< ZonedDateTime > zoned_datetimes(const ZonedDatetimesParams &params={})
Generate ZonedDateTime values: a zone, a wall-clock time in it and, when the clocks repeated that tim...
void test(const std::function< void(TestCase &)> &test_fn, const Settings &settings={}, const std::vector< std::string > &failure_blobs={}, const std::vector< ExplicitTestCase > &explicit_cases={}, const char *caller_file=HEGEL_CALLER_FILE, const char *caller_function=HEGEL_CALLER_FUNCTION, int caller_line=HEGEL_CALLER_LINE)
Run a Hegel test.

Copies share one source: data added through one copy is visible through every other. A source may be used and filled from any number of threads. Add or declare names before building generators that draw from them: a generator reads the names a source knows when it is built.

Member Function Documentation

◆ add_link()

ZoneSource & hegel::generators::temporal::ZoneSource::add_link ( std::string_view name,
std::string_view target )

Make name a link to target, whose data then applies to both. The target need not be known yet.

Parameters
nameThe link's name.
targetThe name it links to.
Returns
This source.
Exceptions
std::invalid_argumentfor a bad name or a link to itself.

◆ add_oracle()

ZoneSource & hegel::generators::temporal::ZoneSource::add_oracle ( std::string_view name,
const ZoneOracle & oracle )

Add a zone by probing an offset oracle; see ZoneOracle.

Parameters
nameAs for add_tzif().
oracleThe callbacks and the probe range.
Returns
This source.
Exceptions
std::invalid_argumentfor a missing offset_at, a probe range out of order or outside the engine's years, a callback that throws or answers an invalid type, a next transition that is not later than the instant asked about, more than 100000 transitions or a bad name.

◆ add_tzif() [1/2]

ZoneSource & hegel::generators::temporal::ZoneSource::add_tzif ( std::string_view name,
const std::vector< uint8_t > & bytes )

Add a zone from a TZif file (RFC 9636, version 2 or later), such as one under /usr/share/zoneinfo. Its 64-bit block and its footer are read.

Parameters
nameThe name the zone is drawn and queried by: not empty, without NUL bytes and not Etc/Unknown.
bytesThe file's contents.
Returns
This source.
Exceptions
std::invalid_argumentfor a malformed file, an offset of two days or more or a bad name.

◆ add_tzif() [2/2]

ZoneSource & hegel::generators::temporal::ZoneSource::add_tzif ( std::string_view name,
const uint8_t * data,
size_t size )

Add a zone from a TZif file held in a buffer; see the other add_tzif().

Parameters
nameThe name the zone is drawn and queried by.
dataThe first byte of the file's contents, read during the call only.
sizeThe number of bytes.
Returns
This source.
Exceptions
std::invalid_argumentas the other add_tzif() does.

◆ add_zone()

ZoneSource & hegel::generators::temporal::ZoneSource::add_zone ( std::string_view name,
const ZoneData & data )

Add a zone from its data given as fields.

Parameters
nameAs for add_tzif().
dataThe zone's data.
Returns
This source.
Exceptions
std::invalid_argumentfor transitions out of order, an offset of two days or more, a field out of range or a bad name.

◆ bundled()

ZoneSource hegel::generators::temporal::ZoneSource::bundled ( )
static

The engine's bundled IANA database. It cannot be changed: each add_* and set_* function on it throws std::invalid_argument.

Returns
The bundled source.

◆ declare()

ZoneSource & hegel::generators::temporal::ZoneSource::declare ( const std::vector< std::string > & names)

Declare names whose data comes later: from the loader the first time a draw or query needs it, or from an add_* call before a zoned datetime is drawn in that zone. A name the source already has keeps what it has.

Parameters
namesThe names, each as for add_tzif().
Returns
This source.
Exceptions
std::invalid_argumentfor a bad name.

◆ info()

std::optional< ZoneInfo > hegel::generators::temporal::ZoneSource::info ( std::string_view name) const

What this source knows about name, as info() does for the default source. A name the catalogue lacks reports the source's link target, the rank after every catalogued name and the tags of its data and spelling.

Parameters
nameThe zone name to look up.
Returns
The description, or std::nullopt for a name the source does not know.
Exceptions
std::invalid_argumentwhen loading the name's data fails.

◆ offset_at()

ZoneOffset hegel::generators::temporal::ZoneSource::offset_at ( std::string_view name,
const Instant & instant ) const

The offset a zone of this source has in force at an instant, as offset_at() does for the default source.

Parameters
nameThe zone name.
instantThe instant to look up.
Returns
The offset in force.
Exceptions
std::invalid_argumentas offset_at() does, or when the source has no data for name.

◆ resolve()

ZonedDateTime hegel::generators::temporal::ZoneSource::resolve ( std::string_view name,
const DateTime & civil ) const

Resolve a wall time in a zone of this source, as resolve() does for the default source.

Parameters
nameThe zone name.
civilThe wall-clock time to resolve.
Returns
The resolved zoned datetime.
Exceptions
std::invalid_argumentas resolve() does, or when the source has no data for name.

◆ set_loader()

ZoneSource & hegel::generators::temporal::ZoneSource::set_loader ( ZoneLoader loader)

Set the loader asked for the data of a name the source lacks; see ZoneLoader. It replaces the loader set before, which is destroyed once no call of it is running, and an empty function removes it. A name the loader adds nothing for is unknown from then on.

Parameters
loaderThe loader.
Returns
This source.
Exceptions
std::invalid_argumentfor the bundled source.

◆ set_version()

ZoneSource & hegel::generators::temporal::ZoneSource::set_version ( std::string_view version)

Record which release of its database the source holds. Failure output does not report it.

Parameters
versionThe release, such as "2026e".
Returns
This source.

◆ version()

std::string hegel::generators::temporal::ZoneSource::version ( ) const
Returns
The release of the source's database: for the bundled source tzdb_version(), for another what set_version() recorded, or an empty string.

The documentation for this class was generated from the following file: