算术和与ctime库数据类型time_t的比较

时间:2014-11-15 17:26:38

标签: c++ ctime

我试图使用ctime库在C ++中实现我自己的小闹钟桌面应用程序。起初看似容易,但我有几个意想不到的问题,这些问题已经意外地消失了。现在我离开了我的time_t变量timeAlarm未正确初始化。 由于time_t就像一个时间戳,表示从00:00:00 1.1.1970开始经过的秒数,并在ctime库中声明为long(有符号整数类型),我将timeAlarm初始化为22 o& #39;时钟(晚上10点)为(time_t)(22*60*60)

但是,当我通过SetWindowText()timeAlarm的静态窗口文本设置为strftime()时,它始终会输出" 01:00:00" 。剩下的时间"下一个午夜是零时间。

因此,我是否错误地初始化timeAlarm,还是将difftime用于错误的用途?可能出现什么问题?

以下是应用显示内容的图片:

App client area with the static windows

以下是所有代码。

#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 个答案:

答案 0 :(得分:1)

localtime应用时区更正,假设您提供的时间是UTC。你在UTC + 1时区,所以它增加了一个小时。

当前时间正常,因为time也适用于UTC。在您的示例中,UTC时间应为17:11:00。

警报显示的时间是错误的,因为直到在WM_CREATE处理程序中设置文本之后才将其初始化为22:00。显然它初始化为0,即加1小时时为01:00。

22:00:00至17:11:00之间的差异是4:49:00。为时区调整添加一个小时,它是5:59。

如果您不希望在每次转化时应用时区调整,则应使用gmtime代替localtime