简体   繁体   English

我应该使用哪种助推器来存储人类年龄

[英]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 (这会存储一个日期,而不是持续时间)。

  • I don't want to store user date of birth because I need to store its age when my program ran (medical data). 我不想存储用户的出生日期,因为我需要在我的程序运行时存储它的年龄(医疗数据)。
  • I don't want to store a regular int because it's not accurate enough (24 years old + 11 months is almost 25). 我不想存储常规int因为它不够准确(24岁+ 11个月差不多25)。
  • I don't want to store a 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: 接着:

  • Have a function to get age in years: theAge.years() returning 30 (ignoring months) 有一个年龄的功能:theAge.years()返回30(忽略几个月)
  • Possibly have a conversion to float that would give me 30.5 as an age 可能有一个转换浮动,这将给我30.5作为一个年龄

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中确实存在持续时间类型,具体为:

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: 请注意,虽然使用特定monthsyears可能会产生意外结果,因为它们提供了一个月末的行为:

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() == 30age(30,6,8).getAsFloat() == 30.521902

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该使用哪个库才能使用Boost算法进行编译 - which library should i use in order to compile with boost algorithm 我应该在具有不同类型参数的类中使用Boost Variant - Should I use Boost Variant for class with different type parameters 我应该使用boost :: variant吗? - Should I use boost::variant? 我怎么知道我应该使用哪个C ++标准版本来构建哪个版本的Boost? - How do I know which C++ standard version I should use to build which version of Boost? 我可以告诉Boost.MPI哪个类版本与Boost.Serialization一起使用? - Can I tell Boost.MPI which class version to use with Boost.Serialization? 我应该使用哪个数据库来存储记录,我应该如何使用它? - Which database should I use to store records, and how should I use it? 我应该在支持C ++ 03的编译器中使用哪个Boost版本? - Which Boost version should I use with a C++03-capable compiler? 我应该在c ++ visual-studio-2005中使用哪个版本的boost? - Which version of boost should I use with c++ visual-studio-2005? 我应该使用哪些编译选项来使用cl.exe与Boost链接? - Which compilation options should I use to link with Boost using cl.exe? boost :: asio :: steady_timer()vs sleep()我应该使用哪一个? - boost::asio::steady_timer() vs sleep() which one should I use?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM