mirror of
https://github.com/HowardHinnant/date.git
synced 2025-08-02 20:24:27 +02:00
show throw example
@@ -527,7 +527,7 @@ which currently outputs `2016f`.
|
||||
<a name="time_point_to_components"></a>
|
||||
### Obtaining a `time_point` from `y/m/d h:m:s` components
|
||||
|
||||
#### Date/time, interpreted as UTC
|
||||
#### Date/time interpreted as UTC, to `time_point`
|
||||
Converting a date/time, interpreted as UTC
|
||||
```cpp
|
||||
struct DateTime { // hold date/time (interpreted as UTC), to be converted to time_point
|
||||
@@ -574,7 +574,7 @@ std::cout << "Date/Time is " << tp << std::endl;
|
||||
|
||||
The above needs only the `"date/date.h"` header. (`"date/tz.h"` header and tz library are not needed).
|
||||
|
||||
#### Date/time, interpreted as coming from a specific time-zone
|
||||
#### Date/time (interpreted as coming from a specific time-zone), to `time_point`
|
||||
If the date/time is taken as being from a specific time-zone, then "date/tz.h" header and tz library are needed:
|
||||
|
||||
```cpp
|
||||
@@ -595,8 +595,10 @@ The above function can be called as follows
|
||||
|
||||
```cpp
|
||||
DateTime datetime{2016 /*year*/, 12 /*month*/, 24 /*day*/, 23 /* hour */, 0 /* min */, 0 /*sec*/}; // variable definition
|
||||
using date::operator<<;
|
||||
|
||||
auto tp = datetime_to_timepoint(datetime, date::current_zone()); // datetime from local timezone
|
||||
|
||||
using date::operator<<;
|
||||
std::cout << tp << std::endl;
|
||||
std::cout << date::make_zoned("UTC", tp) << std::endl;
|
||||
std::cout << date::make_zoned(date::current_zone(), tp) << std::endl;
|
||||
@@ -609,7 +611,9 @@ std::cout << date::make_zoned(date::locate_zone("Europe/Moscow"), tp) << std::en
|
||||
// 2016-12-25 01:00:00.000000000 MSK
|
||||
|
||||
|
||||
|
||||
auto tp2 = datetime_to_timepoint(datetime, date::locate_zone("America/New_York")); // datetime from New York
|
||||
|
||||
std::cout << tp2 << std::endl;
|
||||
std::cout << date::make_zoned("UTC", tp2) << std::endl;
|
||||
std::cout << date::make_zoned(date::current_zone(), tp2) << std::endl;
|
||||
@@ -620,6 +624,18 @@ std::cout << date::make_zoned(date::locate_zone("Europe/Moscow"), tp2) << std::e
|
||||
// 2016-12-25 04:00:00.000000000 UTC
|
||||
// 2016-12-25 05:00:00.000000000 CET // e.g. (if you're in CET timezone...)
|
||||
// 2016-12-25 07:00:00.000000000 MSK
|
||||
|
||||
|
||||
|
||||
datetime = DateTime{2018 /*year*/, 3 /*month*/, 25 /*day*/, 2 /* hour */, 10 /* min */, 0 /*sec*/};
|
||||
auto tp3 = datetime_to_timepoint(datetime, date::locate_zone("Europe/Berlin"));
|
||||
|
||||
// Will throw, with the following message
|
||||
// terminate called after throwing an instance of 'date::nonexistent_local_time'
|
||||
// what(): 2018-03-25 02:10:00 is in a gap between
|
||||
// 2018-03-25 02:00:00 CET and
|
||||
// 2018-03-25 03:00:00 CEST which are both equivalent to
|
||||
// 2018-03-25 01:00:00 UTC
|
||||
```
|
||||
|
||||
#### `struct tm` holding the components
|
||||
|
Reference in New Issue
Block a user