-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
277 lines (251 loc) · 5.83 KB
/
main.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "common.h"
#ifdef __linux__
void clrscr(){
clear(); // Clears Screen
refresh(); // Refreshes (Syncs change in memory to terminal)
}
void Sleep(int ms){
char command[20];
sprintf(command, "sleep %ds", ms / 1000);
system(command);
}
#else
void clrscr(){
system("cls");
}
#endif
struct time timezones[MAXTIMEZONES]; // List of timezones to show on time section
int tzcount; // Number of timezones
int main(){
// Variable declarations
struct date d;
char input;
int flag = 1;
// Initial Value for variables
tzcount = readTimezones(timezones); // Read timezone from file
d = getCurrentDate();
#ifdef __linux__
// curses Terminal initialization
WINDOW *w = initscr();
cbreak();
nodelay(w, TRUE);
noecho();
#endif
while(flag) {
timezones[0] = getCurrentTime();
updateTime(timezones, tzcount);
clrscr();
displayCalendarAndTime(&d);
input = displayMainMenu(&d);
// Handle cases not handled by displayMainMenu
switch (input){
case 't':
tzcount = displayTimezoneMenu();
break;
case 'q':
flag = 0;
break;
}
// If date is out of range (due to user input)
if (!validDate(&d)) {
printf("\nDate is Out of Range\n");
fflush(stdin);
#ifdef __linux__
getchar();
#else
getch();
#endif
d = getCurrentDate();
}
}
// Done :)
return 0;
}
void printTabs(int n){
// prints n number of tabs on the screen
int i;
for (i=0;i<n;i++)
printf("\t");
}
void printTime(struct time* time){
// prints time in a nice format
printf("%s %d:%d:%d %s (%+d mins)", time->name, time->hr % 12 , time->min, time->sec, (time->hr<13)? "AM":"PM", time->utcdiff);
}
void displayCalendarAndTime(struct date *d){
// Displays Month Calendar And Time on the screen
int i,t=0;
struct monthData m = calculateMonthData(d);
// Header line (Month/Year) and Days
printf("\t\t %d/%d/%d %s\r\n", d->year, d->month, d->day, (d->type == AD_DATE)? "AD":"BS");
printf("Sun \tMon \tTues \tWed \tThus \tFri \tSat \t\tTime\r\n");
// Start of Days
// Gap before 1st day
printTabs(m.firstDay);
int day = m.firstDay;
i = 1;
for (i=1; i<=m.numDays; i++, day++){
// Print this day number
if (i == d->day)
printf("-%d-\t",i);
else
printf("%d\t", i);
if (day % 7 == 6) { // After Saturday the calender portion ends and time portion starts
if (t < tzcount) {
printf("\t");
printTime(&timezones[t]);
t++;
}
printf("\r\n");
}
}
// Continue showing the remaining timezones
printTabs(8 - day % 7);
for (;t<tzcount;t++){
printTime(&timezones[t]);
printf("\r\n");
printTabs(8);
}
}
char displayMainMenu(struct date *d){
// Show the main menu/options at the bottom
// also if user pressed a button changes/converts date accordingly
// returns the key user pressed or returns 0 if time has to be updated
char input=0;
struct time now, prev;
printf("\n\r\nPress \r\n p for previous month \t\t n for next month, \r\n d to jump to specific date \t t to edit timezones \r\n s to switch calendar \t\t q to quit \r\n ");
prev = getCurrentTime();
while (1) {
// Sleep for 100 ms
Sleep(100);
now = getCurrentTime();
// Stop when time changes
if (now.sec != prev.sec)
break;
prev = now;
// or when user presses a key
#ifdef __linux__
input = getch();
if (input != ERR)
break;
#else
if (_kbhit()) {
input = getch();
break;
}
#endif
}
switch(input) {
case 'p':
if (d->month== 1) {
d->month = 12;
d->year--;
} else {
d->month--;
}
d->day = 1;
break;
case 'n':
if (d->month == 12) {
d->month = 1;
d->year++;
} else {
d->month++;
}
d->day = 1;
break;
case 'd':
while (1) {
printf("\r\n Enter date as Y/m/d : ");
d->year = -1; // Make date invalid before entry
scanf(" %d/%d/%d", &d->year, &d->month, &d->day);
if (validDate(d))
break;
else{
printf("\r\nDate Invalid");
fflush(stdin);
#ifdef __linux__
getchar();
#else
getch();
#endif
}
}
break;
case 's':
if (d->type == AD_DATE) {
*d = convertADToBS(d);
} else {
*d = convertBSToAD(d);
}
break;
}
return input;
}
int displayTimezoneMenu(){
// Shows, edits, and adds timezones
// Returns final number of tzs
int i, entry, count = tzcount;
char input;
clrscr();
printf("TIMEZONES\r\n");
printf("=========\r\n");
// List timezones
printf("SN \tUTC OFFSET \tName \r\n");
for (i=0; i<count; i++) {
printf("%d \t%+d \t\t%s\r\n", i, timezones[i].utcdiff, timezones[i].name);
}
// Show menu
printf("\nPress: \r\n");
if (count < MAXTIMEZONES)
printf(" a to add\r\n");
if (count > 1){
printf(" e to edit entry\r\n");
printf(" r to remove entry\r\n");
}
printf("Any other key to exit\r\n");
// Get Input and Process it
fflush(stdin);
#ifdef __linux__
input = getchar();
#else
input = getch();
#endif
switch (input) {
case 'a':
if (count < MAXTIMEZONES) {
entry = count;
count++;
}
break;
case 'e':
if (count > 1) {
printf("Entry Number? (1-%d)", count-1);
scanf("%d", &entry);
}
break;
case 'r':
if (count > 1){
printf("Entry Number? (1-%d)", count-1);
scanf("%d", &entry);
if (entry>0 && entry<count){
for (i=entry;i<count;i++)
timezones[i] = timezones[i+1];
count = --tzcount;
saveTimezones(timezones, tzcount);
}
entry = 0;
}
break;
default:
entry = 0;
}
// Modify/Add entry if it was selected
if (entry && entry<=count) {
printf("Name: ");
scanf("%s", timezones[entry].name);
printf("\r\nDifference from UTC (in mins): ");
scanf("%d", &timezones[entry].utcdiff);
saveTimezones(timezones, count);
}
return count;
}