|
1 | | -#include "Clock.h" // Include the Clock class definition. |
2 | | -#include "Display.h" // Include the Display class (used to show digits, colon, AM/PM). |
3 | | -#include "Utility.h" // Include helper functions (for time zones, weather, and logging). |
4 | | -#include <iostream> // For input and output. |
5 | | -#include <ctime> // For working with time. |
6 | | -#include <thread> // For sleep functionality. |
7 | | -#include <chrono> // For time durations (e.g., seconds). |
8 | | -#include <cstdio> // For printf (printing formatted text). |
9 | | - |
10 | | -using namespace std; |
11 | | - |
| 1 | +#include "Clock.h" // Include the Clock class declaration so we know what functions and variables belong to Clock. |
| 2 | +#include "Display.h" // Include the Display class for drawing digits and symbols on the screen. |
| 3 | +#include "Utility.h" // Include Utility functions that help with time zones, weather data, and logging. |
| 4 | +#include <iostream> // Include the iostream library for standard input/output operations. |
| 5 | +#include <ctime> // Include ctime for working with time functions (e.g., getting the current time). |
| 6 | +#include <thread> // Include thread to allow us to pause the program (sleep functionality). |
| 7 | +#include <chrono> // Include chrono to work with time durations, such as seconds. |
| 8 | +#include <cstdio> // Include cstdio for using printf, which prints formatted text. |
| 9 | + |
| 10 | +using namespace std; // Allows us to use names from the std (standard) namespace without the std:: prefix. |
12 | 11 | // Constructor for the Clock class. |
13 | 12 | // It initializes user preferences and calculates the time offsets based on location. |
14 | 13 | Clock::Clock(const std::string &uName, |
15 | 14 | const std::string &loc, |
16 | 15 | int format, |
17 | 16 | bool dispSec) |
18 | | - : userName(uName), // Initialize the user name. |
19 | | - location(loc), // Initialize the location. |
20 | | - timeFormat(format), // Set the time format (12-hour or 24-hour). |
21 | | - displaySeconds(dispSec) // Determine if seconds should be displayed. |
| 17 | + : userName(uName), // Initialize the userName member with the passed value uName. |
| 18 | + location(loc), // Initialize the location member with loc. |
| 19 | + timeFormat(format), // Set the timeFormat (either 12 or 24) using format. |
| 20 | + displaySeconds(dispSec) // Determine whether to display seconds (true/false) from dispSec. |
22 | 21 | { |
23 | 22 | // 1. Calculate offsets for time zone and minutes. |
| 23 | + // The timeZoneOffset tells us how many hours to add to UTC to get the local time. |
24 | 24 | timeZoneOffset = Utility::getTimeZoneOffset(location); |
| 25 | + // The minuteOffset handles cases where the offset is not a whole hour (like Kathmandu, which is +45 minutes). |
25 | 26 | minuteOffset = Utility::getMinutesOffset(location); |
26 | 27 |
|
27 | 28 | // 2. Log the user’s preferences to a file. |
| 29 | + // This function writes the user's choices (name, location, format, and seconds option) into a log file. |
28 | 30 | Utility::logUserChoice(userName, location, timeFormat, displaySeconds); |
29 | 31 |
|
30 | | - // 3. Fetch the weather information for the location (this happens once). |
| 32 | + // 3. Fetch the weather information for the location. |
| 33 | + // This gets the current weather details (like temperature and description) and stores it in weatherInfo. |
31 | 34 | weatherInfo = Utility::getWeatherInfo(location); |
32 | 35 | } |
33 | | - |
34 | 36 | // The start() function contains an infinite loop that updates the clock every second. |
35 | 37 | void Clock::start() |
36 | 38 | { |
37 | | - while (true) // Loop forever until the program is stopped. |
| 39 | + while (true) // This loop runs forever until you manually stop the program. |
38 | 40 | { |
39 | 41 | // Clear the console screen before re-drawing the time. |
| 42 | + // This avoids printing new output on top of the old, creating a "refresh" effect. |
40 | 43 | Display::clearScreen(); |
41 | 44 |
|
42 | | - // Get the current UTC time. |
43 | | - time_t now = time(NULL); |
44 | | - tm *t = gmtime(&now); // Convert to a UTC time structure. |
| 45 | + // Get the current time in UTC (Coordinated Universal Time). |
| 46 | + time_t now = time(NULL); // time(NULL) returns the current time as a time_t type. |
| 47 | + tm *t = gmtime(&now); // Convert the time_t value into a tm structure (broken-down time in UTC). |
45 | 48 |
|
46 | 49 | // Apply the time zone and minute offsets (to convert UTC to local time). |
47 | | - t->tm_hour += timeZoneOffset; |
48 | | - t->tm_min += minuteOffset; |
| 50 | + t->tm_hour += timeZoneOffset; // Adjust the hours by adding the location's time zone offset. |
| 51 | + t->tm_min += minuteOffset; // Adjust the minutes by adding any extra minutes (if applicable). |
49 | 52 |
|
50 | | - // mktime() fixes any values that might be out of normal range (like minutes over 59). |
| 53 | + // mktime() fixes any out-of-range values. |
| 54 | + // For example, if t->tm_min exceeds 59, mktime() will increment the hour accordingly. |
51 | 55 | mktime(t); |
52 | 56 |
|
53 | | - // Extract the hour, minute, and second from the time structure. |
| 57 | + // Extract the current hour, minute, and second from the tm structure. |
54 | 58 | int hour = t->tm_hour; |
55 | 59 | int minute = t->tm_min; |
56 | 60 | int second = t->tm_sec; |
57 | 61 |
|
58 | | - // For 12-hour format, determine if it is AM or PM. |
| 62 | + // For 12-hour format, we need to adjust the hour and determine AM or PM. |
59 | 63 | bool isPm = false; |
60 | 64 | if (timeFormat == 12) |
61 | 65 | { |
62 | | - isPm = (hour >= 12); |
63 | | - hour = hour % 12; // Convert to 12-hour format. |
64 | | - if (hour == 0) hour = 12; // In a 12-hour clock, 0 is represented as 12. |
| 66 | + isPm = (hour >= 12); // If the hour is 12 or more, it's PM. |
| 67 | + hour = hour % 12; // Convert hour from 24-hour to 12-hour format. |
| 68 | + if (hour == 0) hour = 12; // In 12-hour format, 0 is represented as 12. |
65 | 69 | } |
66 | 70 |
|
67 | 71 | // -------------------- DISPLAY TIME -------------------- |
| 72 | + // The following lines call Display functions to draw each part of the clock. |
| 73 | + |
68 | 74 | // Display the hour digits: |
69 | | - Display::displayDigit(hour / 10, 10, 5); // Tens place of the hour. |
70 | | - Display::displayDigit(hour % 10, 18, 5); // Ones place of the hour. |
| 75 | + Display::displayDigit(hour / 10, 10, 5); // Display the tens digit of the hour at column 10, row 5. |
| 76 | + Display::displayDigit(hour % 10, 18, 5); // Display the ones digit of the hour at column 18, row 5. |
71 | 77 |
|
72 | 78 | // Display a colon (:) between hours and minutes. |
73 | | - Display::displayColon(26, 5); |
| 79 | + Display::displayColon(26, 5); // Draw the colon at column 26, row 5. |
74 | 80 |
|
75 | 81 | // Display the minute digits: |
76 | | - Display::displayDigit(minute / 10, 34, 5); // Tens place of the minute. |
77 | | - Display::displayDigit(minute % 10, 42, 5); // Ones place of the minute. |
| 82 | + Display::displayDigit(minute / 10, 34, 5); // Draw the tens digit of the minute at column 34, row 5. |
| 83 | + Display::displayDigit(minute % 10, 42, 5); // Draw the ones digit of the minute at column 42, row 5. |
78 | 84 |
|
79 | | - // Optionally display seconds if the user chose to. |
| 85 | + // Optionally display seconds if the user chose to show them. |
80 | 86 | if (displaySeconds) |
81 | 87 | { |
82 | | - Display::displayColon(50, 5); |
83 | | - Display::displayDigit(second / 10, 58, 5); |
84 | | - Display::displayDigit(second % 10, 66, 5); |
| 88 | + Display::displayColon(50, 5); // Draw another colon for seconds at column 50, row 5. |
| 89 | + Display::displayDigit(second / 10, 58, 5); // Draw the tens digit of the seconds at column 58, row 5. |
| 90 | + Display::displayDigit(second % 10, 66, 5); // Draw the ones digit of the seconds at column 66, row 5. |
85 | 91 | } |
86 | 92 |
|
87 | | - // If 12-hour format is selected, display the AM/PM indicator. |
| 93 | + // If the clock is in 12-hour format, display the AM/PM indicator. |
88 | 94 | if (timeFormat == 12) |
89 | 95 | { |
90 | | - Display::displayAmPm(isPm, 80, 5); |
| 96 | + Display::displayAmPm(isPm, 80, 5); // Draw either "AM" or "PM" at column 80, row 5. |
91 | 97 | } |
92 | 98 |
|
93 | 99 | // -------------------- DISPLAY DATE -------------------- |
94 | | - // Get the local date (formatted according to location). |
| 100 | + // Get the local date formatted based on the location. |
95 | 101 | std::string localDate = Utility::getLocalDate(location); |
96 | 102 |
|
97 | | - // Move the cursor to a specific position (row 15, column 10) and print the date. |
| 103 | + // Use ANSI escape sequences to move the cursor to row 15, column 10, |
| 104 | + // then print the date in a specific color (here, yellow is used). |
98 | 105 | printf("\033[%d;%dH", 15, 10); |
99 | 106 | printf("\033[1;33mToday in %s: %s\033[0m\n", location.c_str(), localDate.c_str()); |
100 | 107 |
|
101 | 108 | // -------------------- DISPLAY WEATHER -------------------- |
102 | | - // Move the cursor to row 17, column 10 and display the weather information. |
| 109 | + // Move the cursor to row 17, column 10 and print the weather info. |
103 | 110 | printf("\033[%d;%dH", 17, 10); |
104 | 111 | printf("\033[1;32mWeather: %s\033[0m\n", weatherInfo.c_str()); |
105 | 112 |
|
106 | | - fflush(stdout); // Ensure all output is printed immediately. |
| 113 | + fflush(stdout); // Flush the output buffer to ensure everything is printed immediately. |
107 | 114 |
|
108 | | - // Pause the program for 1 second before updating the display. |
| 115 | + // Pause for 1 second before the loop runs again. |
109 | 116 | this_thread::sleep_for(chrono::seconds(1)); |
110 | 117 | } |
111 | 118 | } |
0 commit comments