mirror of
https://github.com/HowardHinnant/date.git
synced 2025-08-05 13:44:26 +02:00
Updated Examples and Recipes (markdown)
@@ -3,6 +3,7 @@ This page contains examples and recipes contributed by community members. Feel f
|
|||||||
##Contents
|
##Contents
|
||||||
- [The current local time](#localtime)
|
- [The current local time](#localtime)
|
||||||
- [The current time somewhere else](#elsetime)
|
- [The current time somewhere else](#elsetime)
|
||||||
|
- [Get the current difference between any two arbitrary time zones](#deltatz)
|
||||||
- [Obtaining a `time_point` from `y/m/d h:m:s` components](#time_point_to_components)
|
- [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)
|
- [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)
|
- [Normalizing `y/m/d` when it is `!ok()`](#normalize)
|
||||||
@@ -71,6 +72,38 @@ This outputs the current time in Shanghai, for example:
|
|||||||
|
|
||||||
All IANA timezone names (or links -- aliases to timezones) are supported.
|
All IANA timezone names (or links -- aliases to timezones) are supported.
|
||||||
|
|
||||||
|
<a name="deltatz"></a>
|
||||||
|
### Get the current difference between any two arbitrary time zones
|
||||||
|
(by [Howard Hinnant](https://github.com/HowardHinnant))
|
||||||
|
|
||||||
|
Would you just like to know how many hours ahead or behind your friend is in another time zone? Here's how you do that with this library.
|
||||||
|
|
||||||
|
#include "tz.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int
|
||||||
|
main()
|
||||||
|
{
|
||||||
|
auto current_time = std::chrono::system_clock::now();
|
||||||
|
auto la = date::make_zoned("America/Los_Angeles", current_time);
|
||||||
|
auto sy = date::make_zoned("Australia/Sydney", current_time);
|
||||||
|
std::cout << date::make_time(sy.get_local_time() - la.get_local_time()) << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
Let's say you want to find out how many hours ahead Sydney is from LA (now). First get the current UTC time. Then create a `zoned_time` for both "America/Los_Angeles" and "Australia/Sydney" using that current UTC time. Then subtract their `local_time`s. `make_time` is a handy formatting tool for printing out that duration:
|
||||||
|
|
||||||
|
17:00:00.000000
|
||||||
|
|
||||||
|
Because we did not specify otherwise, the default behavior is to give us the full precision of `system_clock::time_point`. If you would prefer other behavior (e.g. minutes precision), that is easily accomplished with just a little more work. `using` directives make the code a little more readable:
|
||||||
|
|
||||||
|
using namespace date;
|
||||||
|
using namespace std::chrono;
|
||||||
|
std::cout << make_time(floor<minutes>(sy.get_local_time() - la.get_local_time())) << '\n';
|
||||||
|
|
||||||
|
And now the output is:
|
||||||
|
|
||||||
|
17:00
|
||||||
|
|
||||||
<a name="time_point_to_components"></a>
|
<a name="time_point_to_components"></a>
|
||||||
### Obtaining a `time_point` from `y/m/d h:m:s` components
|
### Obtaining a `time_point` from `y/m/d h:m:s` components
|
||||||
(by [ecorm](https://github.com/ecorm))
|
(by [ecorm](https://github.com/ecorm))
|
||||||
|
Reference in New Issue
Block a user