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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
#ifndef _TIME_H
#define _TIME_H
#include <stdc_common.h>
#define CLK_TCK 1000000000 // doesnt matter; clock() will always fail.
typedef long clock_t;
clock_t clock(void) {
// "If the processor time used is not available or its value cannot be represented, the function returns the value (clock_t)-1." C89 § 4.12.2.1
return -1;
}
double difftime(time_t time1, time_t time0) {
return (double)(time1 - time0);
}
time_t time(time_t *timer) {
struct timespec ts = {0};
if (clock_gettime(CLOCK_REALTIME, &ts) != 0) return -1;
if (timer) *timer = ts.tv_sec;
return ts.tv_sec;
}
// @NONSTANDARD(except in UTC+0): we don't support local time in timezones other than UTC+0.
struct tm {
int tm_sec; /* seconds after the minute --- [0, 60] */
int tm_min; /* minutes after the hour --- [0, 59] */
int tm_hour; /* hours since midnight --- [0, 23] */
int tm_mday; /* day of the month --- [1, 31] */
int tm_mon; /* months since January --- [0, 11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday --- [0, 6] */
int tm_yday; /* days since January 1 --- [0, 365] */
int tm_isdst; /* Daylight Saving Time flag */
};
void _gmtime_r(const time_t *timer, struct tm *tm) {
time_t t = *timer;
int year = 1970;
int days = t / 86400;
int leap_year;
int month;
tm->tm_isdst = 0;
tm->tm_wday = (4 + days) % 7; // jan 1 1970 was a thursday
while (1) {
leap_year = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
int ydays = leap_year ? 366 : 365;
if (days < ydays) break;
days -= ydays;
++year;
}
tm->tm_year = year - 1900;
tm->tm_yday = days;
for (month = 0; month < 12; ++month) {
int mdays;
switch (month) {
case 0: case 2: case 4: case 6: case 7: case 9: case 11:
mdays = 31;
break;
case 3: case 5: case 8: case 10:
mdays = 30;
break;
case 1:
mdays = 28 + leap_year;
break;
}
if (days < mdays) break;
days -= mdays;
}
tm->tm_mday = days + 1;
tm->tm_mon = month;
t %= 86400;
tm->tm_hour = t / 3600;
t %= 3600;
tm->tm_min = t / 60;
tm->tm_sec = t % 60;
}
time_t mktime(struct tm *tm) {
// @NONSTANDARD-ish.
// not implementing this -- note that the implementation has to support tm_* values
// outside of their proper ranges.
return (time_t)-1;
}
struct tm *gmtime(const time_t *timer) {
static struct tm result;
_gmtime_r(timer, &result);
return &result;
}
struct tm *localtime(const time_t *timer) {
static struct tm result;
_gmtime_r(timer, &result);
return &result;
}
static const char _wday_name[7][4] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
static const char _weekday_name[7][16] = {
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
};
static const char _mon_name[12][4] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
static const char _month_name[12][16] = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
char *asctime(const struct tm *timeptr) {
// lifted from the (draft of the) C standard
static char result[32];
sprintf(result, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
_wday_name[timeptr->tm_wday],
_mon_name[timeptr->tm_mon],
timeptr->tm_mday, timeptr->tm_hour,
timeptr->tm_min, timeptr->tm_sec,
1900 + timeptr->tm_year);
return result;
}
char *ctime(const time_t *timer) {
return asctime(localtime(timer));
}
size_t strftime(char *s, size_t maxsize, const char *format, const struct tm *tm) {
size_t n = 0, l;
char *name;
while (*format) {
if (*format == '%') {
++format;
int c = *format++;
switch (c) {
case 'a':
if (n+4 >= maxsize) return 0;
strcpy(s, _wday_name[tm->tm_wday]);
s += 3;
n += 3;
break;
case 'A':
name = _weekday_name[tm->tm_wday];
l = strlen(name);
if (n+l+1 >= maxsize) return 0;
strcpy(s, name);
s += l;
n += l;
break;
case 'b':
if (n+4 >= maxsize) return 0;
strcpy(s, _mon_name[tm->tm_mon]);
s += 3;
n += 3;
break;
case 'B':
name = _month_name[tm->tm_mon];
l = strlen(name);
if (n+l+1 >= maxsize) return 0;
strcpy(s, name);
s += l;
n += l;
break;
case 'c':
if (n+32 >= maxsize) return 0;
sprintf(s, "%s %02d %s %d %02d:%02d:%02d %s UTC",
_wday_name[tm->tm_wday], tm->tm_mday, _mon_name[tm->tm_mon],
1900+tm->tm_year, (tm->tm_hour + 11) % 12 + 1, tm->tm_min, tm->tm_sec,
tm->tm_hour >= 12 ? "PM" : "AM");
s += 31;
n += 31;
break;
case 'd':
if (n+3 >= maxsize) return 0;
sprintf(s, "%02d", tm->tm_mday);
s += 2;
n += 2;
break;
case 'H':
if (n+3 >= maxsize) return 0;
sprintf(s, "%02d", tm->tm_hour);
s += 2;
n += 2;
break;
case 'I':
if (n+3 >= maxsize) return 0;
sprintf(s, "%02d", (tm->tm_hour + 11) % 12 + 1);
s += 2;
n += 2;
break;
case 'j':
if (n+4 >= maxsize) return 0;
sprintf(s, "%03d", tm->tm_yday + 1);
s += 3;
n += 3;
break;
case 'm':
if (n+3 >= maxsize) return 0;
sprintf(s, "%02d", tm->tm_mon + 1);
s += 2;
n += 2;
break;
case 'M':
if (n+3 >= maxsize) return 0;
sprintf(s, "%02d", tm->tm_min);
s += 2;
n += 2;
break;
case 'p':
if (n+3 >= maxsize) return 0;
sprintf(s, "%s", tm->tm_hour >= 12 ? "PM" : "AM");
s += 2;
n += 2;
break;
case 'S':
if (n+3 >= maxsize) return 0;
sprintf(s, "%02d", tm->tm_sec);
s += 2;
n += 2;
break;
case 'w':
if (n+2 >= maxsize) return 0;
sprintf(s, "%d", tm->tm_wday);
s += 1;
n += 1;
break;
case 'x':
if (n+16 >= maxsize) return 0;
sprintf(s, "%s %02d %s %d",
_wday_name[tm->tm_wday], tm->tm_mday, _mon_name[tm->tm_mon],
1900+tm->tm_year);
s += 15;
n += 15;
break;
case 'X':
if (n+16 >= maxsize) return 0;
sprintf(s, "%02d:%02d:%02d %s UTC",
(tm->tm_hour + 11) % 12 + 1, tm->tm_min, tm->tm_sec,
tm->tm_hour >= 12 ? "PM" : "AM");
s += 15;
n += 15;
break;
case 'y':
if (n+3 >= maxsize) return 0;
sprintf(s, "%02d", tm->tm_year % 100);
s += 2;
n += 2;
break;
case 'Y':
if (n+5 >= maxsize) return 0;
sprintf(s, "%d", tm->tm_year + 1900);
s += 4;
n += 4;
break;
case 'Z':
if (n+4 >= maxsize) return 0;
strcpy(s, "UTC");
s += 3;
n += 3;
break;
case '%':
if (n >= maxsize) return 0;
*s++ = '%';
n += 1;
break;
case 'U': // WEEK NUMBER OF THE YEAR? WHO GIVES A SHIT?
case 'W': // WEEK NUMBER OF THE YEAR MONDAY-BASED EDITION. IF YOU DIDNT ALREADY GET ENOUGH WEEK NUMBERS. FUCK YOU
default:
fprintf(stderr, "Bad strftime format.\n");
abort();
}
} else {
if (n >= maxsize) return 0;
*s++ = *format++;
n += 1;
}
}
if (n >= maxsize) return 0;
*s = 0;
#undef _Push_str
return n;
}
#endif // _TIME_H
|