GetLocalTime & GetSystemTime ?

This is a discussion on GetLocalTime & GetSystemTime ? within the Windows Programming forums, part of the Platform Specific Boards category; I dont know why my test code works with GetLocalTime but doesnt with GetSystemTime; i want the program to play ...

  1. #1
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001
    Posts
    218

    GetLocalTime & GetSystemTime ?

    I dont know why my test code works with GetLocalTime but doesnt with GetSystemTime;

    i want the program to play sound if for instance, time is later than

    19pm (simplified didnt bother with min&sec) it shows the correct hour and play the sound for GetLocalTime but if i replace it with GetSystemTime ,it will show 0 and wont play sound no matter what time it is; here is the code i hope i get lucky with this one compare to my other posts.
    thx
    Code:
    case 91:
     //GetLocalTime(&st);
    			
    GetSystemTime(&st);
    			 
    if(st.wHour > 19 ){
    hdc = GetDC(hwnd);
    TextOut(hdc, 10, 0,ptext, wsprintf(ptext, "%i", st.wHour));
    ReleaseDC(hwnd, hdc);
    			 
    PlaySound (TEXT ("alarm.wav"), NULL, SND_FILENAME | SND_ASYNC);
    }
     return 0;

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Try this simple test program, if it works, then your doing something wrong somewhere else...

    Code:
    #include <windows.h>
    #include <iostream.h>
    
    int main()
    {
        SYSTEMTIME SystemTime;
    
        GetSystemTime(&SystemTime);
        cout << SystemTime.wHour << endl;
        GetLocalTime(&SystemTime);
        cout << SystemTime.wHour << endl;
        
        return 0;
    }
    ... this is what it gave me...
    Attached Images Attached Images  
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001
    Posts
    218
    thx adrian but i said in last post that it shows 2 different values for both,

    i used textout and for system it is always = 0 and for local is correct.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    As usual, I don't understand.

    >>> it shows 2 different values for both,

    What do you mean by that for example! A single call to either of the routines will fill a single SYSTEMTIME structure. Unless your local time is UTC, then I would not expect the wHour values to be the same, but there is only one wHour value in the structure.

    I certainly wouldn't dream of calling TextOut() the way you are doing it. You are assuming that the wsprintf() will set up ptext before you are using it in the same call, and that it is going to work.

    TextOut(hdc, 10, 0,ptext, wsprintf(ptext, "%i", st.wHour));

    Try this instead, it still assumes a lot, but it has got to better than what you had before.

    wsprintf(ptext, "%i", st.wHour);
    TextOut(hdc, 10, 0, ptext, lstrlen(ptext));
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001
    Posts
    218
    thx again norsman .i try to use this method when i get home
    if thats the case then no wonder about assertion failures i used to get with some of my codes that used textout like that.

    by the way shouldnt i change " i " to be a unsigned short int rather than int
    because wHour is defined as DWORD?

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    i or d, it doesn't really matter, they are equivalent.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001
    Posts
    218
    Hi ;

    i put this code as follow and added min and sec to it .
    funny thing minute and second is correct but hour is 21 when its supposed to be 16

    Code:
    case 91:
    GetSystemTime(&st);
    			 
     if(st.wHour > 14 ){
     hdc = GetDC(hwnd);
    wsprintf(ptext, "%i:%i:%i", st.wHour,st.wMinute, st.wSecond);
    TextOut(hdc, 10, 0,ptext,strlen(ptext) );
    ReleaseDC(hwnd,hdc);                                                        
     PlaySound (TEXT ("alarm.wav"), NULL, SND_FILENAME  |SND_ASYNC);
    
    			 
         }
     return 0;

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I see that adrianxw was already addressed this issue but it I see it was thoroughly ignored.

    GetSystemTime() fills a SYSTEMTIME struct with the system time in UTC format.

    GetLocalTime() fills a SYSTEMTIME struct with the system time with an adjustment made for time zone.

    I live in California and so my GetSystemTime() result be 8 hours ahead of the GetLocalTime(), and is.

  9. #9
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001
    Posts
    218
    My apology ,my ignorance caused extra work i should have noted the statment:

    "Unless your local time is UTC, then I would not expect the wHour values to be the same,"

    MORE carefully thx again.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I'm sure no one was hurt Nonetheless, msdn has documentation on api functions. Its not a bad idea to check there first when something happens that you don't expect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GetSystemTime and my current time
    By Joelito in forum Windows Programming
    Replies: 2
    Last Post: 09-20-2005, 06:57 PM
  2. GetSystemTime
    By Roaring_Tiger in forum Windows Programming
    Replies: 2
    Last Post: 04-26-2003, 12:16 AM
  3. timeGetTime(), getSystemTime(), GetTickCount()
    By Shadow12345 in forum Windows Programming
    Replies: 1
    Last Post: 11-02-2002, 09:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21