Updated Examples and Recipes (markdown)

Howard Hinnant
2016-05-21 11:25:13 -04:00
parent 2bf241aad1
commit f1e78bcbb3

@@ -312,7 +312,7 @@ This example demonstrates both some simple date arithmetic, and how to handle di
#include <iostream> #include <iostream>
#include "date.h" #include "date.h"
#include "tz.h" #include "tz.h"
int int
main() main()
{ {
@@ -320,17 +320,16 @@ This example demonstrates both some simple date arithmetic, and how to handle di
using namespace date; using namespace date;
// Dave was born April 24, 1954. 10:03 AM pst // Dave was born April 24, 1954. 10:03 AM pst
// Want to know when he is 2 Gigaseconds old // Want to know when he is 2 Gigaseconds old
auto birth = sys_days{apr/24/1954} + 10h + 3min; auto birthday = make_zoned("America/Los_Angeles", local_days{apr/24/1954} + 10h + 3min);
auto z = locate_zone("America/Los_Angeles"); std::cout << "born : " << birthday << '\n';
auto t = z->to_sys(birth); birthday = birthday.get_sys_time() + 2'000'000'000s;
t += 2'000'000'000s; std::cout << "2Gs birthday: " << birthday << '\n';
auto p = z->to_local(t);
std::cout << p.first << ' ' << p.second << '\n';
} }
One first creates the local time, and then converts that to UTC using the `Zone` for "America/Los_Angeles". Then add 2Gs, then convert the time from UTC back to "America/Los_Angeles". The result is a pair, with the first part holding a `time_point` and the second part holding the abbreviation for the local time. This outputs: One first creates the local time, and then pairs that to the time zone "America/Los_Angeles" using `make_zoned`. Then add 2Gs to the `sys_time` (not the `local_time`) of `birthday`. This outputs:
2017-09-08 14:36:20 PDT `born : 1954-04-24 10:03:00 PST`
`2Gs birthday: 2017-09-08 14:36:20 PDT`
Note that without handling the timezone correctly, this result would be an hour off (2017-09-08 13:36:20) because the birth date falls in PST, and the celebration date falls in PDT. Note that without handling the timezone correctly, this result would be an hour off (2017-09-08 13:36:20) because the birth date falls in PST, and the celebration date falls in PDT.