如何使用 boost::date_time 获取两个日期之间的天数

2023-12-27

boost::date_time 中是否有可用的 API 来获取两个特定于日历的日期之间的天数?

例如,2005/01/01 和 2006/12/31 之间的天数在 7 天日历中为 730,在 5 天日历中为 504。


是的: posix_time 来救援

Live On Coliru http://coliru.stacked-crooked.com/a/467c93aec7a5e5d1

#include <boost/date_time/gregorian/greg_date.hpp>
#include <iostream>

int main() {
    using boost::gregorian::date;

    date a { 2005, 1, 1 }, b { 2006, 12, 31 };

    std::cout << (b-a).days() << "\n";
}

Prints

729 [1]

Use posix_time::ptime如果您想使用公历日期+一天中的时间(hh:mm:ss.fffffff)。local_time::local_date_time为此添加时区意识(例如,正确了解夏令时)


Trivia Incidentally this number is also the square of 27, and the cube of 9, and as a consequence of these properties, a perfect totient number, a centered octagonal number, and a Smith number. It is not the sum of four consecutive primes

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 boost::date_time 获取两个日期之间的天数 的相关文章

随机推荐