January 15, 2017
On Sunday, 15 January 2017 at 14:04:39 UTC, Nestor wrote:
> ...
> 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?
>

On second thought, if a baby was born in march 1 of 1999 (non-leap year), in march 1 of 2000 (leap year) the age would have been one year plus one day (because of february 29). So perhaps the best thing is to always perform a "relaxed" calculation.


January 15, 2017
On Sunday, 15 January 2017 at 14:20:04 UTC, Nestor wrote:
> On Sunday, 15 January 2017 at 14:04:39 UTC, Nestor wrote:
>> ...
>> 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?
>>
>
> On second thought, if a baby was born in march 1 of 1999 (non-leap year), in march 1 of 2000 (leap year) the age would have been one year plus one day (because of february 29).

No. A baby born on March 1st 1999 is just "one year old" on March 1st 2000, as it also is on March 2nd or any day after during the same year.

> So perhaps the best thing is to always perform a "relaxed" calculation.

I guess the problem of people born on February 29th is really application-dependent, and it also depends on the use of the calculated age. A social web app: users probably would like to see their age change on the 28th of non-leap years. A regulation-aware software: just follow what the law says. Etc.



January 15, 2017
On Sunday, 15 January 2017 at 16:57:35 UTC, biozic wrote:
> On Sunday, 15 January 2017 at 14:20:04 UTC, Nestor wrote:
>> On second thought, if a baby was born in march 1 of 1999 (non-leap year), in march 1 of 2000 (leap year) the age would have been one year plus one day (because of february 29).
>
> No. A baby born on March 1st 1999 is just "one year old" on March 1st 2000, as it also is on March 2nd or any day after during the same year.
>

Perhaps I didn't make myself clear. I was not refering here to age in the conventional sense, but to the actual aging process. In other words, in this particular case the amount of days elapsed would have been 366 instead of 365.
January 15, 2017
On Sunday, January 15, 2017 03:43:32 Nestor via Digitalmars-d-learn wrote:
> Hi,
>
> I would simply like to get someone's age, but I am a little lost with time and date functions. I can already get the duration, but after reading the documentation it's unclear to me how to convert that into years. See following code:
>
> import std.stdio;
>
> void getAge(int yyyy, int mm, int dd) {
>    import std.datetime;
>    SysTime t1 = SysTime(Date(yyyy, mm, dd));
>    SysTime t2 = Clock.currTime();
>    writeln(t2 - t1);
> }
>
> int main() {
>    try
>      getAge(1980, 1, 1);
>    catch(Exception e) {
>      writefln("%s.\n(%s, line %s)", e.msg, e.file, e.line);
>    }
> }
>
> Notice getAge should return ubyte instead of void, only I haven't been able to find how to do it. Any suggestion would be welcome.
>
> Thanks in advance.

Well, there's diffMonths:

http://dlang.org/phobos/std_datetime.html#.SysTime.diffMonths

However, I doubt that it really does quite what you want. Because of the varying lengths of months and years, you're probably going to have to write code that does what you want with some combination of function. You probably want to do something like

void getAge(int yyyy, int mm, int dd)
{
    auto birthdate = Date(yyyy, mm, dd);
    auto currDate = cast(Date)Clock.currTime;
    // This make Feb 29th become March 1st
    auto birthdayThisYear = Date(currDate.year, mm, 1) + days(dd - 1);
    auto years = currDate.year - birthdate.year;
    if(currDate < birthdayThisYear)
        --years;
    writeln(years);
}

I _think_ that that does it, but I'd want to do something like

void printAge(int yyyy, int mm, int dd)
{
    writeln(getAge(cast(Date)Clock.currTime(), yyyy, mm, dd);
}

int getAge(Date currDate, int yyyy, int mm, int dd)
{
    auto birthdate = Date(yyyy, mm, dd);
    auto currDate = cast(Date)Clock.currTime;
    // This make Feb 29th become March 1st
    auto birthdayThisYear = Date(currDate.year, mm, 1) + days(dd - 1);
    auto years = currDate.year - birthdate.year;
    if(currDate < birthdayThisYear)
        --years;
    return years;
}

and then add unit tests for getAge to verify that it did the correct thing for various dates. It's quite possible that there's something subtley wrong with it. Also, depending on what exactly you're trying to do, it's possible that I didn't quite understand what you're trying to do and that it needs some additional tweaks in order to do what you want.

- Jonathan M Davis

January 15, 2017
Thank you all.
1 2
Next ›   Last »