I'm fighting a few days with COleDateTime
in MFC.
I have CTime
with correct values. Correct years, days, months, hours, minutes and seconds.
I tried a few ways to convert CTime
to COleDateTime
:
-1.I put CTim
e data to constructor of COleDateTime
COleDateTime(int nYear,int nMonth,int nDay, int nHour, int nMin,int nSec );
-2. I formatted CTime
to time.Format("%m/%d/%y %H:%M:%S");
and passed to ParseDateTime
of COleDateTime
.
-3. Also I tried to use SetDateTime
of COleDateTime
After that I'm getting incorrect values of minutes 1-2 min. more or less. I have never seen it before and I couldn't find nothing in internet.Everybody says abot loss precision but this a second, not a minute. Please advice something for me! Thank you
I think the problem is that COleDateTime internally uses a float for storage, and the value represents the number of days since 30 December 1899.
As the number of days gets larger, the precision of the smaller fields (like minutes) decreases. For example, a float can accurately store the values 1000000 and 0.0000001, but it CAN'T store 1000000.0000001. It doesn't have enough bits of precision.
This limitation is hinted at in the MSDN documentation:
This type is also used to represent date-only or time-only values. By convention, the date 0 (30 December 1899) is used for time-only values. Similarly, the time 0:00 (midnight) is used for date-only values.
So basically, if you want a precise time, set the date to 30 December 1899.
It seems like Microsoft could have just designed this class to store the "days" portion as an integer, but hey, that would be too EASY.