Updated Examples and Recipes (markdown)

Howard Hinnant
2021-04-16 21:13:04 -04:00
parent 643336343b
commit 3fa3690604

@@ -587,7 +587,7 @@ std::chrono::system_clock::time_point datetime_to_timepoint(const DateTime &dt,
auto ymd = year(dt.year)/dt.month/dt.day; // year_month_day type
if (!ymd.ok()) { throw std::runtime_error("Invalid date"); }
return make_zoned(tzone, local_days{ymd} + hours(dt.hour) + minutes(dt.min) + seconds(dt.sec)).get_sys_time();
return zoned_time{tzone, local_days{ymd} + hours(dt.hour) + minutes(dt.min) + seconds(dt.sec)}.get_sys_time();
}
```
@@ -600,9 +600,9 @@ auto tp = datetime_to_timepoint(datetime, date::current_zone()); // datetime fro
using date::operator<<;
std::cout << tp << std::endl;
std::cout << date::make_zoned("UTC", tp) << std::endl;
std::cout << date::make_zoned(date::current_zone(), tp) << std::endl;
std::cout << date::make_zoned(date::locate_zone("Europe/Moscow"), tp) << std::endl;
std::cout << date::zoned_time{"UTC", tp} << std::endl;
std::cout << date::zoned_time{date::current_zone(), tp} << std::endl;
std::cout << date::zoned_time{date::locate_zone("Europe/Moscow"), tp} << std::endl;
// Will print e.g. (if you're in CET timezone...)
// 2016-12-24 22:00:00.000000000
@@ -615,9 +615,9 @@ std::cout << date::make_zoned(date::locate_zone("Europe/Moscow"), tp) << std::en
auto tp2 = datetime_to_timepoint(datetime, date::locate_zone("America/New_York")); // datetime from New York
std::cout << tp2 << std::endl;
std::cout << date::make_zoned("UTC", tp2) << std::endl;
std::cout << date::make_zoned(date::current_zone(), tp2) << std::endl;
std::cout << date::make_zoned(date::locate_zone("Europe/Moscow"), tp2) << std::endl;
std::cout << date::zoned_time{"UTC", tp2} << std::endl;
std::cout << date::zoned_time{date::current_zone(), tp2} << std::endl;
std::cout << date::zoned_time{date::locate_zone("Europe/Moscow"), tp2} << std::endl;
// Will print
// 2016-12-25 04:00:00.000000000
@@ -2077,7 +2077,7 @@ int main()
// bool operator tells us whether stream was successfully parsed
assert(bool(inputStream));
auto zt = make_zoned(tz_name, tp);
zoned_time zt{tz_name, tp};
// 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';
@@ -2130,15 +2130,15 @@ int main()
case local_info::nonexistent:
// time stamp never existed. Throw an error?
// Or here is how map to a unique UTC equivalent:
zt = make_zoned(zone, tp, choose::earliest); // choose::latest also
zt = zoned_time{zone, tp, choose::earliest}; // choose::latest also
// gives same answer.
break;
case local_info::ambiguous:
// Use tz_abbrev to break the ambiguity
if (info.first.abbrev == tz_abbrev)
zt = make_zoned(zone, tp, choose::earliest);
zt = zoned_time{zone, tp, choose::earliest};
else if (info.second.abbrev == tz_abbrev)
zt = make_zoned(zone, tp, choose::latest);
zt = zoned_time{zone, tp, choose::latest};
else
throw std::runtime_error(tz_abbrev +
" is not a valid abbreviation for " + tz_name);
@@ -2331,9 +2331,9 @@ tzdb_manager(std::atomic<bool>& run)
using namespace date;
// Get the current UTC time for today's local 02:00, approximation is ok
auto tz = current_zone();
auto check_at = make_zoned(tz, floor<days>(make_zoned(tz, system_clock::now())
zoned_time check_at{tz, floor<days>(zoned_time{tz, system_clock::now()}
.get_local_time()) + 2h,
choose::latest).get_sys_time();
choose::latest}.get_sys_time();
// Initialize clean-trigger for several years in the future
auto clean_at = check_at + days{1000};
while (run)