mirror of
https://github.com/HowardHinnant/date.git
synced 2025-08-05 13:44:26 +02:00
Updated Examples and Recipes (markdown)
@@ -1,6 +1,8 @@
|
||||
This page contains examples and recipes contributed by community members. Feel free to add your own contributions by clicking on the "Edit" button. Please "sign" your contributions by adding a link to your GitHub profile. **But please understand that your contributions will henceforth be considered donated under the [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0/)**. If this requirement is a problem for anyone, bring it to Howard's attention and we will try to work out a compromise.
|
||||
|
||||
##Contents
|
||||
- [The current local time](#localtime)
|
||||
- [The current time somewhere else](#elsetime)
|
||||
- [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)
|
||||
@@ -19,6 +21,56 @@ This page contains examples and recipes contributed by community members. Feel f
|
||||
|
||||
***
|
||||
|
||||
<a name="localtime"></a>
|
||||
### The current local time
|
||||
(by [Howard Hinnant](https://github.com/HowardHinnant))
|
||||
|
||||
This couldn't be easier:
|
||||
|
||||
```c++
|
||||
#include "tz.h"
|
||||
#include <iostream>
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
std::cout << date::make_zoned(date::current_zone(), std::chrono::system_clock::now()) << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
`system_clock::now()` of course gets the current time. It is a de facto standard that this current time is measured in terms of [Unix Time](https://en.wikipedia.org/wiki/Unix_time) which is a very close approximation to UTC. `make_zoned` pairs the current UTC time with the computer's current local time zone, producing a `zoned_time`. The `zoned_time` has a streaming operator so you can easily print it out:
|
||||
|
||||
```c++
|
||||
2016-07-05 23:01:05.818378 EDT
|
||||
```
|
||||
|
||||
Note that the precision of this output is with whatever precision your `system_clock::now()` supports (microseconds on macOS where I'm writing this).
|
||||
|
||||
<a name="elsetime"></a>
|
||||
### The current time somewhere else
|
||||
(by [Howard Hinnant](https://github.com/HowardHinnant))
|
||||
|
||||
If you need to find out the current time where you _aren't_, then that is a simple matter too:
|
||||
|
||||
```c++
|
||||
#include "tz.h"
|
||||
#include <iostream>
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
std::cout << date::make_zoned("Asia/Shanghai", std::chrono::system_clock::now()) << '\n';
|
||||
}
|
||||
```
|
||||
|
||||
This outputs the current time in Shanghai, for example:
|
||||
|
||||
```c++
|
||||
2016-07-06 11:01:05.818378 CST
|
||||
```
|
||||
|
||||
All IANA timezone names (or links -- aliases to timezones) are supported.
|
||||
|
||||
<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