Updated Examples and Recipes (markdown)

Howard Hinnant
2016-07-07 22:03:36 -04:00
parent 7d74a91aa3
commit 8f09e07112

@@ -109,7 +109,7 @@ And now the output is:
### Set simultaneous meeting in two different time zones
(by [Howard Hinnant](https://github.com/HowardHinnant))
Say you want to set up a video conference between New York and Moscow. This can be done in a few simple ways. First you need to decide when the meeting is going to be with respect to _somebody's_ clock. For example, let's say we want to have the meeting on Jul 7, 2016 at 9am in New York. How do we define that time, and then find the same instant in Moscow?
Say you want to set up a video conference between New York and Moscow. This can be done in a few simple ways. First you need to decide when the meeting is going to be with respect to _somebody's_ clock. For example, let's say we want to have the meeting on Jul 8, 2016 at 9am in New York. How do we define that time, and then find the same instant in Moscow?
#include "tz.h"
#include <iostream>
@@ -119,29 +119,29 @@ Say you want to set up a video conference between New York and Moscow. This can
{
using namespace std::chrono;
using namespace date;
auto ny = make_zoned("America/New_York", local_days{jul/7/2016} + 9h);
auto ny = make_zoned("America/New_York", local_days{jul/8/2016} + 9h);
auto moscow = make_zoned("Europe/Moscow", ny);
std::cout << ny << '\n';
std::cout << moscow << '\n';
}
The use of `local_days` creates a calendar date local to whatever time zone you pair it with (in this case "America/New_York"). To make this some time other than midnight, just add the time duration since midnight (hours, minutes, seconds, whatever) to the `local_days`, in this case `9h` for 09:00:00. This forms a `zoned_time` that corresponds to 2016-07-07 09:00:00 EDT. To find the same time in Moscow, just create a new `zoned_time` with "Europe/Moscow" and the New York `zoned_time`. This creates a `zoned_time` with the equivalent UTC time, but associated with the time zone "Europe/Moscow".
The use of `local_days` creates a calendar date local to whatever time zone you pair it with (in this case "America/New_York"). To make this some time other than midnight, just add the time duration since midnight (hours, minutes, seconds, whatever) to the `local_days`, in this case `9h` for 09:00:00. This forms a `zoned_time` that corresponds to 2016-07-08 09:00:00 EDT. To find the same time in Moscow, just create a new `zoned_time` with "Europe/Moscow" and the New York `zoned_time`. This creates a `zoned_time` with the equivalent UTC time, but associated with the time zone "Europe/Moscow".
Then just print it out:
2016-07-07 09:00:00 EDT
2016-07-07 16:00:00 MSK
2016-07-08 09:00:00 EDT
2016-07-08 16:00:00 MSK
Obviously you could just as easily specify the meeting in Moscow's time zone and then find the equivalent time in New York:
auto moscow = make_zoned("Europe/Moscow", local_days{7_d/jul/2016} + 16h);
auto moscow = make_zoned("Europe/Moscow", local_days{8_d/jul/2016} + 16h);
auto ny = make_zoned("America/New_York", moscow);
This would result in the exact same output. For those paying attention, I reordered the date from m/d/y to d/m/y just to show that I could. The meaning is the same.
Sometimes it is convenient to specify the time independent of either timezone. For example this might be some celestial event such as an eclipse. Often such events are recorded in UTC so that people in all time zones can more easily know the correct time. It is just as easy to use UTC for this example:
auto utc = sys_days{2016_y/jul/7} + 13h;
auto utc = sys_days{2016_y/jul/8} + 13h;
auto ny = make_zoned("America/New_York", utc);
auto moscow = make_zoned("Europe/Moscow", etc);
@@ -149,13 +149,13 @@ I reordered the date to y/m/d just to show that I could. As long as the first u
Instead of `local_days`, `sys_days` is used instead. `sys_days` means UTC (technically it means [Unix Time](https://en.wikipedia.org/wiki/Unix_time) which is a very close approximation to UTC). Then you can construct each `zoned_time` with the UTC time (which has type `sys_time<hours>` in this example). You could also construct the second `zoned_time` from the first, just as before:
auto ny = make_zoned("America/New_York", sys_days{jul/7/2016} + 13h);
auto ny = make_zoned("America/New_York", sys_days{jul/8/2016} + 13h);
auto moscow = make_zoned("Europe/Moscow", ny);
In any event, the output is still:
2016-07-07 09:00:00 EDT
2016-07-07 16:00:00 MSK
2016-07-08 09:00:00 EDT
2016-07-08 16:00:00 MSK
<a name="time_point_to_components"></a>
### Obtaining a `time_point` from `y/m/d h:m:s` components