AM_user AM_user - 7 days ago 5
C++ Question

Correct time sum in no UTC timezone

I am programming MFC with Visual Studio 2010 in Central Europe.

I have 2 time picker that i use to get the duration of two different jobs
and I get on my code the time setted in these control using a CTime variable.

I also have an edit control that have to display the sum of the 2 pickers previously setted.

My code is the following:

CTime TimeJob1;
CTime TimeJob2;

CTimeSpan CTSTotalJobsTime, CTSJob1, CTSJob2;

m_DTP_Job1. GetTime(TimeJob1);
m_DTP_Job2. GetTime(TimeJob2);

CTSJob1 = TimeJob1 - CTime(0);
CTSJob2 = TimeJob2 - CTime(0);

CTSTotalJobsTime = CTSJob1 + CTSJob2;

CString strTotalJobsTime = CTSTotalJobsTime.Format(_T("%H:%M:%S"));
m_EditTotalJobsTime.SetWindowText(strTotalJobsTime);


When I edit the time picker control and calc the sum of the two times, it seems like if I have an offset of 1 hour.

I try to explain in a better way.

If I set the duration of the first job to 01:00:00 and of the second job 01:00:00 the sum of the time in the edit control is 00:00:00.

If I set the duration of the first job to 02:00:00 and of the second job 02:00:00 the sum of the time in the edit control is 01:00:00.

If I set the duration of the first job to 05:00:00 and of the second job 01:00:00 the sum of the time in the edit control is 04:00:00.

If I set the duration of the first job to 02:00:00 and of the second job 04:00:00 the sum of the time in the edit control is 01:00:00.

I suppose is a problem due to the difference between my system time (UTC+1) and the UTC time of the Time(0).

Am I wrong? How can I solve this issue?

Thanks

EDIT
I initialize my time picker on OnInitDialog() in this way:

CTime timeTime(1970, 1, 2, 0, 0, 0);

m_DTP_Job1->SetTime(&timeTime);
m_DTP_Job2->SetTime(&timeTime);

Answer

THIS IS MY SOLUTION:

I tried to use CTimeSpan and the date time picker control, but i see that they don't work for me because CTimeSpan parameter are day, hours, minutes, second while i use the date time picker only for time (no days).

So when i set the hours at 00, thanks to UTF automatic conversion, my CTimeSpan change day to the previose one and set the hours to 23.

So, i decided to delete the date time picker and use 3 differnt CEdit for hours, minutes, second.