locked
Bug: COleDateTimeSpan returns wrong GetTotalSeconds RRS feed

  • Question

  • win64 mfc development

    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
     int nRetCode = 0;

     // initialize MFC and print and error on failure
     if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
     {
      // TODO: change error code to suit your needs
      _tprintf(_T("Fatal Error: MFC initialization failed\n"));
      nRetCode = 1;
     }
     else
     {
      COleDateTime d1(2005,4,26,3,47,00);
      COleDateTime d2(2004,3,19,2,50,00);
      COleDateTimeSpan span1(d1);
      COleDateTimeSpan span2(d2);
      printf("[OleDateTimeSpan1] [Total Days=%f] [Total Minutes=%f] [Total Seconds=%f]\r\n",span1.GetTotalDays(),span1.GetTotalMinutes(),span1.GetTotalSeconds());
      printf("[OleDateTimeSpan2] [Total Days=%f] [Total Minutes=%f] [Total Seconds=%f]\r\n",span2.GetTotalDays(),span2.GetTotalMinutes(),span2.GetTotalSeconds());
     }

     return nRetCode;
    }

    output
    [OleDateTimeSpan1] [Total Days=38468.000000] [Total Minutes=55394147.000000] [Total Seconds=-2147483648.000000]
    [OleDateTimeSpan2] [Total Days=38065.000000] [Total Minutes=54813770.000000] [Total Seconds=-2147483648.000000]

    The total seconds are the same!! Is this a win64 based bug?

    Tuesday, April 26, 2005 7:58 PM

Answers

  • no.. it's a bug. All the documentation is saying is only the integer portion of the double is valid, it is always integer.000000. The same documentation applies to GetTotalDays() and GetTotalMinutes() and they return the proper values. If you cast from a double to an int the value returned by GetTotalSeconds is still wrong.
    Thursday, April 28, 2005 2:45 PM

All replies

  • no.. it's a bug. All the documentation is saying is only the integer portion of the double is valid, it is always integer.000000. The same documentation applies to GetTotalDays() and GetTotalMinutes() and they return the proper values. If you cast from a double to an int the value returned by GetTotalSeconds is still wrong.
    Thursday, April 28, 2005 2:45 PM
  • Yes. It is a bug in my program. I can confirm it.
    Tuesday, May 30, 2006 3:05 AM
  • Hello.
    I'm facing a problem that is connected to the topic discussed here.
    I need to figure out the number of seconds from 01-01-1904 to 01-01-2007.
    I used the following code:

    COleDateTimeSpan span = COleDateTime(2007, 1, 1, 0, 0, 0) - COleDateTime(1904, 1, 1, 0, 0, 0);
    double dblSeconds = span.GetTotalSeconds();

    And at the end, dblSeconds equals -2147483648.00000000 .
    This is of course incorrent (the correct value should be around 3250454400, based on the fact that GetTotalDays() returned 37621.000000).
    I understand there is some sort of overflow of the double value.
    My little workaround was making a "pit-stop" at 1950 - calculating the span twice, and then adding the results.

    My question is, isn't there a better way to do this? If the function always returns integers anyway, why couldn't it have returned an unsigned long?
    Sunday, October 21, 2007 8:53 PM