[英]Which boost class should I use to store a human age
I have to store an age (years, months, days....possibly hours, minutes, seconds) of the user. 我必须存储用户的年龄(年,月,日......可能是小时,分钟,秒)。 I'm working with C++ and boost. 我正在使用C ++和boost。
I'm not sure wich class of boost::posix_time
(or boost::date_time
) I should use. 我不确定我应该使用的boost::posix_time
(或boost::date_time
)类。
I tried boost::posix_time::time_duration
, but it's not obvious because there is no constructor taking a year count, it's only hours, so I did: 我尝试过boost::posix_time::time_duration
,但这并不明显,因为没有构造函数需要一年计算,它只有几个小时,所以我做了:
boost::posix_time::time_duration age = boost::posix_time::hours(24*365*ageInYears);
But I'm not sure that's a good strategy because all years does not have 365 days ;-) 但我不确定这是一个好策略,因为所有年份都没有365天;-)
I also tried boost::gregorian::date
, but that's tricky because this one does not allow to store a year
before 1400 (and this stores a date, not a duration). 我也尝试过boost::gregorian::date
,但这很棘手,因为这个版本不允许在1400之前存储year
(这会存储一个日期,而不是持续时间)。
int
because it's not accurate enough (24 years old + 11 months is almost 25). 我不想存储常规int
因为它不够准确(24岁+ 11个月差不多25)。 float
because I don't want to reinvent the wheel with float to age conversion I would have to do... 我不想存储一个float
因为我不想重新发明轮子与浮动到年龄转换我将不得不做... Is there really no class making it easy to store a number of years and optionally a number of month and days in boost? 真的没有课程可以很容易地存储多年,并且可选择一些月份和日期来提升吗?
Ideally, for a guy of 30 years old and a half, I'd like to be able to create an object like that: boost::....... theAge( 30, 6, 0 );
理想情况下,对于一个30岁半的人,我希望能够创建一个这样的对象: boost::....... theAge( 30, 6, 0 );
and then: 接着:
boost::posix_time::time_duration
really is one way to do this properly. boost::posix_time::time_duration
确实是一种正确的方法。 Another way (which I personally would prefer) is to store the birth date and the "as-of date" both, and subtract them when you need to find the age as-of that date. 另一种方式(我个人更喜欢)是存储出生日期和“截止日期”两者,并在需要查找该日期的年龄时减去它们。
In any case you don't need a constructor taking a number of years--you can simply subtract birth_date from today--if you do that using date_time objects, you'll get a time_duration. 在任何情况下,您都不需要花费数年的构造函数 - 您可以简单地从今天减去birth_date - 如果您使用date_time对象执行此操作,您将获得time_duration。
There are indeed duration types in boost::gregorian
, specifically: boost::gregorian
中确实存在持续时间类型,具体为:
boost::gregorian::date_duration
(aka boost::gregorian::days
) - a count of days boost::gregorian::date_duration
(又名boost::gregorian::days
) - 天数 boost::gregorian::months
- a count of calendar months boost::gregorian::months
- 日历月数 boost::gregorian::years
- a count of calendar years boost::gregorian::years
- 日历年数 boost::gregorian::weeks
- a count of 7 days boost::gregorian::weeks
- 7天的计数 These would be ideal for storage ie store a tuple of (years, months, days). 这些将是存储的理想选择,即存储(年,月,日)元组。
Note though that arithmetic using in particular months
and years
can have unexpected results, as they provide a snap-to-end-of-month behavior: 请注意,虽然使用特定months
和years
可能会产生意外结果,因为它们提供了一个月末的行为:
months single(1); // 1 month duration
date(2005,Feb,28) + single; // => 2005-Mar-31
Edit from OP owner: There's actually a an existing boost struct to store year/month/day objects ( boost::date_time::date_time::year_month_day_base
). 从OP所有者编辑:实际上存在一个用于存储年/月/日对象的boost结构( boost::date_time::date_time::year_month_day_base
)。
Here is an implementation perfect to answer the OP: 这是一个完美的回答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
然后, age(30).years() == 30
和age(30,6,8).getAsFloat() == 30.521902
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.