Re: Convert duration to years?

2017-01-15 Thread Nestor via Digitalmars-d-learn

Thank you all.


Re: Convert duration to years?

2017-01-15 Thread Jonathan M Davis via Digitalmars-d-learn
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 , int mm, int dd) {
>import std.datetime;
>SysTime t1 = SysTime(Date(, 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 , int mm, int dd)
{
auto birthdate = Date(, 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 , int mm, int dd)
{
writeln(getAge(cast(Date)Clock.currTime(), , mm, dd);
}

int getAge(Date currDate, int , int mm, int dd)
{
auto birthdate = Date(, 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



Re: Convert duration to years?

2017-01-15 Thread Nestor via Digitalmars-d-learn

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.


Re: Convert duration to years?

2017-01-15 Thread biozic via Digitalmars-d-learn

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.






Re: Convert duration to years?

2017-01-15 Thread Nestor via Digitalmars-d-learn

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.





Re: Convert duration to years?

2017-01-15 Thread Nestor via Digitalmars-d-learn

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


Re: Convert duration to years?

2017-01-15 Thread biozic via Digitalmars-d-learn

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




Re: Convert duration to years?

2017-01-15 Thread ag0aep6g via Digitalmars-d-learn

On 01/15/2017 07:58 AM, Nestor wrote:

I eventually came up with this, but it seems an ugly hack:

import std.stdio;

uint getAge(int , ubyte mm, ubyte dd) {
  ubyte correction;
  import std.datetime;
  SysTime t = Clock.currTime();
  if (t.month < mm) correction = 1;
  else if (t.month == mm) correction = (t.day < dd) ? 1 : 0;
  else correction = 0;
  return (t.year -  - correction);
}

void main() {
  try
writefln("Edad: %s aƱos.", getAge(1958, 1, 21));
  catch(Exception e) {
writefln("%s.\n(%s, line %s)", e.msg, e.file, e.line);
  }
}


That's the better approach, I think. Years have variable lengths. 
Determining "age" in years works by comparing dates, not durations.


I would write it like this, but as far as I see yours does the same thing:


int getAge(int , int mm, int dd)
{
import std.datetime;

immutable SysTime now = Clock.currTime();
immutable int years = now.year - ;

return mm > now.month || mm == now.month && dd > now.day
? years - 1 // birthday hasn't come yet this year
: years; // birthday has already been this year
}

void main()
{
import std.stdio;

/* Day of writing: 2017-01-15 */
writeln(getAge(1980, 1, 1)); /* 37 */
writeln(getAge(1980, 1, 15)); /* 37 (birthday is today) */
writeln(getAge(1980, 1, 30)); /* 36 */
writeln(getAge(1980, 6, 1)); /* 36 */
}



Isn't there a built-in function to do this?


If there is, finding it in std.datetime would take me longer than 
writing it myself.


Re: Convert duration to years?

2017-01-15 Thread Nestor via Digitalmars-d-learn
I cleaned up the function a little, but it still feels like a 
hack:


uint getAge(uint , 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 -  - correction);
}

Isn't there anything better?


Re: Convert duration to years?

2017-01-15 Thread Nestor via Digitalmars-d-learn
On Sunday, 15 January 2017 at 07:25:26 UTC, rikki cattermole 
wrote:
So I had a go at this and found I struggled looking at "magic" 
functions and methods.

Turns out there is a much simpler answer.

int getAge(int , int mm, int dd) {
  import std.datetime;
  auto t1 = cast(DateTime)SysTime(Date(, mm, dd));
  auto t2 = cast(DateTime)Clock.currTime();

  int numYears;
  while(t2 > t1) {
 t1.add!"years"(1);
 numYears++;
  }

  return numYears;
}



Well... correct me if I am wrong, but isn't t1.add!"years"(1) 
simply adding one year to t1?


Re: Convert duration to years?

2017-01-14 Thread rikki cattermole via Digitalmars-d-learn

On 15/01/2017 4:43 PM, Nestor 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 , int mm, int dd) {
  import std.datetime;
  SysTime t1 = SysTime(Date(, 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.


So I had a go at this and found I struggled looking at "magic" functions 
and methods.

Turns out there is a much simpler answer.

int getAge(int , int mm, int dd) {
  import std.datetime;
  auto t1 = cast(DateTime)SysTime(Date(, mm, dd));
  auto t2 = cast(DateTime)Clock.currTime();

  int numYears;
  while(t2 > t1) {
 t1.add!"years"(1);
 numYears++;
  }

  return numYears;
}