-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.c
140 lines (116 loc) · 3.05 KB
/
clock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include "display.h"
#include "charset.h"
#define DATE_LENGTH 6
#define TIME_LENGTH 9
void drawStr(char* buf, char strlen, char x, char y) {
char i;
for(i=0; i<strlen - 1; i++) {
drawChar(i*(CHAR_WIDTH + 2) + x, y, buf[i]);
}
}
/* Draws the hours, minutes, and seconds in block letters. */
void drawTime(struct tm * timeinfo) {
char buf[TIME_LENGTH];
gotoxy(2,15);
printf("Local Time");
strftime(buf, TIME_LENGTH, "%H:%M:%S", timeinfo);
drawStr(buf, TIME_LENGTH, 9, 18);
}
/* Draws a calendar. */
void drawCalendar(struct tm * timeinfo) {
char i;
char wk1;
char wk_offset;
char day_offset;
char today;
char month;
char buf[15]; /* 15 = max number of characters in a month/year combo */
today = timeinfo->tm_mday;
month = timeinfo->tm_mon;
wk1 = 0;
wk_offset = 0;
day_offset = 0;
/* Print a calendar header. */
gotoxy(3,2);
strftime(buf, 15, "%B %Y",timeinfo);
printf("%s Calendar", buf);
gotoxy(3,4);
printf("Su Mo Tu We Th Fr Sa Wk");
/* Blank out the calendar area. */
for(i=0; i < 8; i++) {
gotoxy(3, 5+i);
printf(" ");
}
for(i=1; i < 32; i++) { /* Iterate over all possible days in the month. */
timeinfo->tm_mday = i;
if (mktime(timeinfo) != -1) { /* mktime returns -1 for Feb 30 and friends */
/* If it's the first day of the month, calculate the start position. */
if(i==1) {
day_offset = timeinfo->tm_wday;
strftime(buf, 3, "%U", timeinfo);
wk1 = atoi(buf);
gotoxy(25, 5);
printf("%d",wk1);
/* If it's any other Sunday, with iteration still in the same month. */
} else if(timeinfo->tm_wday == 0 && month == timeinfo->tm_mon) {
/* Calculate what week it is. */
wk_offset++;
day_offset = 0;
gotoxy(25, 5 + wk_offset);
printf("%d",wk1+wk_offset);
}
/* Print out a single day in the correct position */
if(month == timeinfo->tm_mon) {
/* Invert console colors to mark the current day. */
if(i==today) {
invertColors();
}
gotoxy((day_offset * 3)+3,wk_offset + 5);
printf("%d",timeinfo->tm_mday);
day_offset++;
/* Un-invert colors of the current day. */
if(i==today) {
restoreColors();
}
}
}
}
}
/* Draws the three-letter month and two-digit day in block letters. */
void drawDate(struct tm * timeinfo) {
char buf[DATE_LENGTH];
gotoxy(36,2);
printf("Today's date");
gotoxy(36,5);
strftime(buf, DATE_LENGTH, "%a%d", timeinfo);
drawStr(buf, DATE_LENGTH, 36, 5);
}
/* Entry point. */
int main() {
char day;
time_t rawtime;
struct tm * timeinfo;
day = 0;
clrscr();
while(1) { /* Run until interrupted. */
time(&rawtime);
timeinfo = localtime(&rawtime);
/* Draw the time on every update. */
drawTime(timeinfo);
/* If the date has changed, draw the date and year. This avoids console
shearing on older hardware. */
if(day != timeinfo->tm_mday) {
/* Update the day. */
day = timeinfo->tm_mday;
drawDate(timeinfo);
/* Update the calendar. */
drawCalendar(timeinfo);
}
fflush(stdout);
sleep(1);
}
}