mirror of
https://github.com/HowardHinnant/date.git
synced 2025-08-05 05:34:27 +02:00
Updated Examples and Recipes (markdown)
@@ -225,12 +225,12 @@ This creates two `year_month` objects and subtracts them. This gives a `std::ch
|
|||||||
|
|
||||||
36
|
36
|
||||||
|
|
||||||
To include the influence of the day-fields, it is best to convert `d1` and `d2` to `sys_days `s:
|
To include the influence of the day-fields, it is best to convert `d1` and `d2` to `sys_days`s:
|
||||||
|
|
||||||
auto dp1 = sys_days(d1);
|
auto dp1 = sys_days(d1);
|
||||||
auto dp2 = sys_days(d2);
|
auto dp2 = sys_days(d2);
|
||||||
|
|
||||||
Now we could (for example) subtract the two `sys_days `s, and round the result to the nearest integral month:
|
Now we could (for example) subtract the two `sys_days`s, and round the result to the nearest integral month:
|
||||||
|
|
||||||
std::cout << round<months>(dp2-dp1).count() << '\n';
|
std::cout << round<months>(dp2-dp1).count() << '\n';
|
||||||
|
|
||||||
@@ -259,11 +259,11 @@ The [ISO week date](https://en.wikipedia.org/wiki/ISO_week_date) is an internati
|
|||||||
|
|
||||||
Like `<date.h>`, you can specify an ISO week date in any of the three orders: y/wn/wd, wd/wn/y, wn/wd/y (big endian, little endian, mixed (american) endian).
|
Like `<date.h>`, you can specify an ISO week date in any of the three orders: y/wn/wd, wd/wn/y, wn/wd/y (big endian, little endian, mixed (american) endian).
|
||||||
|
|
||||||
Also like `<date.h>`, you can implicitly convert a ISO week date to `sys_days `, and vice-versa. For convenience, an alias of `date:: sys_days ` exists as `iso_week:: sys_days `:
|
Also like `<date.h>`, you can implicitly convert a ISO week date to `sys_days`, and vice-versa. For convenience, an alias of `date:: sys_days ` exists as `iso_week:: sys_days `:
|
||||||
|
|
||||||
iso_week:: sys_days dp = iso_date;
|
iso_week:: sys_days dp = iso_date;
|
||||||
|
|
||||||
And recall that `sys_days ` is just a type alias for a `std::chrono::time_point<std::chrono::system_clock, days>`. So the ISO week date (`iso_week:year_weeknum_weekday`) is immediately interoperable with the entire `<chrono>` library, just like `date::year_month_day` is.
|
And recall that `sys_days` is just a type alias for a `std::chrono::time_point<std::chrono::system_clock, days>`. So the ISO week date (`iso_week:year_weeknum_weekday`) is immediately interoperable with the entire `<chrono>` library, just like `date::year_month_day` is.
|
||||||
|
|
||||||
auto now = std::chrono::system_clock::now();
|
auto now = std::chrono::system_clock::now();
|
||||||
auto dp = date::floor<iso_week::days>(now);
|
auto dp = date::floor<iso_week::days>(now);
|
||||||
@@ -275,7 +275,7 @@ Which just output for me:
|
|||||||
|
|
||||||
2016-W11-Sat 03:07:02.460737
|
2016-W11-Sat 03:07:02.460737
|
||||||
|
|
||||||
And because `iso_week:year_weeknum_weekday` is implicitly convertible to and from `sys_days `, that makes it immediately (and explicitly) convertible to any other calendar system that is implicitly convertible to and from `sys_days `:
|
And because `iso_week:year_weeknum_weekday` is implicitly convertible to and from `sys_days`, that makes it immediately (and explicitly) convertible to any other calendar system that is implicitly convertible to and from `sys_days`:
|
||||||
|
|
||||||
auto civil_date = date::year_month_day{iso_date};
|
auto civil_date = date::year_month_day{iso_date};
|
||||||
std::cout << civil_date << ' ' << time << '\n';
|
std::cout << civil_date << ' ' << time << '\n';
|
||||||
@@ -284,7 +284,7 @@ which outputs:
|
|||||||
|
|
||||||
2016-03-19 03:07:02.460737
|
2016-03-19 03:07:02.460737
|
||||||
|
|
||||||
And there you have it: `sys_days ` is a _Rosetta Stone_ for translating _any_ calendar to any other calendar. Just make your calendar convert to and from `sys_days `, and you have interoperability with _every_ other calendar which does so.
|
And there you have it: `sys_days` is a _Rosetta Stone_ for translating _any_ calendar to any other calendar. Just make your calendar convert to and from `sys_days`, and you have interoperability with _every_ other calendar which does so.
|
||||||
|
|
||||||
using namespace date::literals;
|
using namespace date::literals;
|
||||||
auto today = 2016_y/mar/19;
|
auto today = 2016_y/mar/19;
|
||||||
@@ -381,7 +381,7 @@ Let's say you want to search the globe, and all time, for time zones when the da
|
|||||||
auto end = sys_days{jan/1/2035} + 0s;
|
auto end = sys_days{jan/1/2035} + 0s;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
auto info = z.get_info(begin, tz::utc);
|
auto info = z.get_info(begin);
|
||||||
if (info.save != 0h && info.save != 1h)
|
if (info.save != 0h && info.save != 1h)
|
||||||
{
|
{
|
||||||
std::cout << z.name() << " has a daylight savings offset of "
|
std::cout << z.name() << " has a daylight savings offset of "
|
||||||
@@ -396,9 +396,9 @@ Let's say you want to search the globe, and all time, for time zones when the da
|
|||||||
|
|
||||||
You first get a reference to the tz database, then iterate over each zone in the database. For each zone, set a range of time points to search over. In this example I start searching as far back as possible, and search forward to the year 2035.
|
You first get a reference to the tz database, then iterate over each zone in the database. For each zone, set a range of time points to search over. In this example I start searching as far back as possible, and search forward to the year 2035.
|
||||||
|
|
||||||
Starting at the beginning of time, get an `Info` for that UTC `time_point`. An `Info` looks like this:
|
Starting at the beginning of time, get an `sys_info` for that UTC `time_point`. An `sys_info` looks like this:
|
||||||
|
|
||||||
struct Info
|
struct sys_info
|
||||||
{
|
{
|
||||||
second_point begin;
|
second_point begin;
|
||||||
second_point end;
|
second_point end;
|
||||||
|
Reference in New Issue
Block a user