-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.c
119 lines (108 loc) · 2.56 KB
/
date.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
#include "date.h"
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// documentation in date.h
const int DAYS_PER_YEAR = 365;
const double YEARS_PER_DAY = 1.0 / 365;
const int DATE_LENGTH = 11;
struct date {
int total_day;
int day;
int month;
int year;
};
struct date *date_create(int day, int month, int year) {
assert(day >= 1);
assert(day <= 31);
assert(month >= 1);
assert(month <= 12);
assert(year >= 1900);
assert(year <= 2100);
struct date *d = malloc(sizeof(struct date));
d->total_day = 0;
d->day = day;
d->month = month;
d->year = year;
d->total_day += year * DAYS_PER_YEAR;
int months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for (int i = 1; i < month; i++) {
d->total_day += months[i - 1];
}
d->total_day += day;
return d;
}
struct date *current_date() {
time_t t;
time(&t);
struct tm *date = gmtime(&t);
int day = date->tm_mday;
int month = date->tm_mon + 1;
int year = date->tm_year + 1900;
return date_create(day, month, year);
}
struct date *string_to_date(const char *s) {
assert(s);
char new_s[DATE_LENGTH];
strcpy(new_s, s);
int day = atoi(new_s + 8);
new_s[7] = '\0';
int month = atoi(new_s + 5);
new_s[4] = '\0';
int year = atoi(new_s);
return date_create(day, month, year);
}
void date_destroy(struct date *d) {
assert(d);
free(d);
}
int date_compare(const struct date *d1, const struct date *d2) {
assert(d1);
assert(d2);
return d1->total_day - d2->total_day;
}
void date_string(struct date *d, char *s) {
assert(d);
assert(s);
int year = d->year;
int month = d->month;
int day = d->day;
s[10] = '\0';
s[9] = '0' + day % 10;
day /= 10;
s[8] = '0' + day;
s[7] = '-';
s[6] = '0' + month % 10;
month /= 10;
s[5] = '0' + month;
s[4] = '-';
s[3] = '0' + year % 10;
year /= 10;
s[2] = '0' + year % 10;
year /= 10;
s[1] = '0' + year % 10;
year /= 10;
s[0] = '0' + year;
}
// returns true if the given date is in the future, false if it is in the
// present or past
bool future(struct date *d) {
struct date *today = current_date();
int diff = date_compare(d, today);
free(today);
return diff > 0;
}
/*
// uncomment to test
int main() {
struct date *d = date_create(30, 7, 2023);
struct date *today = current_date();
int diff = date_compare(d, today);
char s[DATE_LENGTH];
date_string(d, s);
printf("diff is %d, day is %s", diff, s);
}
*/