0% found this document useful (0 votes)
19 views

Com Prog

The code displays and continuously updates the current date, time, and related information using C programming. It obtains the system time and adjusts it for the local time zone before extracting individual components. Helper functions help format and display the time data in a readable way, while the main loop ensures the display is refreshed every second.

Uploaded by

Anacio Aryana
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Com Prog

The code displays and continuously updates the current date, time, and related information using C programming. It obtains the system time and adjusts it for the local time zone before extracting individual components. Helper functions help format and display the time data in a readable way, while the main loop ensures the display is refreshed every second.

Uploaded by

Anacio Aryana
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

I.

Introduction

Accurate timekeeping and the display of real-time date and time information play a crucial

role in numerous applications, ranging from scheduling tasks to coordinating events across

different time zones. This research focuses on the development of a C program that

provides a continuous and up-to-date display of the current date, time, day of the week,

month, year, and whether it is day or night. By leveraging the functionalities offered by the

`<stdio.h>` and `<time.h>` header files, this program enables efficient input/output

operations and time manipulation.

The program's core functionality revolves around extracting and formatting individual time

components and ensuring that the displayed information is always current. It achieves this

by utilizing functions designed to obtain the system's local time, adjusting it for a specific

time zone, and continuously refreshing the displayed information. Additionally, the program

incorporates a visually appealing feature by providing greetings based on the time of day,

creating a more immersive user experience.

This research delves into a comprehensive analysis of the code structure, function

definitions, and execution flow of the C program. By examining this code, researchers and

developers can enhance their understanding of real-time timekeeping and gain insights into

effective date and time management within the context of C programming.


In the following sections, we will explore the code step by step, elucidating its various

components and the rationale behind their implementation. By examining the program's

structure and functionality, readers will gain a deeper understanding of real-time date and

time display in C programming, enabling them to apply this knowledge to their own projects

and applications.

Keywords: Real-time date and time display, C programming, time manipulation, time zone

adjustment, input/output operations.


II. Methodology

The provided code is a C program that displays the current date, time, day of the week,

month, year, and whether it is day or night. It uses the `stdio.h` and `time.h` libraries for

input/output and time-related functions, respectively.

Here's an overview of the methodology used in the code:

1. The program defines a function `isNightTime` that takes the current hour and minute

as arguments and determines whether it is night time based on the hour. It returns 1 if it

is night and 0 otherwise.

2. Three helper functions are defined: `displayYear`, `displayMonth`, and

`displayDayOfWeek`. These functions take the corresponding values (year, month, and

day of the week) and display them using the `printf` function.

3. Another helper function, `displayTimeOfDay`, is defined to display a greeting message

based on the current hour. It uses if-else statements to determine whether it is morning,

afternoon, or evening.

4. In the `main` function, a continuous loop is set up using `while (1)` to repeatedly

update and display the current date and time.


5. The current time is obtained using the `time` function, and the local time is retrieved

using `localtime` and stored in the `localTime` struct.

6. Since the code is specifically adjusted for Manila (UTC+8), the hour in `localTime` is

incremented by 8 to account for the time zone. The modified time is then converted back

to a time structure using `mktime`.

7. Individual time components such as hour, minute, second, day, month, year, and day

of the week are extracted from the `localTime` struct.

8. The `isNightTime` function is called to determine whether it is night or day based on

the current hour and minute.

9. The current date, time, day of the week, month, year, and day/night status are

displayed using `printf` statements. The helper functions `displayDayOfWeek`,

`displayMonth`, `displayYear`, and `displayTimeOfDay` are called to display the

corresponding information.

10. The `fflush` function is called to flush the output buffer and ensure the displayed

information is immediately visible.

11. The program includes a 1-second delay using the `sleep` function to update the time

display every second.


12. The program continues to loop indefinitely until it is manually terminated.

Overall, this program continuously displays the current date, time, and related

information in a loop, updating the display every second.


Results and Discussion

#include <stdio.h>

#include <time.h>

