Advertisement
pwtenny

Untitled

Jun 16th, 2025
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.63 KB | Source Code | 0 0
  1. #include <windows.h> // Required for Windows API
  2. #include <string>      // For std::string
  3. #include <chrono>      // For time operations
  4. #include <thread>      // For std::this_thread::sleep_for
  5. #include <ctime>       // For time_t, tm, gmtime
  6. #include <iomanip>     // For std::put_time
  7.  
  8. // Global handles for the time labels
  9. HWND hTelAvivTimeLabel;
  10. HWND hTehranTimeLabel;
  11.  
  12. // Window procedure function (callback for window messages)
  13. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  14. {
  15.     switch (uMsg)
  16.     {
  17.         case WM_CREATE:
  18.         {
  19.             // Create static text controls (labels)
  20.             hTelAvivTimeLabel = CreateWindowEx(
  21.                 0, "STATIC", "Tel Aviv: Loading...",
  22.                 WS_CHILD | WS_VISIBLE | SS_CENTER,
  23.                 10, 10, 260, 30, // x, y, width, height
  24.                 hwnd, (HMENU)1001, GetModuleHandle(NULL), NULL);
  25.  
  26.             hTehranTimeLabel = CreateWindowEx(
  27.                 0, "STATIC", "Tehran: Loading...",
  28.                 WS_CHILD | WS_VISIBLE | SS_CENTER,
  29.                 10, 50, 260, 30, // x, y, width, height
  30.                 hwnd, (HMENU)1002, GetModuleHandle(NULL), NULL);
  31.  
  32.             // Set font for better readability
  33.             HFONT hFont = CreateFont(
  34.                 24, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
  35.                 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
  36.                 DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "SansSerif");
  37.  
  38.             SendMessage(hTelAvivTimeLabel, WM_SETFONT, (WPARAM)hFont, TRUE);
  39.             SendMessage(hTehranTimeLabel, WM_SETFONT, (WPARAM)hFont, TRUE);
  40.  
  41.             break;
  42.         }
  43.         case WM_TIMER:
  44.         {
  45.             if (wParam == 1) // Our timer ID
  46.             {
  47.                 // Update times
  48.                 SYSTEMTIME stLocal;
  49.                 GetLocalTime(&stLocal); // Get local time
  50.  
  51.                 // Tel Aviv (GMT+3 or GMT+2 depending on DST, assuming GMT+3 for simplicity,
  52.                 // you'd need a more robust time zone library for exact DST handling)
  53.                 // For simplicity, we'll assume a fixed offset.
  54.                 // A robust solution would use a library like Howard Hinnant's date library or ICU.
  55.                 // For this example, we'll calculate approximate offsets.
  56.  
  57.                 // Get current UTC time
  58.                 SYSTEMTIME stUTC;
  59.                 GetSystemTime(&stUTC);
  60.  
  61.                 // Convert SYSTEMTIME to file time for easier arithmetic
  62.                 FILETIME ftUTC, ftTelAviv, ftTehran;
  63.                 SystemTimeToFileTime(&stUTC, &ftUTC);
  64.  
  65.                 // Define time zone offsets in 100-nanosecond intervals (1 hour = 3600 * 10^7 = 36,000,000,000)
  66.                 // Tel Aviv: UTC+3 (during summer), UTC+2 (during winter) - Let's use a fixed +3 for this example.
  67.                 // Tehran: UTC+3:30
  68.  
  69.                 // For simplicity and without a complex timezone library, we'll rely on
  70.                 // getting UTC time and applying a fixed offset.
  71.                 // The correct way would involve Windows API functions like TzSpecificLocalTimeToSystemTime
  72.                 // or a comprehensive timezone library.
  73.  
  74.                 // Using standard C++ time functions for simplicity, which requires knowing the current offset from UTC.
  75.                 // A better solution for real-world applications would be to use a robust date/time library
  76.                 // that handles time zones and DST correctly (e.g., ICU, or Howard Hinnant's date library).
  77.  
  78.                 // Let's approximate by getting current local time and adjusting.
  79.                 // This is a simplification and not perfectly accurate for all timezone/DST scenarios.
  80.  
  81.                 // For a truly accurate solution for time zones in C++, you would need
  82.                 // to use a dedicated library like Howard Hinnant's "date" library or ICU.
  83.                 // The Windows API itself has functions for time zone conversion, but they are
  84.                 // more involved than a simple offset.
  85.  
  86.                 // For this example, we'll demonstrate using `_tzset` and `_ftime_s` for basic timezone awareness,
  87.                 // but this is highly platform-specific (Microsoft Visual C++ runtime).
  88.                 // A portable C++ solution would use `std::chrono` and a timezone library.
  89.  
  90.                 // Let's use `std::chrono` for basic time, but truly complex timezones are hard without a library.
  91.                 auto now = std::chrono::system_clock::now();
  92.  
  93.                 // Tel Aviv: Approx UTC+3:00 (Israel Standard Time / Israel Daylight Time)
  94.                 // Tehran: Approx UTC+3:30 (Iran Standard Time / Iran Daylight Time)
  95.  
  96.                 // The following time zone calculations are *approximations* and
  97.                 // do not account for Daylight Saving Time rules for each specific region.
  98.                 // For accurate real-world applications, use a robust time zone library.
  99.  
  100.                 // Convert to time_t for easier manipulation (seconds since epoch)
  101.                 time_t currentTime_t = std::chrono::system_clock::to_time_t(now);
  102.  
  103.                 // Tel Aviv Time (approx. UTC+3, assuming it's summer for simplicity)
  104.                 // This is a direct offset. For proper DST, you'd need a robust library.
  105.                 time_t telAvivTime_t = currentTime_t + (3 * 3600); // +3 hours
  106.                 struct tm telAvivTm;
  107.                 gmtime_s(&telAvivTm, &telAvivTime_t); // Use gmtime_s for thread safety
  108.  
  109.                 char telAvivBuffer[64];
  110.                 strftime(telAvivBuffer, sizeof(telAvivBuffer), "Tel Aviv: %I:%M:%S %p", &telAvivTm);
  111.                 SetWindowText(hTelAvivTimeLabel, telAvivBuffer);
  112.  
  113.                 // Tehran Time (approx. UTC+3:30)
  114.                 time_t tehranTime_t = currentTime_t + (3 * 3600) + (30 * 60); // +3 hours 30 minutes
  115.                 struct tm tehranTm;
  116.                 gmtime_s(&tehranTm, &tehranTime_t); // Use gmtime_s for thread safety
  117.  
  118.                 char tehranBuffer[64];
  119.                 strftime(tehranBuffer, sizeof(tehranBuffer), "Tehran: %I:%M:%S %p", &tehranTm);
  120.                 SetWindowText(hTehranTimeLabel, tehranBuffer);
  121.             }
  122.             break;
  123.         }
  124.         case WM_CLOSE:
  125.             DestroyWindow(hwnd);
  126.             break;
  127.  
  128.         case WM_DESTROY:
  129.             KillTimer(hwnd, 1); // Stop the timer
  130.             PostQuitMessage(0);
  131.             break;
  132.     }
  133.     return DefWindowProc(hwnd, uMsg, wParam, lParam);
  134. }
  135.  
  136. // Entry point for the Windows application
  137. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  138. {
  139.     // Register the window class
  140.     WNDCLASSEX wc = {0};
  141.     wc.cbSize = sizeof(WNDCLASSEX);
  142.     wc.lpfnWndProc = WindowProc;
  143.     wc.hInstance = hInstance;
  144.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  145.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); // Standard window background
  146.     wc.lpszClassName = "TimeDisplayAppClass";
  147.     RegisterClassEx(&wc);
  148.  
  149.     // Create the window
  150.     HWND hwnd = CreateWindowEx(
  151.         0, "TimeDisplayAppClass", "World Clocks (C++)",
  152.         WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  153.         CW_USEDEFAULT, CW_USEDEFAULT, 300, 150, // x, y, width, height
  154.         NULL, NULL, hInstance, NULL);
  155.  
  156.     if (hwnd == NULL)
  157.     {
  158.         return 0;
  159.     }
  160.  
  161.     // Set a timer to update the time every second
  162.     SetTimer(hwnd, 1, 1000, NULL); // Timer ID 1, interval 1000ms
  163.  
  164.     // Initial update
  165.     SendMessage(hwnd, WM_TIMER, 1, 0);
  166.  
  167.     // Message loop
  168.     MSG msg = {0};
  169.     while (GetMessage(&msg, NULL, 0, 0))
  170.     {
  171.         TranslateMessage(&msg);
  172.         DispatchMessage(&msg);
  173.     }
  174.  
  175.     return (int)msg.wParam;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement