Arithmetic and comparison with ctime library data type time_t

520 views Asked by At

I'm trying to implement my own little alarm clock desktop app in C++ with the ctime library. Seemed easy at first, but I've had several unexpected problems, that have disappeared just as unexpectedly. Now I'm left with my time_t variable timeAlarm not being initialized properly. Since time_t is like a timestamp that represents the seconds passed since 00:00:00 1.1.1970 and is declared as long (signed integer type) in the ctime library, I initialize timeAlarm to be 22 o'clock (10pm) as (time_t)(22*60*60).

However, when I set the static window's text with SetWindowText() to timeAlarm via strftime(), it always outputs "01:00:00". The "remaining time" has next midnight as zero time.

So do I initialize timeAlarm incorrectly or do I use difftime for the wrong purpose? What could be going wrong?

The following is a picture of what the app shows:

App client area with the static windows

The following is all the code.

#include <windows.h>
#include <ctime>
#include <string>
#include "resources.h"

HWND hwndMain;
HWND hwndTimeNow;
    time_t timeNow;
HWND hwndTimeAlarm;
    time_t timeAlarm;
HWND hwndTimeRemaining;
    time_t timeRemaining;
UINT_PTR timerID;

// Converts time_t to std::string
std::string time_tToStr(time_t * ptr){
    char timeString[16];
    strftime(timeString,sizeof(timeString),"%X",localtime(ptr));
    return timeString;
};

// Sets window text to time string from time_t value
void time_tToWindowText(HWND hwnd, time_t * ptr){
    SetWindowText(hwnd,time_tToStr(ptr).c_str());
};

LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
    switch(Message) {
        case WM_CREATE: {
            // Present time
            CreateWindow("STATIC", "Zeit", WS_CHILD | WS_VISIBLE, 10, 10, 100, 15, hwnd, NULL, NULL, NULL);
            hwndTimeNow = CreateWindow("STATIC", NULL, WS_CHILD | WS_VISIBLE, 120, 10, 200, 15, hwnd, NULL, NULL, NULL);
            // Alarm time
            CreateWindow("STATIC", "Alarmzeit", WS_CHILD | WS_VISIBLE, 10, 35, 100, 15, hwnd, NULL, NULL, NULL);
            hwndTimeAlarm = CreateWindow("STATIC", NULL, WS_CHILD | WS_VISIBLE, 120, 35, 200, 15, hwnd, NULL, NULL, NULL);
            time_tToWindowText(hwndTimeAlarm,&timeAlarm);
            // Remaining time
            CreateWindow("STATIC", "Countdown", WS_CHILD | WS_VISIBLE, 10, 60, 100, 15, hwnd, NULL, NULL, NULL);
            hwndTimeRemaining = CreateWindow("STATIC", NULL, WS_CHILD | WS_VISIBLE, 120, 60, 200, 15, hwnd, NULL, NULL, NULL);
            break;
        }
        case WM_TIMER: { // Runs once a second

            timeNow = time(NULL);
            timeNow = (time_t)(timeNow%(60*60*24)); // Get only daytime
            time_tToWindowText(hwndTimeNow,&timeNow);

            timeRemaining = (time_t)difftime(timeAlarm, timeNow);
            time_tToWindowText(hwndTimeRemaining,&timeRemaining);

            if (timeRemaining <= 0){
                int ans = MessageBox(hwndMain, "22:00:00 - Alarm time reached.", "Alarm", MB_OK|MB_ICONINFORMATION);
                PostQuitMessage(0);
                break;
            }
            else
                timerID = SetTimer(hwndMain, 0, 1000, NULL);

            break;
        }
        case WM_DESTROY: {
            KillTimer(hwnd,timerID);
            PostQuitMessage(0);
            break;
        }
        default:
            return DefWindowProc(hwnd, Message, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    WNDCLASSEX wc;
    MSG Msg;

    memset(&wc,0,sizeof(wc));
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.lpfnWndProc   = WndProc;
    wc.hInstance     = hInstance;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);

    wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
    wc.lpszClassName = "WindowClass";
    wc.hIcon         = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MAINICON));
    wc.hIconSm       = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MAINICON), IMAGE_ICON, 16, 16, 0);

    if (!RegisterClassEx(&wc)) {
        MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
        return 0;
    }

    hwndMain = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Heia",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        350,
        150,
        NULL,NULL,hInstance,NULL);

    if (hwndMain == NULL) {
        MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
        return 0;
    }

    timeAlarm = (time_t)(22*60*60); // Set alarm time to 22 o'clock (10 pm)
    PostMessage(hwndMain, WM_TIMER,0,0); // Kick off timer loop

    while (GetMessage(&Msg, NULL, 0, 0) > 0) { 
        TranslateMessage(&Msg); 
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}
1

There are 1 answers

0
Mark Ransom On BEST ANSWER

localtime applies a time zone correction, assuming the time you feed it is in UTC. You're in a UTC+1 time zone, so it adds one hour.

The current time is OK because time also works in UTC. In your example the UTC time should be 17:11:00.

The time shown for the alarm is wrong because you don't initialize it to 22:00 until after the text is set in the WM_CREATE handler. Evidently it initialized to 0, which is 01:00 when you add an hour.

The difference between 22:00:00 and 17:11:00 is 4:49:00. Add one hour for timezone adjustment and it's 5:59.

If you don't want the timezone adjustment applied at each conversion you should use gmtime instead of localtime.