mirror of
https://github.com/HowardHinnant/date.git
synced 2025-08-06 06:04:27 +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
|
||||||
- [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)
|
||||||
|
|
||||||
***
|
***
|
||||||
|
|
||||||
@@ -99,6 +100,33 @@ std::cout << t.tm_year << "-" << t.tm_mon << "-" << t.tm_day << " ";
|
|||||||
<< t.tm_hour << ":" << t.tm_min << ":" << t.tm_sec << "\n";
|
<< t.tm_hour << ":" << t.tm_min << ":" << t.tm_sec << "\n";
|
||||||
|
|
||||||
```
|
```
|
||||||
|
<a name="normalize"></a>
|
||||||
|
### Normalizing `y/m/d` when it is `!ok()`
|
||||||
|
(by [Howard Hinnant](https://github.com/HowardHinnant))
|
||||||
|
|
||||||
|
The following function will "normalize" a `year_month_day`, much like `mktime` normalizes a `tm`. This function is not part of the library, but is offered as an example if you find your self needing things like this:
|
||||||
|
|
||||||
|
date::year_month_day
|
||||||
|
normalize(date::year_month_day ymd)
|
||||||
|
{
|
||||||
|
using namespace date;
|
||||||
|
ymd += months{0};
|
||||||
|
ymd = day_point(ymd);
|
||||||
|
return ymd;
|
||||||
|
}
|
||||||
|
|
||||||
|
The first line simply adds 0 months to the `ymd`. If `ymd.month()` is 0, with will subtract one from the `year` and set the month to `Dec`. If `ymd.month()` is greater than 12, the year will be incremented as many times as appropriate, and the month will be brought within the proper range. This operation will do nothing if `ymd.month().ok()` is already true.
|
||||||
|
|
||||||
|
The second line will "normalize" the day field. The second line requires that the month field is already normalized. For example `2015_y/dec/32` will become `2016_y/jan/1`. If the day field is already in range, there will be no change.
|
||||||
|
|
||||||
|
The conversion from `year_month_day` to `day_point` calls this algorithm:
|
||||||
|
|
||||||
|
http://howardhinnant.github.io/date_algorithms.html#days_from_civil
|
||||||
|
|
||||||
|
And the conversion from `day_point` back to `year_month_day` calls this algorithm:
|
||||||
|
|
||||||
|
http://howardhinnant.github.io/date_algorithms.html#civil_from_days
|
||||||
|
|
||||||
***
|
***
|
||||||
|
|
||||||
 _This work is licensed under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0/)._
|
 _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