-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.c
153 lines (130 loc) · 2.53 KB
/
calendar.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
#include <stdio.h>
#include <stdbool.h>
// a.
bool is_leap_year(int year)
{
if ( year%400 == 0) // Exactly divisible by 400 e.g. 1600, 2000
return true;
else if ( year%100 == 0) // Exactly divisible by 100 and not by 400 e.g. 1900, 2100
return false;
else if ( year%4 == 0 ) // Exactly divisible by 4 and neither by 100 nor 400 e.g. 2016, 2020
return true;
else // Not divisible by 4 or 100 or 400 e.g. 2017, 2018, 2019
return false;
}
// b.
int days_in_month(int month, int year)
{
if(month == 9 || month == 4 || month == 6 || month == 11)
return 30;
else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
return 31;
else if(month == 2 && is_leap_year(year)==true)
return 29;
else
return 28;
}
// c.
int days_from_epoch(int day, int month, int year)
{
int days = 0;
int i = 0;
int j = 0;
int diff = 0;
if(year<1970)
{
diff = 1970 - year;
for(j=1;j<diff;j++)
{
if(is_leap_year(1970-j))
days+=366;
else
days+=365;
}
for(i=month+1;i<=12;i++)
days += days_in_month(i-1,year);
days += (days_in_month(month,year)-day+1);
}
else
{
diff = year - 1970;
for(j=0;j<diff;j++)
{
if(is_leap_year(1970+j))
days+=366;
else
days+=365;
}
for(i=1;i<month;i++)
days += days_in_month(i,year);
days += (day-1);
}
return days;
}
// d.
int day_of_week(int day, int month, int year)
{
int _day = days_from_epoch(day,month,year)%7;
if(year>1970)
{
switch(_day)
{
case 0 :
return 4;
case 1 :
return 5;
case 2 :
return 6;
case 3 :
return 0;
case 4 :
return 1;
case 5 :
return 2;
case 6 :
return 3;
}
}
else
{
switch(_day)
{
case 0 :
return 4;
case 1 :
return 3;
case 2 :
return 2;
case 3 :
return 1;
case 4 :
return 0;
case 5 :
return 6;
case 6 :
return 5;
}
}
}
// e.
void display_month(int month, int year)
{
printf("\nTHE CALENDAR FOR %d-%d IS -\n",month,year);
printf("\n S M T W T F S\n");
int begin = day_of_week(1,month,year);
int i = 0;
int count = 0;
for(i=0;i<3*begin;i++)
{
printf(" ");
++count;
}
for(i=1;i<=days_in_month(month,year);i++)
{
printf("%3d",i);
count+=3;
if(count%21==0)
printf("\n");
}
printf("\n\n");
}