Convert duration to years?

biozic via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jan 15 03:01:28 PST 2017


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...



More information about the Digitalmars-d-learn mailing list