]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/date_time/example/gregorian/month_add.cpp
1 /* Simple program that uses the gregorian calendar to progress by exactly
2 * one month, regardless of how many days are in that month.
4 * This method can be used as an alternative to iterators
7 #include "boost/date_time/gregorian/gregorian.hpp"
14 using namespace boost::gregorian
;
16 date d
= day_clock::local_day();
17 date d2
= d
+ months(1);
18 date d3
= d
- months(1);
19 std::cout
<< "Today is: " << to_simple_string(d
) << ".\n"
20 << "One month from today will be: " << to_simple_string(d2
) << ".\n"
21 << "One month ago was: " << to_simple_string(d3
)
23 std::cout
<< "******** Warning read this ***********************\n";
24 std::cout
<< "Be aware that adding a month is not exactly like regular numeric math.\n"
25 << "Addition/Subtraction of months will 'snap to the end' of a month that\n"
26 << "is shorter than the current month. For example consider "
27 << "Jan 31, 2004 + (1 month)\n";
28 date
d4(2004, Jan
, 31);
29 date d5
= d4
+ months(1);
30 std::cout
<< "Result is: " << to_simple_string(d5
)
33 std::cout
<< "\nSo what does this mean? It means the result of adding months is order\n"
34 << "dependent, non-commutative, and may create problems for applications.\n"
36 << "Jan 30, 2004 + (1 month) + (1 month) != Jan 30, 2004 + (2 months)\n"
37 << "Why not? Because Jan 30, 2004 + 1 month is Feb 29 + 1 month is Mar 31st.\n"
38 << "while Jan 30, 2004 + 2 months is Mar 30th.\n"
39 << "All of this clears up as long as all the starting dates before the 28th of\n"
40 << "the month -- then all the behavior follows classical mathematical rules.\n";
42 date
d6(2004, Jan
, 30);
43 date d7
= d6
+ months(1) + months(1);
44 date d8
= d6
+ months(2);
45 std::cout
<< "2004-01-30 + (1 month) + (1 month) is: " << to_simple_string(d7
) << ".\n"
46 << "2004-01-30 + (2 months) is: " << to_simple_string(d8
)
52 /* Copyright 2001-2005: CrystalClear Software, Inc
53 * http://www.crystalclearsoftware.com
55 * Subject to the Boost Software License, Version 1.0.
56 * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)