Skip to content

Commit 60cb987

Browse files
feat: changes added comments and fixed time zone offsets
1 parent 3a5fa25 commit 60cb987

File tree

9 files changed

+193
-88
lines changed

9 files changed

+193
-88
lines changed

Clock.cpp

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,118 @@
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.
1211
// Constructor for the Clock class.
1312
// It initializes user preferences and calculates the time offsets based on location.
1413
Clock::Clock(const std::string &uName,
1514
const std::string &loc,
1615
int format,
1716
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.
2221
{
2322
// 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.
2424
timeZoneOffset = Utility::getTimeZoneOffset(location);
25+
// The minuteOffset handles cases where the offset is not a whole hour (like Kathmandu, which is +45 minutes).
2526
minuteOffset = Utility::getMinutesOffset(location);
2627

2728
// 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.
2830
Utility::logUserChoice(userName, location, timeFormat, displaySeconds);
2931

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.
3134
weatherInfo = Utility::getWeatherInfo(location);
3235
}
33-
3436
// The start() function contains an infinite loop that updates the clock every second.
3537
void Clock::start()
3638
{
37-
while (true) // Loop forever until the program is stopped.
39+
while (true) // This loop runs forever until you manually stop the program.
3840
{
3941
// Clear the console screen before re-drawing the time.
42+
// This avoids printing new output on top of the old, creating a "refresh" effect.
4043
Display::clearScreen();
4144

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).
4548

4649
// 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).
4952

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.
5155
mktime(t);
5256

53-
// Extract the hour, minute, and second from the time structure.
57+
// Extract the current hour, minute, and second from the tm structure.
5458
int hour = t->tm_hour;
5559
int minute = t->tm_min;
5660
int second = t->tm_sec;
5761

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.
5963
bool isPm = false;
6064
if (timeFormat == 12)
6165
{
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.
6569
}
6670

6771
// -------------------- DISPLAY TIME --------------------
72+
// The following lines call Display functions to draw each part of the clock.
73+
6874
// 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.
7177

7278
// 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.
7480

7581
// 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.
7884

79-
// Optionally display seconds if the user chose to.
85+
// Optionally display seconds if the user chose to show them.
8086
if (displaySeconds)
8187
{
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.
8591
}
8692

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.
8894
if (timeFormat == 12)
8995
{
90-
Display::displayAmPm(isPm, 80, 5);
96+
Display::displayAmPm(isPm, 80, 5); // Draw either "AM" or "PM" at column 80, row 5.
9197
}
9298

9399
// -------------------- DISPLAY DATE --------------------
94-
// Get the local date (formatted according to location).
100+
// Get the local date formatted based on the location.
95101
std::string localDate = Utility::getLocalDate(location);
96102

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).
98105
printf("\033[%d;%dH", 15, 10);
99106
printf("\033[1;33mToday in %s: %s\033[0m\n", location.c_str(), localDate.c_str());
100107

101108
// -------------------- 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.
103110
printf("\033[%d;%dH", 17, 10);
104111
printf("\033[1;32mWeather: %s\033[0m\n", weatherInfo.c_str());
105112

106-
fflush(stdout); // Ensure all output is printed immediately.
113+
fflush(stdout); // Flush the output buffer to ensure everything is printed immediately.
107114

108-
// Pause the program for 1 second before updating the display.
115+
// Pause for 1 second before the loop runs again.
109116
this_thread::sleep_for(chrono::seconds(1));
110117
}
111118
}

Clock.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
#ifndef CLOCK_H
1+
#ifndef CLOCK_H // If CLOCK_H is not defined, then define it.
22
#define CLOCK_H
33

4-
#include <string>
4+
#include <string> // Include string to use std::string.
55

6-
// The Clock class handles user settings, time calculation, and the display loop.
6+
// The Clock class handles user settings, time calculations, and updating the display continuously.
77
class Clock
88
{
99
private:
10-
std::string userName; // User’s name.
11-
std::string location; // Location (e.g., London, Tokyo).
12-
int timeFormat; // Time format: 12 or 24 hours.
13-
bool displaySeconds; // Whether to show seconds.
14-
int timeZoneOffset; // Hour offset based on location.
15-
int minuteOffset; // Minute offset (for non-whole hour differences).
10+
std::string userName; // Stores the user’s name.
11+
std::string location; // Stores the location (e.g., London, Tokyo) for time zone and weather purposes.
12+
int timeFormat; // Stores the time format: 12 for 12-hour or 24 for 24-hour format.
13+
bool displaySeconds; // Indicates whether seconds should be displayed.
14+
int timeZoneOffset; // Stores the hour offset from UTC for the given location.
15+
int minuteOffset; // Stores any additional minute offset (for cities with non-whole hour offsets).
1616

17-
// Weather information fetched from the internet.
17+
// Stores the weather information fetched from the internet.
1818
std::string weatherInfo;
1919

2020
public:
21-
// Constructor: Initializes the clock with the user’s preferences.
21+
// Constructor: Initializes the Clock with the provided user settings.
2222
Clock(const std::string &userName,
2323
const std::string &location,
2424
int timeFormat,
2525
bool displaySeconds);
2626

27-
// Starts the continuous clock loop (updating the display every second).
27+
// The start() method enters an infinite loop to continuously update the clock display.
2828
void start();
2929
};
3030

