Encapsulate std::tm in std::chrono::time_point<> and other ones

103 views
Skip to first unread message

HarD Gamer

unread,
Aug 29, 2016, 5:54:48 PM8/29/16
to ISO C++ Standard - Future Proposals

Now, if we need a textual representation of a time_point or the current hour, we have a lot of work to do. 
If we had a intuitive  time_point<...> now; .... now.sec(); now.min(); now.hour(); now.day(); now.month(); now.year(); ... [or now.tm()].
the work would be more readably and fun.
And what about a simple now.to_string()?

Howard Hinnant

unread,
Aug 29, 2016, 6:06:06 PM8/29/16
to std-pr...@isocpp.org
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);
auto lt = ineedhours3.get_local_time();
auto h = make_time(lt - floor<days>(lt)).hours();
if (h > 18h)
cout << "Let's to party!!\n";
else
cout << "The party is comming..\n";
}

?

https://github.com/HowardHinnant/date

Howard

signature.asc

José Rodrigo

unread,
Aug 29, 2016, 6:07:46 PM8/29/16
to std-pr...@isocpp.org
good man


--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/A87786B6-D6E5-4E97-A393-0E77D4583CE5%40gmail.com.

Tony V E

unread,
Aug 29, 2016, 6:14:24 PM8/29/16
to Standard Proposals
I'm still confused (although maybe it is all the 'auto's such that I don't know what is what)
see below:

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:
 
        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>...
 
        if (h > 18h)
            cout << "Let's to party!!\n";
        else
            cout << "The party is comming..\n";
    }

?

https://github.com/HowardHinnant/date

Howard



--
Be seeing you,
Tony

Howard Hinnant

unread,
Aug 29, 2016, 6:29:19 PM8/29/16
to std-pr...@isocpp.org
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.
signature.asc

Howard Hinnant

unread,
Aug 29, 2016, 8:05:25 PM8/29/16
to std-pr...@isocpp.org
On Aug 29, 2016, at 6:29 PM, Howard Hinnant <howard....@gmail.com> wrote:
>
>>> 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>…
>
> 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.

On further reflection, make_time() really is overkill for this application since the minutes and seconds are discarded. The following is both simpler and slightly more efficient:

auto h = floor<chrono::hours>(lt - floor<days>(lt));

Howard

signature.asc
Reply all
Reply to author
Forward
0 new messages