Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CTime question 2

Status
Not open for further replies.

MinnisotaFreezing

Programmer
Jun 21, 2001
120
KR
Hello, code snippit follows

CTime time = CTime::GetCurrentTime();
month = time.GetMonth()+2;
year = time.GetYear();
day = time.GetDay();
CTime time2(year, month, day, 0, 0, 0);

That gets a CTime object that = 2 months from now. But look, I use 2 CTime objects. Can anyone suggest something else? Maybe

CTime * newtime = new CTime();
newtime->GetCurrentTime();
month = newtime->GetMonth()+2;
year = newtime->GetYear();
day = newtime->GetDay();
newtime = new CTime(year, month, day, 0,0,0);

The only problem there is GetCurrentTime returns a CTime object, not a pointer to CTime, so I have no way of getting newtime to point to a CTime object with the current time in it. Unless we go this way, which is better. Rather than 2 CTime objects we have an object and a pointer.

CTime time1 = CTime::GetCurrentTime();
month = time1.GetMonth()+2;
year = time1.GetYear();
day = time1.GetDay();
CTime * newtime = new CTime(year, month, day, 0,0,0);

Can anyone see a way to do this either with only a pointer or only on CTime object?

Yea, I guess I have a little time on my hands. Also, this is just example code, it would fail if it were Nov or Dec.

CJB
 
Being that you see the Nov/Dec problem, the way to do it with one CTime would be to just reuse your current CTime. I believe the line

CTime t = CTime::GetCurrentTime();
int month = t.GetMonth();
t= CTime(t.GetYear(),
(t.GetMonth()>10 ? t.GetMonth()%10:t.GetMonth()+2)
t.GetDay(),
t.GetMin(),
t.GetSec());

I think that will do the trick for you. Not 100% sure though, havent tested it.

Matt
 
How about this:

SYSTEMTIME aTime;
::GetSystemTime(&aTime);
aTime.wMonth +=2;
CTime time(aTime);
 
Thanks guys, just what I was looking for. Not at my compiler so I can't test them, but I bet they will do what I wan't and so much simpler as well.

CJb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top