Link to home
Start Free TrialLog in
Avatar of bfeddish
bfeddishFlag for United States of America

asked on

Adding a month to a current date


I have a CTimevar called dDepDate.  Can someone post a function to add 1 month (not just 31 days) to this date?  I don't feel like re-inventing the wheel.

Thanks,
Bryan
Avatar of Salte
Salte

Depends on what you mean by a 'month'. If you mean that from 15th of january you should get 15th of february?

so far so good...what about 31st of march? Should that be 31st of april? err.... let's rethink this a bit..

Your request is reasonable enough, however, the specification isn't complete. You need to decide what to do with dates when the next month doesn't have the date in question. What about 29th of january? Should it be 29th of february? That might work if the year is a leapyear, otherwise it won't.

One reasonable approach is to then always move it to the 'last day of the next month'. The question is then what to do when you want to add one more month:

30 january + 1 month == 30 february (too much) == 28/29th february.

Next month you get:

28/29th february + 1 month == ???

should it be 28/29th of march or should it be 30th of march (the original date)?

There are good reasons for selecting either choice and good reasons to avoid either, so which one you choose here is neither obvious nor "one solution fits all" it is very situation dependent. So the question is: why do you want this function? If you could tell us the reason we might be able to figure out which one is reasonable.

So, before we give you any algorithm I want you think over these issues and tell us which policy you want to select.

Simply saying you want a function that gets you next month is extremely poor specification.

Alf
ASKER CERTIFIED SOLUTION
Avatar of Salte
Salte

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bfeddish

ASKER


(Points upped to 250 due to my laziness..)

 k = *localtime(from);

Gives me the error: error C2664: 'localtime' : cannot convert parameter 1 from 'long' to 'const long *'.

Also, how do I covert a CTime var to a time_t structure and back again.
Avatar of Kent Olsen

k = localtime (from);

Should get you a lot closer.  :)

The "*" dereferences the returned result which makes it look like a "const long *".


Kdo

k = localtime (from);

Should get you a lot closer.  :)

The "*" dereferences the returned result which makes it look like a "const long *".


Kdo
Oh, if you have a CTime variable I guess you are using MFC.

I assumed you used the standard C and C++ time type of time_t.

If you have a CTime variable I believe it is more or less immediate as the CTime variable already have methods to get the day of the month, month of the year and the year.

IF it doesn't then you must convert it to some type that does. The idea is simple enough, just get the date broken down into day, month and year then add the month and put them together to a new time variable.

Some special logic for day in month >= 28 since that is assumed to be 'last day of the month'.

Note, if you use non-standard ways of representing time - such as CTime or System::DateTime (.NET) or SYSTEMTIME (Win32) etc it is a good idea to say so when you ask. If you just ask - people will assume you use standard time type which is time_t.

Alf
oops, maybe I should read the question better.... You do say that you have a CTime variable.

Oh well, in any case, I am not sure about the details of that class - I don't have the MFC documentation available here on this Linux machine but I assume it has methods to get the day, month and year, you just get those, add the month, check for the day in month being >= 28 and if so set it to the last day of the month.

The last day is given in the table I originally gave, make sure that you index the table with january == 0, february == 1 etc.

For february you have to make a special test for leapyear. If the year can be 1700, 1800, 1900, 2100, 2200 or 2300 or something like that then you also have to take into account that century years are not leapyears unless they are divisible by 400.

Alf

I just translated your function to use the CTime struct.