31-
#endif
31+
#endif // End of CLOCK_H definition.

DigitalClock

0 Bytes
Binary file not shown.

Display.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
#ifndef DISPLAY_H
1+
#ifndef DISPLAY_H // If DISPLAY_H is not defined, then define it.
22
#define DISPLAY_H
33

4-
#include <string>
4+
#include <string> // Include string to use std::string in our class.
55

66
// The Display class handles drawing the clock’s digits and symbols on the console.
77
class Display
88
{
99
private:
1010
static const int HEIGHT = 7; // Each digit or symbol is 7 lines tall.
1111

12-
// ANSI escape codes for colored output.
13-
static constexpr const char* FG_COLOR = "\033[1;32m"; // Bold Green text.
14-
static constexpr const char* RESET_COLOR = "\033[0m"; // Reset to default color.
12+
// ANSI escape codes for colored output:
13+
// FG_COLOR sets the text to bold green.
14+
static constexpr const char* FG_COLOR = "\033[1;32m";
15+
// RESET_COLOR resets the text color to the terminal's default.
16+
static constexpr const char* RESET_COLOR = "\033[0m";
1517

1618
// ASCII art patterns for digits 0-9.
1719
static const std::string digits[10][HEIGHT];
1820
// Pattern for the colon.
1921
static const std::string colon[HEIGHT];
20-
// Patterns for AM and PM.
22+
// Patterns for the "AM" and "PM" indicators.
2123
static const std::string am[HEIGHT];
2224
static const std::string pm[HEIGHT];
2325

@@ -35,4 +37,4 @@ class Display
3537
static void displayAmPm(bool isPm, int x, int y);
3638
};
3739

38-
#endif
40+
#endif // End of DISPLAY_H definition.

Input.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#ifndef INPUT_H
1+
#ifndef INPUT_H // If INPUT_H is not defined, then define it.
22
#define INPUT_H
33

4-
#include <string>
4+
#include <string> // Include string to use std::string in our class.
55

66
// The Input class provides functions to obtain and validate input from the user.
77
class Input
@@ -15,4 +15,4 @@ class Input
1515
static std::string getLocationInput();
1616
};
1717

18-
#endif
18+
#endif // End of INPUT_H definition.

Utility.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ using namespace std;
1212
// getTimeZoneOffset() returns the hour offset from UTC for a given location.
1313
int Utility::getTimeZoneOffset(const std::string &location)
1414
{
15-
if (location == "London") return 1;
16-
if (location == "Berlin") return 2;
17-
if (location == "Tokyo") return 9;
18-
if (location == "Sydney") return 10;
19-
if (location == "Kathmandu") return 5;
15+
if (location == "London") return 0; // UTC+1
16+
if (location == "Berlin") return 1; // UTC+1
17+
if (location == "Tokyo") return 9; // UTC+9
18+
if (location == "Sydney") return 11; // UTC+11
19+
if (location == "Kathmandu") return 5; // UTC+5:45 (hour component)
2020
return -9999; // Invalid location indicator.
2121
}
2222

Utility.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#ifndef UTILITY_H
1+
#ifndef UTILITY_H // If UTILITY_H is not defined, define it.
22
#define UTILITY_H
33

4-
#include <string>
4+
#include <string> // Include string to work with std::string.
55

66
// The Utility class provides helper functions for:
77
// - Determining time zone offsets
@@ -16,17 +16,17 @@ class Utility
1616
// Returns any additional minute offset for the given location.
1717
static int getMinutesOffset(const std::string &location);
1818

19-
// Logs the user’s settings to a file.
19+
// Logs the user’s settings (name, location, time format, seconds option) to a file.
2020
static void logUserChoice(const std::string &name,
2121
const std::string &location,
2222
int timeFormat,
2323
bool displaySeconds);
2424

25-
// Fetches weather information (temperature and description) for the location.
25+
// Fetches weather information (temperature and description) for the given location.
2626
static std::string getWeatherInfo(const std::string &location);
2727

28-
// Returns today's date formatted for the location.
28+
// Returns today's date formatted for the given location.
2929
static std::string getLocalDate(const std::string &location);
3030
};
3131

32-
#endif
32+
#endif // End of UTILITY_H definition.

0 commit comments

Comments
 (0)