What class of increase should be used to store human age

I have to store the age (years, months, days ... maybe hours, minutes, seconds) of the user. I work with C ++ and promote.

I'm not sure which class boost::posix_time (or boost::date_time ) I should use.

I tried boost::posix_time::time_duration , but this is not obvious, because there is no constructor with counting the year, it is just a clock, so I did:

 boost::posix_time::time_duration age = boost::posix_time::hours(24*365*ageInYears); 

But I'm not sure that a good strategy, because all years do not have 365 days; -)

I also tried boost::gregorian::date , but this is complicated because this one does not allow to store year to 1400 (and this saves date, not duration).

  • I do not want to store the user's date of birth, because I need to keep his age when my program was launched (medical data).
  • I do not want to store a regular int , because it is not accurate enough (24 years + 11 months - almost 25).
  • I don’t want to store a float because I don’t want to invent a wheel with a float to the age that I would need to do ...

Is there really no class that allows you to easily store several years and, possibly, the number of months and days in boost?

Ideally, for a guy aged 30 or older, I would like to create such an object: boost::....... theAge( 30, 6, 0 ); , and then:

  • You have a function to get the age in years: theAge.years () returns 30 (ignoring the months)
  • Maybe there is a conversion to float that would give me 30.5 as an age
+6
source share
2 answers

There are indeed duration types in boost::gregorian , in particular:

They are ideal for storage, i.e. keep a tuple (years, months, days).

Please note that although arithmetic, using months and years in particular, may have unexpected results, as they provide a binding at the end of the month:

 months single(1); // 1 month duration date(2005,Feb,28) + single; // => 2005-Mar-31 

Change owner OP: Actually, there is an existing boost structure for storing year / month / day objects ( boost::date_time::date_time::year_month_day_base ).

Here is an implementation perfect for answering an OP:

 class age : public date_time::year_month_day_base<years, months, days> { typedef date_time::year_month_day_base<years, months, days> baseClass; public: age( int yearsCount, int monthsCount = 0, int daysCount = 0 ) : baseClass( boost::gregorian::years(yearsCount), boost::gregorian::months(monthsCount), boost::gregorian::days(daysCount) ) { } inline int years() const { return year.number_of_years().as_number(); } inline int months() const { return month.number_of_months().as_number(); } inline int days() const { return day.days(); } float getAsFloat() const { float age = static_cast<float>(years()); age += months()/12.0f; age += days()/365.25f; return age; } }; 

Then age(30).years() == 30 and age(30,6,8).getAsFloat() == 30.521902

+4
source

boost::posix_time::time_duration really is one way to do it right. Another way (which I personally would prefer) is to save the date of birth and “as a date” both, and subtract them when you need to find the age as of that date.

In any case, you do not need a constructor that takes several years - you can just subtract birth_date from today - if you do this using date_time objects, you will get time_duration.

+5
source

All Articles