Efficient algorithm for converting number of days to years (including leap years)

后端 未结 10 2169
逝去的感伤
逝去的感伤 2021-02-05 17:04

The problem

I am writing a class for holding dates in c++, and I found the following problem:

I have a number of days N since a reference date (in

10条回答
  •  天命终不由人
    2021-02-05 17:41

    bool IsLeapYear(int year)
    {
        boost::gregorian::date d1(year, 1, 1);
        boost::gregorian::date d2 = d1 + boost::gregorian::years(1);
        boost::gregorian::date_duration diff = d2 - d1;
        return diff.days() != 365;
    }
    

提交回复
热议问题