Thread: Strange behavior with CDateTimeCtrl

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    12

    Strange behavior with CDateTimeCtrl

    I'm using MSVC++ 6 in Windows XP Pro SP2, and trying to write a simple dialog with a date/time picker control, and am experiencing some strange behavior; when I try to UpdateData() after entering a time in the control, the last assertion in the following CTime constructor fails, even though all input fields appear valid:
    Code:
    CTime::CTime(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, int nDST)
    In particular, this assertion fails for any time earlier than 6 PM. I suspected that my time zone setting might be influencing the problem, so I set my time zone to GMT, and indeed the error vanished.
    Obviously I can't insist my users set their time zone to GMT before running my program - is there something obvious I've missed? Are my MFC source files simply outdated, and if so, is a new version freely available? Thanks for your help.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Can you post the constructor code up to the assertion that failed and your call of said constructor please ?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    12
    Certainly.

    The constructor code:
    Code:
    CTime::CTime(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, int nDST)
    {
    struct tm atm;
    atm.tm_sec = nSec;
    atm.tm_min = nMin;
    atm.tm_hour = nHour;
    ASSERT(nDay >= 1 && nDay <= 31);
    atm.tm_mday = nDay;
    ASSERT(nMonth >= 1 && nMonth <= 12);
    atm.tm_mon = nMonth -1; // tm_mon is 0 based
    ASSERT(nYear >= 1900);
    atm.tm_year = nYear - 1900; //tm_year is 1900 based
    atm.tm_isdst = nDST;
    m_time = mktime(&atm);
    ASSERT(m_time != -1); //This assertion fails
    }
    The above is called from the following other CTime constructor:
    Code:
    CTime::CTime(const SYSTEMTIME& sysTime, int nDST)
    {
    if(sysTime.wYear < 1900)
    {
         time_t time0 = 0L;
         CTime timeT(time0);
         *this = timeT;
    }
    else
    {
         //This next line calls the constructor with failed assertion
         CTime timeT( (int)sysTime.wYear, (int)sysTime.wMonth, (int)sysTime.wDay, (int)sysTime.wHour, (int)sysTime.wMinute, (int)sysTime.wSecond, nDST);
         *this = timeT;
    }
    }
    which in turn is called by

    Code:
    DWORD CDateTimeCtrl::GetTime(CTime& timeDest) const
    {
         SYSTEMTIME sysTime;
         DWORD dwResult = (DWORD)
                   ::SendMessage(m_hWnd, DTM_GETSYSTEMTIME, 0, (LPARAM) &sysTime);
    
         if (dwResult == GDT_VALID)
             timeDest = CTime(sysTime); //Execution flow goes through here
    
        return dwResult;
    }
    Note that all of the above is stock MFC code; the only thing I do is call UpdateData(), which calls DoDataExchange, which calls the above.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange linking error -- can not find shared library
    By George2 in forum C Programming
    Replies: 2
    Last Post: 07-10-2006, 10:51 PM
  2. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  3. Strange response from MSVC
    By VirtualAce in forum Game Programming
    Replies: 2
    Last Post: 04-17-2004, 07:40 AM
  4. Very strange bug...
    By JaWiB in forum Tech Board
    Replies: 6
    Last Post: 04-27-2003, 01:56 PM
  5. bcc32 compiling error (really strange!!)
    By jester in forum C++ Programming
    Replies: 14
    Last Post: 01-26-2002, 04:00 PM