-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatetime.h
More file actions
146 lines (133 loc) · 3.54 KB
/
datetime.h
File metadata and controls
146 lines (133 loc) · 3.54 KB
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
#ifndef DATETIME_H_
#define DATETIME_H_
#include <stdbool.h>
#include <assert.h>
typedef struct Datetime {
int year;
int month;
int day;
int hour;
int minute;
int second;
} Datetime;
Datetime datetime_now(void);
Datetime datetime_add(Datetime a, Datetime b);
int datetime_get_max_days_in_month(int year, int month);
// +1Y1M1w1d1h1m1s -> 0001-01-01 01:01:01
bool datetime_parse_duration(const char *source, Datetime *dt);
int datetime_cmp(Datetime a, Datetime b);
#endif // DATETIME_H_
#ifdef DATETIME_IMPLEMENTATION
#undef DATETIME_IMPLEMENTATION
#include <time.h>
Datetime datetime_now(void)
{
time_t rawtime;
struct tm *tm;
time(&rawtime);
tm = localtime(&rawtime);
return (Datetime) {
.year = 1900 + tm->tm_year,
.day = tm->tm_mday,
.month = tm->tm_mon + 1,
.hour = tm->tm_hour,
.minute = tm->tm_min,
.second = tm->tm_sec,
};
}
int datetime_get_max_days_in_month(int year, int month)
{
switch(month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
return 31;
case 4: case 6: case 9: case 11:
return 30;
case 2:
return year % 4 == 0 ? 29 : 28;
default:
return -1;
}
}
void divmod_add(int *divs, int *mods, int a, int b, int max)
{
*divs = (a + b) / max;
*mods = (a + b) % max;
}
Datetime datetime_add(Datetime a, Datetime b)
{
Datetime result = {0};
int total = 0;
result.year = a.year + b.year;
divmod_add(&result.minute, &result.second, a.second, b.second, 60);
divmod_add(&result.hour, &result.minute, a.minute, b.minute, 60);
divmod_add(&result.day, &result.hour, a.hour, b.hour, 24);
result.day = a.day + b.day;
result.month = a.month + b.month;
int current_threshold = datetime_get_max_days_in_month(result.year, result.month);
while(result.day > current_threshold) {
result.day -= current_threshold;
result.month += 1;
if(result.month > 12) {
result.month = 1;
result.year += 1;
}
}
return result;
}
int datetime_cmp(Datetime a, Datetime b)
{
int x = 0;
x = a.year - b.year;
if(x != 0) return x;
x = a.month - b.month;
if(x != 0) return x;
x = a.day - b.day;
if(x != 0) return x;
x = a.hour - b.hour;
if(x != 0) return x;
x = a.minute - b.minute;
if(x != 0) return x;
x = a.second - b.second;
return x;
}
bool datetime_parse_duration(const char *source, Datetime *dt)
{
// +1Y1M1w1d1h1m1s -> if now = 2020-01-24 10:00:00:000 then it becomes 2021-02-25 11:01:01
if(source[0] != '+') return false;
int n = 0;
Datetime add = {0};
for(int i = 0; source[i] != 0 && i < 256; ++i) {
while('0' <= source[i] && source[i] <= '9') {
n = n*10 + (source[i] - '0');
i+=1;
}
switch(source[i]) {
case 'Y':
add.year += n;
break;
case 'M':
add.month += n;
case 'w':
add.day += 7*n;
break;
case 'd':
add.day += n;
break;
case 'h':
add.hour += n;
break;
case 'm':
add.minute += n;
break;
case 's':
add.second += n;
break;
default:
assert(0 && "invalid deadline syntax");
}
n = 0;
}
*dt = add;
return true;
}
#endif // DATETIME_IMPLEMENTATION