On Aug 29, 2016, at 6:14 PM, Tony V E <
tvan...@gmail.com> wrote:
>
> I'm still confused (although maybe it is all the 'auto's such that I don't know what is what)
> see below:
Fortunately there are docs:
https://howardhinnant.github.io/date/tz.html
https://howardhinnant.github.io/date/date.html
(not that you should’ve run out and immediately read and understood all that, just giving handy links)
>
>> On Mon, Aug 29, 2016 at 6:05 PM, Howard Hinnant <
howard....@gmail.com> wrote:
>>
>> How do you feel about this:
>>
>> #include "tz.h"
>> #include <iostream>
>>
>> int
>> main()
>> {
>> using namespace date;
>> using namespace std;
>> using namespace std::chrono_literals;
>> auto ineedhours3 = make_zoned(current_zone(),
>> floor<chrono::seconds>(chrono::system_clock::now()));
>> cout << "The time is " << format("%a %b %d %T %Y\n", ineedhours3);
>>
> OK, it seems like ineedhours3 is some type of timey-wimey thing that has what I need (ie hours).
> Why do I need these next two lines:
The type of ineedhours3 is a a zoned_time<seconds>, also known as zoned_seconds for short.
https://howardhinnant.github.io/date/tz.html#zoned_time
A zoned_time is a pairing of a time_zone and local_time. Or you can equivalently think of it as a pairing of a time_zone and a sys_time. Either way, it represents a well-specified time in some time zone. From it you can get the local time, the UTC time and the time zone. And you can also format it (including %Z (time zone abbreviation) and %z (time zone offset).
>
>> auto lt = ineedhours3.get_local_time();
>> auto h = make_time(lt - floor<days>(lt)).hours();
>>
> In particular, make_time()? Ain't nobody got time for that. Didn't I already have a time thing? It also looks like magic. floor<days>…
lt has type local_time<seconds>, or local_seconds for short. local_time is a std::chrono::time_point that is not associated with _any_ time zone. One can think of it as the void* of chrono::time_points.
floor<Duration>(time_point) is actually already in the C++1z working draft. It takes a time_point of a finer duration and truncates it to a time_point of a coarser duration. In this case the finer duration is seconds, and the coarser duration is days. days is a chrono::duration<int, ratio<86400>>.
The expression “lt - floor<days>(lt)” converts a time_point since whatever epoch into a chrono::duration of the time since midnight. Since lt has type local_time, as opposed to being a system_clock::time_point, this is with respect to local midnight, as opposed to midnight UTC. We could have just as easily gotten time since UTC midnight by extracting ineedhours3.get_sys_time(), resulting in a system_clock::time_point with seconds precision, or sys_seconds for short.
make_time takes a chrono::duration and breaks it down into a {hours, minutes, seconds} struct. It has a .hours() getter to get the hours. The type of that result is chrono::hours.