mirror of
https://github.com/HowardHinnant/date.git
synced 2025-08-05 13:44:26 +02:00
Updated Examples and Recipes (markdown)
@@ -4,6 +4,7 @@ This page contains examples and recipes contributed by community members. Feel f
|
||||
- [The current local time](#localtime)
|
||||
- [The current time somewhere else](#elsetime)
|
||||
- [Get the current difference between any two arbitrary time zones](#deltatz)
|
||||
- [Set simultaneous meeting in two different time zones](#meeting)
|
||||
- [Obtaining a `time_point` from `y/m/d h:m:s` components](#time_point_to_components)
|
||||
- [Obtaining `y/m/d h:m:s` components from a `time_point`](#components_to_time_point)
|
||||
- [Normalizing `y/m/d` when it is `!ok()`](#normalize)
|
||||
@@ -104,6 +105,56 @@ And now the output is:
|
||||
|
||||
17:00
|
||||
|
||||
<a name="meeting"></a>
|
||||
### 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?
|
||||
|
||||
#include "tz.h"
|
||||
#include <iostream>
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
using namespace std::chrono;
|
||||
using namespace date;
|
||||
auto ny = make_zoned("America/New_York", local_days{jul/7/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".
|
||||
|
||||
Then just print it out:
|
||||
|
||||
2016-07-07 09:00:00 EDT
|
||||
2016-07-07 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{jul/7/2016} + 16h);
|
||||
auto ny = make_zoned("America/New_York", moscow);
|
||||
|
||||
This would result in the exact same output.
|
||||
|
||||
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{jul/7/2016} + 13h;
|
||||
auto ny = make_zoned("America/New_York", utc);
|
||||
auto moscow = make_zoned("Europe/Moscow", utc);
|
||||
|
||||
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 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
|
||||
|
||||
<a name="time_point_to_components"></a>
|
||||
### Obtaining a `time_point` from `y/m/d h:m:s` components
|
||||
(by [ecorm](https://github.com/ecorm))
|
||||
|
Reference in New Issue
Block a user