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
|
#include "date.h"
#define _BSD_SOURCE
enum month {JAN=1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC} month;
Date extract_date(char *str) {
Date ret;
sscanf(str, "%d-%d-%d",
&ret.year,
&ret.month,
&ret.day);
return ret;
}
bool smaller(Date a, Date b) {
if (zero(b)) return true;
if (zero(a)) return false;
return ((a.year < b.year) ||
((a.year == b.year) && (a.month < b.month)) ||
((a.year == b.year) && (a.month == b.month) && (a.day <= b.day)));
}
bool strictly_smaller(Date a, Date b) {
if (zero(b)) return true;
if (zero(a)) return false;
return ((a.year < b.year) ||
((a.year == b.year) && (a.month < b.month)) ||
((a.year == b.year) && (a.month == b.month) && (a.day < b.day)));
}
bool eql(Date a, Date b) {
return (a.day == b.day) &&
(a.month == b.month) &&
(a.year == b.year);
}
bool zero(Date a) {
return ((a.year == 0) ||
(a.month == 0) ||
(a.day == 0));
}
void print_date(Date date) {
printf("%i/%i/%i\n", date.month, date.day, date.year);
}
char *print_date_to_string(Date date) {
char *ret = (char *)calloc(16, 1);
sprintf(ret, "%i/%i/%i", date.month, date.day, date.year);
return ret;
}
Date today() {
FILE *pipe = popen("date \"+%Y-%m-%d\"", "r");
char *td = (char *)malloc(12);
td = fgets(td, 12, pipe);
Date ret = extract_date(td);
pclose(pipe);
free(td);
return ret;
}
Date tomorrow(Date td) {
Date tm = { td.day+1, td.month, td.year };
if (tm.month == FEB && tm.day > 28) {
if (tm.year % 4 == 0 && tm.day > 29) {
tm.day -= 29; tm.month++;
} else {
tm.day -= 28; tm.month++;
}
} else if (tm.month == JAN || tm.month == MAR || tm.month == MAY || tm.month == JUL ||
tm.month == AUG || tm.month == OCT || tm.month == DEC) {
if (tm.day > 31 && tm.month == DEC) {
tm.day -= 31; tm.month = 1;
} else if (tm.day > 31) {
tm.day -= 31; tm.month++;
}
} else if (tm.month == APR || tm.month == JUN || tm.month == SEP || tm.month == NOV) {
if (tm.day > 30) {
tm.day -= 30; tm.month++;
}
}
return tm;
}
Date nextweek(Date td) {
Date nw = { td.day+7, td.month, td.year };
if (nw.month == FEB && nw.day > 28) {
if (nw.year % 4 == 0 && nw.day > 29) {
nw.day -= 29; nw.month++;
} else {
nw.day -= 28; nw.month++;
}
} else if (nw.month == JAN || nw.month == MAR || nw.month == MAY || nw.month == JUL ||
nw.month == AUG || nw.month == OCT || nw.month == DEC) {
if (nw.day > 31 && nw.month == DEC) {
nw.day -= 31; nw.month = 1;
} else if (nw.day > 31) {
nw.day -= 31; nw.month++;
}
} else if (nw.month == APR || nw.month == JUN || nw.month == SEP || nw.month == NOV) {
if (nw.day > 30) {
nw.day -= 30; nw.month++;
}
}
return nw;
}
|