问题
This is how im generating my date i want to add 1 year to it. Thanks in advance.
char tmpbuf[128];
time_t ltime;
struct tm *today;
stringstream reD;
string todayDate;
time( <ime );
today = localtime( <ime );
strftime( tmpbuf, 128,"%Y-%m-%d_%H:%M:%S", today );
reD << tmpbuf;
reD >> todayDate;
boost::replace_all(todayDate, "_", " ");
cout << todayDate << endl;
OK ive decided to go with boost since it will be easier to add days, so 2 examples i need one to add 1 year, and one to add 14 days, heres what i have so fare
#include "boost/date_time.hpp"
#include "boost/date_time/local_time/local_time.hpp"
using namespace boost::posix_time;
using namespace boost::local_time;
int main(){
local_date_time t = local_sec_clock::local_time(time_zone_ptr());
local_time_facet* lf(new local_time_facet("%Y-%m-%d_%H:%M:%S"));
std::cout.imbue(std::locale(std::cout.getloc(), lf));
std::cout << t << std::endl;
return 0;
}
Edit putting time into string
stringstream reD;
reD.imbue(locale(reD.getloc(), lf));
reD << t;
bthis = reD.str();
cout << bthis << endl;
回答1:
I agree about using boost::date_time
, however, the solution here is quite easy.
today->tm_year++;
Although, if you happen to call localtime again, the value will be overwritten, so you should make a copy. Make today
an instance instead of a pointer, and dereference the return value of localtime like this:
today = *localtime( <ime );
You'll have to take into account certain anomalies, like incrementing a year from February 29th on a leap year.
Edit: I see you've decided to use boost::date_time
after all. This makes things much simpler. Here's how you add a year:
t += boost::gregorian::years(1);
And here's how you add 14 days:
t += boost::gregorian::days(14);
Or
t += boost::gregorian::weeks(2);
回答2:
If you're using C++, I highly recommend boost::date_time to take the leg-work out of this.
回答3:
There is no such thing as "adding a year".
Let us suppose that you go into incrementing the year by 1, after all, that is what you are aiming for.
Unfortunately, there are some inconsistencies in the way we deal with the time:
leap years: if you are on February 29th, 2008, what does adding a year mean ? February 28th, 2009 or March 1st, 2009 ? (Hint: changing the month is very confusing for the user of a calendar)
leap seconds: on June 30th or December 31th, a second or two might be added to the last minute, making that minute 61 or 62 seconds. (Hint: once again, changing the day is confusing)
special events: like the calendar re-adjustment that occurred in 1582, where Thursday, October 4, 1582 was followed by Friday, October 15, 1582, resulting in a full 10 days loss.
The problem here, is not really in "adding" a year, you can always choose to round down (preferably when end-users are involved) or up. The real problem is that if you follow this reasoning, you unfortunately lose the symmetry between adding and removing a year:
- original: February 29th, 2008
+
1 year: March 1st, 2009 (rounding up)-
1 year: March 1st, 2008
or by adding 4 years in several consecutive leaps:
- original: February 29th, 2008
+
2 years: February 28th, 2010 (rounding down)+
2 years: February 28th, 2012
Oups!
The mathematical solution to this is to simply evaluate the year duration in terms of seconds, Let's ask Wolfram about it: 3.154 × 10^7 seconds.
However, it may be quite confusing for the user.
And finally, the last solution is that whenever you make computations based on dates and duration, you save the original date away, compute the durations on their own, and then adjust the "displayed" date.
This way, you will be both mathematically correct (ie, respect symmetry and associativity) and behave intuitively for end users.
class MyTime {
...
private:
tm _origin;
tm _deviation;
};
However it is more work... so you have to decide on your scheme by yourself, depending on your application needs.
回答4:
Oh good grief, you C++ people :)
// compare June 15 2018 - 2017 to 2017 - 2016
struct tm y1_tm, y2_tm, y3_tm;
time_t y1716, y1817; // differences
y1_tm.tm_sec = 0; // 2016
y1_tm.tm_min = 0;
y1_tm.tm_hour = 0;
y1_tm.tm_mon = 6;
y1_tm.tm_mday = 15;
y1_tm.tm_year = 2016 - 1900;
y1_tm.tm_mday = 1;
y2_tm.tm_sec = 0; // 2017
y2_tm.tm_min = 0;
y2_tm.tm_hour = 0;
y2_tm.tm_mon = 6;
y2_tm.tm_mday = 15;
y2_tm.tm_year = 2017 - 1900;
y2_tm.tm_mday = 1;
y3_tm.tm_sec = 0; // 2018
y3_tm.tm_min = 0;
y3_tm.tm_hour = 0;
y3_tm.tm_mon = 6;
y3_tm.tm_mday = 15;
y3_tm.tm_year = 2018 - 1900;
y3_tm.tm_mday = 1;
y1716 = mktime(&y2_tm) - mktime(&y1_tm); // 2017 - 2016
y1817 = mktime(&y3_tm) - mktime(&y2_tm); // 2018 - 2017
Both subtractions yield 31536000 seconds. Add that to a time_t for 1 year.
来源:https://stackoverflow.com/questions/8450569/c-add-1-year-to-date