Updated Examples and Recipes (markdown)

HowardHinnant
2015-09-04 11:50:19 -04:00
parent c33ed733e1
commit a073636fd4

@@ -5,6 +5,7 @@ This page contains examples and recipes contributed by community members. Feel f
- [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) - [Normalizing `y/m/d` when it is `!ok()`](#normalize)
- [Converting from {year, microseconds} to CCSDS](#ccsds) - [Converting from {year, microseconds} to CCSDS](#ccsds)
- [Difference in months between two dates](#deltamonths)
*** ***
@@ -192,6 +193,56 @@ The next step is to find when the day started that is associated with `utc`. To
Now the number of microseconds since the start of the day needs to be computed. The start of the day, `dp`, is converted back into the leap-second aware system, and subtracted from the microsecond time point: `utc`. The variable `us` is reused to hold “microseconds since midnight”. Now it is a simple computation to split this into milliseconds since midnight, and microseconds since the last millisecond. Now the number of microseconds since the start of the day needs to be computed. The start of the day, `dp`, is converted back into the leap-second aware system, and subtracted from the microsecond time point: `utc`. The variable `us` is reused to hold “microseconds since midnight”. Now it is a simple computation to split this into milliseconds since midnight, and microseconds since the last millisecond.
<a name="delta months"></a>
### Difference in months between two dates
(by [Howard Hinnant](https://github.com/HowardHinnant))
I recently stumbled across this Stack Overflow question:
http://stackoverflow.com/q/2536379/576911
And I decided to see if I could answer it (here) using this library. The question asks: How can I get the number of months between two dates? And it gives two example dates:
auto d1 = 1_d/oct/2013;
auto d2 = 30_d/oct/2016;
(I've converted the syntax to that of this library).
The question isn't perfectly clear since "months" is not a very precise unit. Do we want the number of "full months"? Or perhaps we should round to the nearest number of integral months? Or do we want a floating-point representation of months which can show fractional months?
These are all reasonable possibilities, and you can compute all of these things with this library. Sometimes the hardest part of a question is sufficiently refining it until you know what is really being asked.
If we want to just ignore the day-field in these dates, and then compute the number of months, that is easily done like so:
std::cout << (d2.year()/d2.month() - d1.year()/d1.month()).count() << '\n';
This creates two `year_month` objects and subtracts them. This gives a `std::chrono::duration` that represents a signed-integral number of months. The output is:
36
To include the influence of the day-fields, it is best to convert `d1` and `d2` to `day_point`s:
auto dp1 = day_point(d1);
auto dp2 = day_point(d2);
Now we could (for example) subtract the two `day_point`s, and round the result to the nearest integral month:
std::cout << round<months>(dp2-dp1).count() << '\n';
This outputs:
37
Or we could create a new `std::chrono::duration` type based on `float`, but with a period of months, and convert the difference to that:
std::cout << duration<float, months::period>(dp2-dp1).count() << '\n';
This outputs:
36.9617
These are all reasonable answers to the question, and all easily computable with this library.
*** ***
![CC BY Logo](http://mirrors.creativecommons.org/presskit/buttons/80x15/svg/by.svg) _This work is licensed under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0/)._ ![CC BY Logo](http://mirrors.creativecommons.org/presskit/buttons/80x15/svg/by.svg) _This work is licensed under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0/)._