Updated Examples and Recipes (markdown)

Howard Hinnant
2017-04-17 13:19:18 -04:00
parent b2b63744ca
commit 7e7754d287

@@ -1622,59 +1622,59 @@ There is now functionality to parse the original format:
And check if the time zone abbreviation is consistent, and in the ambiguous case, use the time zone abbreviation to disambiguate the time stamp: And check if the time zone abbreviation is consistent, and in the ambiguous case, use the time zone abbreviation to disambiguate the time stamp:
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include "tz.h" #include "tz.h"
int main() int main()
{ {
using namespace std; using namespace std;
using namespace date; using namespace date;
istringstream inputStream{ "1999-10-31 00:30:00 US/Pacific PST" }; istringstream inputStream{ "1999-10-31 00:30:00 US/Pacific PST" };
// Using local_seconds would resolve in ambiguous date exception // Using local_seconds would resolve in ambiguous date exception
local_seconds tp; local_seconds tp;
string tz_name; string tz_name;
string tz_abbrev; string tz_abbrev;
inputStream >> parse("%F %T", tp) >> tz_name >> tz_abbrev; inputStream >> parse("%F %T", tp) >> tz_name >> tz_abbrev;
// bool operator tells us whether stream was successfully parsed // bool operator tells us whether stream was successfully parsed
assert(bool(inputStream)); assert(bool(inputStream));
// Check for ambiguous and nonexistent timestamps // Check for ambiguous and nonexistent timestamps
auto zone = locate_zone(tz_name); auto zone = locate_zone(tz_name);
auto info = zone->get_info(tp); auto info = zone->get_info(tp);
zoned_seconds zt{zone}; zoned_seconds zt{zone};
switch (info.result) switch (info.result)
{ {
case local_info::unique: case local_info::unique:
zt = tp; // easy case zt = tp; // easy case
// One can check that the tz_abbrev is consistent with // One can check that the tz_abbrev is consistent with
// info.first.abbrev if desired. // info.first.abbrev if desired.
break; break;
case local_info::nonexistent: case local_info::nonexistent:
// time stamp never existed. Throw an error? // time stamp never existed. Throw an error?
// Or here is how map to a unique UTC equivalent: // Or here is how map to a unique UTC equivalent:
zt = make_zoned(zone, tp, choose::earliest); // choose::latest also zt = make_zoned(zone, tp, choose::earliest); // choose::latest also
// gives same answer. // gives same answer.
break; break;
case local_info::ambiguous: case local_info::ambiguous:
// Use tz_abbrev to break the ambiguity // Use tz_abbrev to break the ambiguity
if (info.first.abbrev == tz_abbrev) if (info.first.abbrev == tz_abbrev)
zt = make_zoned(zone, tp, choose::earliest); zt = make_zoned(zone, tp, choose::earliest);
else if (info.second.abbrev == tz_abbrev) else if (info.second.abbrev == tz_abbrev)
zt = make_zoned(zone, tp, choose::latest); zt = make_zoned(zone, tp, choose::latest);
else else
throw std::runtime_error(tz_abbrev + throw std::runtime_error(tz_abbrev +
" is not a valid abbreviation for " + tz_name); " is not a valid abbreviation for " + tz_name);
break; break;
} }
// This will output America/Los_Angeles, because US/Pacific is an alias of it. // This will output America/Los_Angeles, because US/Pacific is an alias of it.
cout << format("%F %T %Ez", zt) << ' ' << zt.get_time_zone()->name() << '\n'; cout << format("%F %T %Ez", zt) << ' ' << zt.get_time_zone()->name() << '\n';
} }
This involves getting the `local_info` structure from the `time_zone` for that `local_time`. The `local_info` will have all of the information about that `time_zone/local_time` combination, including whether there is a unique mapping to UTC, a non-existing mapping (as in the gap created by "spring forward"), or an ambiguous mapping (created by a local time occurring twice during a "fall back"). This involves getting the `local_info` structure from the `time_zone` for that `local_time`. The `local_info` will have all of the information about that `time_zone/local_time` combination, including whether there is a unique mapping to UTC, a non-existing mapping (as in the gap created by "spring forward"), or an ambiguous mapping (created by a local time occurring twice during a "fall back").