Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h> // Required for Windows API
- #include <string> // For std::string
- #include <chrono> // For time operations
- #include <thread> // For std::this_thread::sleep_for
- #include <ctime> // For time_t, tm, gmtime
- #include <iomanip> // For std::put_time
- // Global handles for the time labels
- HWND hTelAvivTimeLabel;
- HWND hTehranTimeLabel;
- // Window procedure function (callback for window messages)
- LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- switch (uMsg)
- {
- case WM_CREATE:
- {
- // Create static text controls (labels)
- hTelAvivTimeLabel = CreateWindowEx(
- 0, "STATIC", "Tel Aviv: Loading...",
- WS_CHILD | WS_VISIBLE | SS_CENTER,
- 10, 10, 260, 30, // x, y, width, height
- hwnd, (HMENU)1001, GetModuleHandle(NULL), NULL);
- hTehranTimeLabel = CreateWindowEx(
- 0, "STATIC", "Tehran: Loading...",
- WS_CHILD | WS_VISIBLE | SS_CENTER,
- 10, 50, 260, 30, // x, y, width, height
- hwnd, (HMENU)1002, GetModuleHandle(NULL), NULL);
- // Set font for better readability
- HFONT hFont = CreateFont(
- 24, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
- DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
- DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "SansSerif");
- SendMessage(hTelAvivTimeLabel, WM_SETFONT, (WPARAM)hFont, TRUE);
- SendMessage(hTehranTimeLabel, WM_SETFONT, (WPARAM)hFont, TRUE);
- break;
- }
- case WM_TIMER:
- {
- if (wParam == 1) // Our timer ID
- {
- // Update times
- SYSTEMTIME stLocal;
- GetLocalTime(&stLocal); // Get local time
- // Tel Aviv (GMT+3 or GMT+2 depending on DST, assuming GMT+3 for simplicity,
- // you'd need a more robust time zone library for exact DST handling)
- // For simplicity, we'll assume a fixed offset.
- // A robust solution would use a library like Howard Hinnant's date library or ICU.
- // For this example, we'll calculate approximate offsets.
- // Get current UTC time
- SYSTEMTIME stUTC;
- GetSystemTime(&stUTC);
- // Convert SYSTEMTIME to file time for easier arithmetic
- FILETIME ftUTC, ftTelAviv, ftTehran;
- SystemTimeToFileTime(&stUTC, &ftUTC);
- // Define time zone offsets in 100-nanosecond intervals (1 hour = 3600 * 10^7 = 36,000,000,000)
- // Tel Aviv: UTC+3 (during summer), UTC+2 (during winter) - Let's use a fixed +3 for this example.
- // Tehran: UTC+3:30
- // For simplicity and without a complex timezone library, we'll rely on
- // getting UTC time and applying a fixed offset.
- // The correct way would involve Windows API functions like TzSpecificLocalTimeToSystemTime
- // or a comprehensive timezone library.
- // Using standard C++ time functions for simplicity, which requires knowing the current offset from UTC.
- // A better solution for real-world applications would be to use a robust date/time library
- // that handles time zones and DST correctly (e.g., ICU, or Howard Hinnant's date library).
- // Let's approximate by getting current local time and adjusting.
- // This is a simplification and not perfectly accurate for all timezone/DST scenarios.
- // For a truly accurate solution for time zones in C++, you would need
- // to use a dedicated library like Howard Hinnant's "date" library or ICU.
- // The Windows API itself has functions for time zone conversion, but they are
- // more involved than a simple offset.
- // For this example, we'll demonstrate using `_tzset` and `_ftime_s` for basic timezone awareness,
- // but this is highly platform-specific (Microsoft Visual C++ runtime).
- // A portable C++ solution would use `std::chrono` and a timezone library.
- // Let's use `std::chrono` for basic time, but truly complex timezones are hard without a library.
- auto now = std::chrono::system_clock::now();
- // Tel Aviv: Approx UTC+3:00 (Israel Standard Time / Israel Daylight Time)
- // Tehran: Approx UTC+3:30 (Iran Standard Time / Iran Daylight Time)
- // The following time zone calculations are *approximations* and
- // do not account for Daylight Saving Time rules for each specific region.
- // For accurate real-world applications, use a robust time zone library.
- // Convert to time_t for easier manipulation (seconds since epoch)
- time_t currentTime_t = std::chrono::system_clock::to_time_t(now);
- // Tel Aviv Time (approx. UTC+3, assuming it's summer for simplicity)
- // This is a direct offset. For proper DST, you'd need a robust library.
- time_t telAvivTime_t = currentTime_t + (3 * 3600); // +3 hours
- struct tm telAvivTm;
- gmtime_s(&telAvivTm, &telAvivTime_t); // Use gmtime_s for thread safety
- char telAvivBuffer[64];
- strftime(telAvivBuffer, sizeof(telAvivBuffer), "Tel Aviv: %I:%M:%S %p", &telAvivTm);
- SetWindowText(hTelAvivTimeLabel, telAvivBuffer);
- // Tehran Time (approx. UTC+3:30)
- time_t tehranTime_t = currentTime_t + (3 * 3600) + (30 * 60); // +3 hours 30 minutes
- struct tm tehranTm;
- gmtime_s(&tehranTm, &tehranTime_t); // Use gmtime_s for thread safety
- char tehranBuffer[64];
- strftime(tehranBuffer, sizeof(tehranBuffer), "Tehran: %I:%M:%S %p", &tehranTm);
- SetWindowText(hTehranTimeLabel, tehranBuffer);
- }
- break;
- }
- case WM_CLOSE:
- DestroyWindow(hwnd);
- break;
- case WM_DESTROY:
- KillTimer(hwnd, 1); // Stop the timer
- PostQuitMessage(0);
- break;
- }
- return DefWindowProc(hwnd, uMsg, wParam, lParam);
- }
- // Entry point for the Windows application
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- // Register the window class
- WNDCLASSEX wc = {0};
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.lpfnWndProc = WindowProc;
- wc.hInstance = hInstance;
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); // Standard window background
- wc.lpszClassName = "TimeDisplayAppClass";
- RegisterClassEx(&wc);
- // Create the window
- HWND hwnd = CreateWindowEx(
- 0, "TimeDisplayAppClass", "World Clocks (C++)",
- WS_OVERLAPPEDWINDOW | WS_VISIBLE,
- CW_USEDEFAULT, CW_USEDEFAULT, 300, 150, // x, y, width, height
- NULL, NULL, hInstance, NULL);
- if (hwnd == NULL)
- {
- return 0;
- }
- // Set a timer to update the time every second
- SetTimer(hwnd, 1, 1000, NULL); // Timer ID 1, interval 1000ms
- // Initial update
- SendMessage(hwnd, WM_TIMER, 1, 0);
- // Message loop
- MSG msg = {0};
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return (int)msg.wParam;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement