Boost logo

Boost :

Subject: Re: [boost] [gsoc 2013] chrono::date
From: Rob Stewart (robertstewart_at_[hidden])
Date: 2013-04-26 10:03:46


On Apr 25, 2013, at 11:18 PM, Howard Hinnant <howard.hinnant_at_[hidden]> wrote:

> In addition to:
>
> typedef std::chrono::duration
> <
> std::int32_t, std::ratio_multiply<std::chrono::hours::period, std::ratio<24>>
>> days;
>
> One can then, taking the current definition of the civil (gregorian) calendar, create several more units which can be quite useful:
>
> There are exactly 7 days in a week:
>
> typedef std::chrono::duration
> <
> std::int32_t, std::ratio_multiply<days::period, std::ratio<7>>
>> weeks;
>
> There are exactly 146097 days in 400 years, which could rightly be called an era:
>
> typedef std::chrono::duration
> <
> std::int32_t, std::ratio_multiply<days::period, std::ratio<146097>>
>> eras;
>
> There are exactly 400 years in an era:
>
> typedef std::chrono::duration
> <
> std::int32_t, std::ratio_divide<eras::period, std::ratio<400>>
>> years;
>
> And there are exactly 12 months in a year:
>
> typedef std::chrono::duration
> <
> std::int32_t, std::ratio_divide<years::period, std::ratio<12>>
>> months;

Nice!

> Now the definitions of years and months must be understood to be the average length, when converted to other units. But this is not so different than days being 24 hours: this is an average length of a day.

No matter how they are defined, they won't satisfy some set of users.

> No one is concerned that an average month is precisely 2,629,746 seconds long.

I'm sure there is someone who is. ;)

> However being able to say:
>
> assert(round<months>(jul/first/2013 - jan/first/2012).count() == 18);
>
> is arguably quite useful. Many financial calculations are done by the month (e.g. loan interest), even though the unit of "month" isn't as precisely defined as say "second". But with a round function things could "just work".

Even if there were different sets of definitions for different use cases, these are quite useful and interesting. One could imagine different namespaces for different sets of definitions: fin::month vs. sci::month, or whatever.

___
Rob

(Sent from my portable computation engine)


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk