Convert duration to years?
Nestor via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Jan 15 06:04:39 PST 2017
On Sunday, 15 January 2017 at 11:01:28 UTC, biozic wrote:
> On Sunday, 15 January 2017 at 08:40:37 UTC, Nestor wrote:
>> I cleaned up the function a little, but it still feels like a
>> hack:
>>
>> uint getAge(uint yyyy, uint mm, uint dd) {
>> import std.datetime;
>> SysTime t = Clock.currTime;
>> ubyte correction = 0;
>> if(
>> (t.month < mm) ||
>> ( (t.month == mm) && (t.day < dd) )
>> ) correction += 1;
>> return (t.year - yyyy - correction);
>> }
>>
>> Isn't there anything better?
>
> It doesn't feel like a hack to me, because it's simple and
> correct code that comply with the common definition of a
> person's age. The only inaccuracy I can think of is about
> people born on February 29th...
I know. I thought about it as well, but it's not something you
can deal with cleanly.
For example, take a baby born in february 29 of year 2000 (leap
year). In february 28 of 2001 that baby was one day short to one
year.
Family can make a concession and celebrate birthdays in february
28 of non-leap years, but march 1 is the actual day when the year
of life completes. Which one to choose?
Another way to deal with this is modifying the function to take a
parameter which allows to do a relaxed calculation in non-leap
years if one so desires.
More information about the Digitalmars-d-learn
mailing list