int isNightTime(int hour, int minute) {

return (hour >= 20 || hour < 6);

void displayYear(int year) {

printf("Current Year: %04d\n", year);

void displayMonth(int month) {

char* months[] = { "January", "February", "March", "April", "May", "June", "July",

"August", "September", "October", "November", "December" };

printf("Current Month: %s\n", months[month - 1]);

void displayDayOfWeek(int dayOfWeek) {

char* days[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",

"Friday", "Saturday" };

printf("Day of the Week: %s\n", days[dayOfWeek]);

void displayTimeOfDay(int hour) {


if (hour >= 0 && hour < 12) {

printf("Good morning!\n");

} else if (hour >= 12 && hour < 18) {

printf("Good afternoon!\n");

} else {

printf("Good evening!\n");

int main() {

while (1) {

// Obtain current time

time_t currentTime;

struct tm* localTime;

currentTime = time(NULL);

localTime = localtime(&currentTime);

// Adjust time for Manila (UTC+8)

localTime->tm_hour += 8;

mktime(localTime);

// Extract individual time components

int hour = localTime->tm_hour;

int minute = localTime->tm_min;

int second = localTime->tm_sec;

int day = localTime->tm_mday;


int month = localTime->tm_mon + 1; // tm_mon is zero-based

int year = localTime->tm_year + 1900; // tm_year is years since 1900

int dayOfWeek = localTime->tm_wday; // tm_wday represents the day of the

week (0 = Sunday)

// Determine day or night

char* timePeriod = isNightTime(hour, minute) ? "Night" : "Day";

// Display the current date, time, day of the week, month, year, and day/night

status

printf("Current Date: %02d/%02d/%04d\n", month, day, year);

printf("Current Time: %02d:%02d:%02d\n", hour, minute, second);

displayDayOfWeek(dayOfWeek);

displayMonth(month);

displayYear(year);

printf("Day/Night: %s\n", timePeriod);

displayTimeOfDay(hour);

fflush(stdout);

// Delay for 1 second

sleep(1);

return 0;

}
Start

Obtain current time

Adjust time for


Manila

Extract time
components

Determine Day or
Night

Display current
date and time

Display day of
the week
Display month

Display year

Display
Day/Night

Display time of
Day

Delay for 1 second

Repeat

End
The provided code serves as a basic example of how to display and update the

current date, time, and related information using C programming language. It

demonstrates the usage of functions and libraries available in C to manipulate and

format time data.

The `isNightTime` function determines whether it is currently night or day based on

the hour and minute values provided. This function relies on the assumption that

nighttime begins at 8:00 PM (20:00) and lasts until 6:00 AM. However, it's worth

noting that this approach might not be universally accurate for determining nighttime

in different time zones or regions with varying daylight hours.

The helper functions `displayYear`, `displayMonth`, and `displayDayOfWeek` are

used to format and print the corresponding time components. They provide a more

readable and user-friendly representation of the time information.

The `displayTimeOfDay` function provides a greeting message based on the current

hour. It distinguishes between morning, afternoon, and evening by checking the hour

ranges and displaying an appropriate message. This function adds a personalized

touch to the output by providing a greeting relevant to the time of day.

In the `main` function, a continuous loop is created using `while (1)` to repeatedly

update and display the current date and time. This loop ensures that the time

information is continuously updated and displayed on the screen.


The `time` and `localtime` functions are used to obtain the current time and convert it

to a local time representation, respectively. The time information is adjusted for the

Manila time zone (UTC+8) by incrementing the hour value by 8 and then using

`mktime` to update the `localTime` structure.

After extracting the individual time components (hour, minute, second, day, month,

year, and day of the week), the code determines whether it is night or day by calling

the `isNightTime` function. This information is stored in the `timePeriod` variable.

Finally, the current date, time, day of the week, month, year, and day/night status are

displayed using `printf` statements. The corresponding helper functions are called to

format and display the time information in a readable format. The `fflush` function is

used to flush the output buffer and ensure that the displayed information is

immediately visible.

A 1-second delay is included using the `sleep` function to update the time display

every second. This delay prevents the program from overwhelming the system and

allows for smoother and more readable output.

Overall, the code provides a basic framework for continuously displaying the current

date, time, and related information. It can be modified and expanded upon to suit

specific requirements and use cases.


III. Conclusion

In conclusion, the provided code demonstrates how to utilize the `stdio.h` and

`time.h` libraries in C to create a program that continuously displays the current date,

time, day of the week, month, year, and whether it is day or night. By using functions

and structuring the code in a loop, the program updates and refreshes the displayed

information every second.

The code showcases important concepts such as working with time structures,

manipulating time components, and formatting output using `printf`. It also introduces

the idea of time zone adjustment, as it specifically accounts for the Manila time zone

(UTC+8) by incrementing the hour value.

While the code provides a foundation, it should be noted that it has limitations. The

determination of night or day is based on a fixed assumption for a specific time range

and may not be applicable universally. Additionally, the code assumes a continuous

execution without considering potential interruptions or termination conditions.

Nevertheless, the code serves as a starting point for building more complex time-

related applications or integrating time functionalities into larger projects. With further

modifications and enhancements, it can be tailored to meet specific requirements or

combined with additional functionality.


IV. Recommendation

Based on the provided code and its limitations, here are a few recommendations to

consider for further development or improvement:

1. **Flexibility in Nighttime Determination**: Instead of relying on a fixed assumption

for nighttime determination, consider implementing a more dynamic approach. This

could involve retrieving sunset and sunrise times based on the location or using an

API that provides accurate sunset and sunrise information.

2. **Handling Interruptions and Termination**: Currently, the code runs indefinitely in

a continuous loop. It may be beneficial to incorporate a mechanism to handle

interruptions or termination conditions gracefully. For example, you could add a

keyboard interrupt handler to allow the program to be terminated by the user.

3. **User-Defined Time Zones**: Extend the code to allow users to input their desired

time zone, rather than assuming a fixed time zone like Manila (UTC+8). This would

provide greater flexibility and make the program more adaptable to different regions.

4. **Error Handling**: Implement error handling mechanisms to handle potential

errors or unexpected situations. For example, you could check the return values of

functions like `time` and `localtime` to ensure they execute successfully.


5. **Enhanced User Interface**: Consider enhancing the user interface by utilizing

additional formatting options or adding more descriptive messages. You could

provide a menu or interactive prompts for users to select different options or display

specific information.

6. **Modularization and Code Organization**: As the program grows in complexity,

consider modularizing the code into separate functions or files. This would improve

code readability, maintainability, and reusability.

7. **Unit Testing**: Write test cases to verify the correctness and robustness of the

code. This would help identify and fix any potential bugs or issues.

8. **Localization**: If the program is intended for international use, you can explore

localization options to display date and time information in different languages or

formats based on user preferences.

By considering these recommendations and further refining the code, you can

enhance its functionality, make it more robust, and adapt it to meet specific

requirements in different scenarios or applications.


Abstract

The provided C code demonstrates the implementation of a real-time display of the

current date, time, and day/night status. By utilizing the `stdio.h` and `time.h`

libraries, the program harnesses the functionality they offer to retrieve the current

time, manipulate time components, and format the output for display. In this

particular case, the program adjusts the time for the Manila time zone (UTC+8) and

refreshes the displayed information every second, ensuring that it remains accurate

and up-to-date.

The primary objective of this research is to explore the practical application of time-

related functions and examine how to structure code for real-time data display. By

dissecting the provided code and analyzing its various elements, researchers can

gain a deeper understanding of time manipulation in programming and its

implications for real-time applications. This research serves as a valuable resource

for those interested in developing similar programs or incorporating time-related

features into their projects.

Furthermore, the research also acknowledges the limitations of the current code

implementation. While the program successfully displays the real-time date, time,

and day/night status, it is worth noting that it is specifically designed for the Manila

time zone. As a result, it may not provide accurate information for users in different

time zones. The research emphasizes the importance of considering time zone
adjustments when developing real-time applications to ensure that the displayed

information aligns with the intended audience.

To enhance the functionality and adaptability of the code, the research proposes

potential improvements. For instance, incorporating user input to select the desired

time zone would allow the program to cater to users from various regions.

Additionally, implementing error handling mechanisms to handle situations where the

time retrieval or manipulation encounters issues would contribute to the program's

robustness and reliability.

Overall, this research contributes to the understanding of time manipulation in

programming and provides a foundation for developing more advanced time-related

applications. By analyzing the code, exploring its limitations, and suggesting potential

enhancements, researchers and developers can expand their knowledge and create

more sophisticated programs that involve real-time data display and time

management.
Real-Time Display of Current Date, Time, and Day/Night Status Using

C Programming Language

In partial fulfilment of the requirements in Computer Programming 10

By

Kesia Joy Aquino

June 21 2023
Appendix

#include <stdio.h>

#include <time.h>

int isNightTime(int hour, int minute) {

return (hour >= 20 || hour < 6);

void displayYear(int year) {

printf("Current Year: %04d\n", year);

void displayMonth(int month) {

char* months[] = { "January", "February", "March", "April", "May", "June", "July", "August",

"September", "October", "November", "December" };

printf("Current Month: %s\n", months[month - 1]);

void displayDayOfWeek(int dayOfWeek) {

char* days[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };

printf("Day of the Week: %s\n", days[dayOfWeek]);

}
void displayTimeOfDay(int hour) {

if (hour >= 0 && hour < 12) {

printf("Good morning!\n");

} else if (hour >= 12 && hour < 18) {

printf("Good afternoon!\n");

} else {

printf("Good evening!\n");

int main() {

while (1) {

// Obtain current time

time_t currentTime;

struct tm* localTime;

currentTime = time(NULL);

localTime = localtime(&currentTime);

// Adjust time for Manila (UTC+8)

localTime->tm_hour += 8;

mktime(localTime);

// Extract individual time components

int hour = localTime->tm_hour;

int minute = localTime->tm_min;


int second = localTime->tm_sec;

int day = localTime->tm_mday;

int month = localTime->tm_mon + 1; // tm_mon is zero-based

int year = localTime->tm_year + 1900; // tm_year is years since 1900

int dayOfWeek = localTime->tm_wday; // tm_wday represents the day of the week (0 = Sunday)

// Determine day or night

char* timePeriod = isNightTime(hour, minute) ? "Night" : "Day";

// Display the current date, time, day of the week, month, year, and day/night status

printf("Current Date: %02d/%02d/%04d\n", month, day, year);

printf("Current Time: %02d:%02d:%02d\n", hour, minute, second);

displayDayOfWeek(dayOfWeek);

displayMonth(month);

displayYear(year);

printf("Day/Night: %s\n", timePeriod);

displayTimeOfDay(hour);

fflush(stdout);

// Delay for 1 second

sleep(1);

return 0;

You might also like