mirror of
https://github.com/HowardHinnant/date.git
synced 2025-08-05 13:44:26 +02:00
Updated Examples and Recipes (markdown)
@@ -7,6 +7,7 @@ This page contains examples and recipes contributed by community members. Feel f
|
||||
- [Converting from {year, microseconds} to CCSDS](#ccsds)
|
||||
- [Difference in months between two dates](#deltamonths)
|
||||
- [Parsing ISO strings](http://stackoverflow.com/a/33438989/576911)
|
||||
- [2Gs Birthday](#birthday2gs)
|
||||
|
||||
***
|
||||
|
||||
@@ -244,6 +245,38 @@ This outputs:
|
||||
|
||||
These are all reasonable answers to the question, and all easily computable with this library.
|
||||
|
||||
<a name="birthday2gs"></a>
|
||||
### 2Gs Birthday
|
||||
(by [Howard Hinnant](https://github.com/HowardHinnant))
|
||||
|
||||
This example demonstrates both some simple date arithmetic, and how to handle discontinuities in a timezone. Dave was born in the "America/Los_Angeles" timezone at 10:03am on April 24, 1954. When will he be 2,000,000,000 seconds old (in the same timezone?
|
||||
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include "date.h"
|
||||
#include "tz.h"
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
using namespace std::chrono_literals;
|
||||
using namespace date;
|
||||
// Dave was born April 24, 1954. 10:03 AM pst
|
||||
// Want to know when he is 2 Gigaseconds old
|
||||
auto birth = day_point{apr/24/1954} + 10h + 3min;
|
||||
auto z = locate_zone("America/Los_Angeles");
|
||||
auto t = z->to_sys(birth);
|
||||
t += 2'000'000'000s;
|
||||
auto p = z->to_local(t);
|
||||
std::cout << p.first << ' ' << p.second << '\n';
|
||||
}
|
||||
|
||||
One first creates the local time, and then converts that to UTC using the `Zone` for "America/Los_Angeles". Then add 2Gs, then convert the time from UTC back to "America/Los_Angeles". The result is a pair, with the first part holding a `time_point` and the second part holding the abbreviation for the local time. This outputs:
|
||||
|
||||
2017-09-08 14:36:20 PDT
|
||||
|
||||
Note that without handling the timezone correctly, this result would be an hour off (2017-09-08 13:36:20) because the birth date falls in PST, and the celebration date falls in PDT.
|
||||
|
||||
***
|
||||
|
||||
 _This work is licensed under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0/)._
|
Reference in New Issue
Block a user