mirror of
https://github.com/HowardHinnant/date.git
synced 2025-08-05 21:54:27 +02:00
Updated Examples and Recipes (markdown)
@@ -261,7 +261,7 @@ to_tm(date::zoned_seconds tp)
|
||||
t.tm_mon = (ymd.month() - January).count();
|
||||
t.tm_year = (ymd.year() - 1900_y).count();
|
||||
t.tm_wday = (weekday{ld} - Sunday).count();
|
||||
t.tm_yday = (ld - local_days{ymd.year()/jan/1}).count();
|
||||
t.tm_yday = (ld - local_days{ymd.year()/January/1}).count();
|
||||
t.tm_isdst = tp.get_info().save != minutes{0};
|
||||
return t;
|
||||
}
|
||||
@@ -330,7 +330,7 @@ to_nanoseconds(timespec const& ts)
|
||||
|
||||
One just converts the integrals to their proper `duration` types and adds them. The result has type `nanoseconds`.
|
||||
|
||||
We can resume the above the above function to convert to a `time_point`:
|
||||
We can reuse the above the above function to convert to a `time_point`:
|
||||
|
||||
```c++
|
||||
date::sys_time<std::chrono::nanoseconds>
|
||||
@@ -412,7 +412,7 @@ since_local_midnight(std::chrono::system_clock::time_point t, const date::time_z
|
||||
{
|
||||
using namespace date;
|
||||
using namespace std::chrono;
|
||||
auto zt = make_zoned(zone, t);
|
||||
zoned_time zt{zone, t};
|
||||
zt = floor<days>(zt.get_local_time());
|
||||
return floor<milliseconds>(t - zt.get_sys_time());
|
||||
}
|
||||
@@ -440,14 +440,14 @@ since_local_midnight(const date::zoned_seconds& zt)
|
||||
So to output the current time in milliseconds since the local midnight, you would just:
|
||||
|
||||
```c++
|
||||
std::cout << since_local_midnight().count() << "ms\n";
|
||||
std::cout << since_local_midnight() << '\n';
|
||||
```
|
||||
|
||||
To ensure that our function is working, it is worthwhile to output a few example dates. This is most easily done by specifying a time zone (I'll use "America/New_York"), and some local date/times where I know the right answer:
|
||||
|
||||
```c++
|
||||
auto zt = make_zoned(locate_zone("America/New_York"), local_days{jan/15/2016} + 3h);
|
||||
std::cout << zt << " is " << since_local_midnight(zt).count() << "ms after midnight\n";
|
||||
zoned_time zt{locate_zone("America/New_York"), local_days{January/15/2016} + 3h};
|
||||
std::cout << zt << " is " << since_local_midnight(zt) << " after midnight\n";
|
||||
```
|
||||
|
||||
This 3am in the middle of the Winter. This outputs:
|
||||
@@ -459,8 +459,8 @@ which is correct (10800000ms == 3h).
|
||||
I can run the test again just by assigning a new local time to `zt`. The following is 3am just after the "spring forward" daylight saving transition (2nd Sunday in March):
|
||||
|
||||
```c++
|
||||
zt = local_days{sun[2]/mar/2016} + 3h;
|
||||
std::cout << zt << " is " << since_local_midnight(zt).count() << "ms after midnight\n";
|
||||
zt = local_days{Sunday[2]/March/2016} + 3h;
|
||||
std::cout << zt << " is " << since_local_midnight(zt) << " after midnight\n";
|
||||
```
|
||||
|
||||
This outputs:
|
||||
@@ -472,16 +472,16 @@ Because the local time from 2am to 3am was skipped, this correctly outputs 2 hou
|
||||
An example from the middle of Summer gets us back to 3 hours after midnight:
|
||||
|
||||
```c++
|
||||
zt = local_days{jul/15/2016} + 3h;
|
||||
std::cout << zt << " is " << since_local_midnight(zt).count() << "ms after midnight\n";
|
||||
zt = local_days{July/15/2016} + 3h;
|
||||
std::cout << zt << " is " << since_local_midnight(zt) << " after midnight\n";
|
||||
```
|
||||
2016-07-15 03:00:00 EDT is 10800000ms after midnight
|
||||
|
||||
And finally an example just after the Fall transition from daylight saving back to standard gives us 4 hours:
|
||||
|
||||
```c++
|
||||
zt = local_days{sun[1]/nov/2016} + 3h;
|
||||
std::cout << zt << " is " << since_local_midnight(zt).count() << "ms after midnight\n";
|
||||
zt = local_days{Sunday[1]/November/2016} + 3h;
|
||||
std::cout << zt << " is " << since_local_midnight(zt) << " after midnight\n";
|
||||
```
|
||||
|
||||
2016-11-06 03:00:00 EST is 14400000ms after midnight
|
||||
|
Reference in New Issue
Block a user