summaryrefslogtreecommitdiffstats
path: root/src/datetime.c
blob: 72f02191eadfbadc7524f4f118d1bbc26f93a3d0 (plain)
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 * Copyright (C) 2021, 2022  P. J. McDermott
 *
 * This file is part of @
 *
 * @ is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * @ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with @.  If not, see <http://www.gnu.org/licenses/>.
 */

#define _XOPEN_SOURCE
/* glibc requires these for timegm(): */
#define _DEFAULT_SOURCE
#define _BSD_SOURCE

#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "datetime.h"
#include "formats.h"
#include "i18n.h"

static void
_datetime_buf_cpy_p(char *dst, const char *src)
{
	for (; *src != '\0'; ++src, ++dst) {
		if (*src == '^') {
			*dst = ' ';
		} else {
			*dst = *src;
		}
	}
	*dst = '\0';
}

static void
_datetime_buf_cpy_f(char *dst, const char *src)
{
	for (; *src != '\0'; ++src, ++dst) {
		if (*src == '^') {
			++src;
		}
		*dst = *src;
	}
	*dst = '\0';
}

static void
_datetime_reset_tm(struct tm *tm)
{
	tm->tm_year  = INT_MIN;
	tm->tm_mon   = INT_MIN;
	tm->tm_mday  = INT_MIN;
	tm->tm_wday  = INT_MIN;
	tm->tm_hour  = INT_MIN;
	tm->tm_min   = INT_MIN;
	tm->tm_sec   = INT_MIN;
	tm->tm_isdst = INT_MIN;
}

static void
_datetime_normalize(struct tm *now_tm, struct tm *arg_tm, time_t *arg_sec)
{
	int days_in_mon[12] = {31, 28, 31, 30, 31, 30,
	                       31, 31, 30, 31, 30, 31};

	if (now_tm->tm_year % 4 == 0 && (now_tm->tm_year % 100 != 0 ||
				now_tm->tm_year % 400 == 0)) {
		days_in_mon[1] = 29;
	}

	if (arg_tm->tm_sec == INT_MIN) {
		arg_tm->tm_sec = 0;
	}
	if (arg_tm->tm_mday == INT_MIN && arg_tm->tm_wday == INT_MIN) {
		/* No date specified; try today. */
		arg_tm->tm_year = now_tm->tm_year;
		arg_tm->tm_mon  = now_tm->tm_mon;
		arg_tm->tm_mday = now_tm->tm_mday;
		arg_tm->tm_wday = now_tm->tm_wday;
		*arg_sec = mktime(arg_tm);
		if (difftime(*arg_sec, mktime(now_tm)) <= 0) {
			/* Specified time already happened today; use tomorrow.
			 */
			++arg_tm->tm_mday;
			arg_tm->tm_wday = (arg_tm->tm_wday + 1) % 7;
			if (arg_tm->tm_mday > days_in_mon[arg_tm->tm_mon]) {
				arg_tm->tm_mday = 1;
				++arg_tm->tm_mon;
				if (arg_tm->tm_mon >= 12) {
					arg_tm->tm_mon = 0;
					++arg_tm->tm_year;
				}
			}
			*arg_sec = mktime(arg_tm);
		}
	} else if (arg_tm->tm_mday == INT_MIN) {
		/* Only a weekday specified; try tomorrow or next week. */
		arg_tm->tm_year = now_tm->tm_year;
		arg_tm->tm_mon  = now_tm->tm_mon;
		arg_tm->tm_mday = now_tm->tm_mday;
		if (arg_tm->tm_wday <= now_tm->tm_wday) {
			arg_tm->tm_mday += 7;
		}
		arg_tm->tm_mday += arg_tm->tm_wday - now_tm->tm_wday;
		if (arg_tm->tm_mday > days_in_mon[arg_tm->tm_mon]) {
			arg_tm->tm_mday -= days_in_mon[arg_tm->tm_mon];
			++arg_tm->tm_mon;
			if (arg_tm->tm_mon >= 12) {
				arg_tm->tm_mon = 0;
				++arg_tm->tm_year;
			}
		}
		*arg_sec = mktime(arg_tm);
	} else if (arg_tm->tm_year == INT_MIN) {
		/* No year specified; try this year. */
		arg_tm->tm_year = now_tm->tm_year;
		*arg_sec = mktime(arg_tm);
		if (difftime(*arg_sec, mktime(now_tm)) <= 0) {
			/* Specified time already happened this year; use next
			 * year. */
			++arg_tm->tm_year;
			*arg_sec = mktime(arg_tm);
		}
	} else {
		*arg_sec = mktime(arg_tm);
	}
}

int
datetime_parse(struct tm *now_tm, const char *input,
		struct tm *arg_tm, time_t *arg_sec)
{
	int        date_fmt_len;
	int        time_fmt_len;
	int        misc_fmt_len;
	int        fmt_len;
	char      *fmt_buf;
	size_t     d;
	size_t     t;
	char      *end;
	time_t     sec;
	bool       got;

	date_fmt_len = strlen(FORMATS_DATE[0]);
	time_fmt_len = strlen(FORMATS_TIME[0]);
	misc_fmt_len = strlen(FORMATS_MISC[0]);
	fmt_len = date_fmt_len + time_fmt_len;
	if (misc_fmt_len > fmt_len) {
		fmt_len = misc_fmt_len;
	}
	fmt_buf = calloc(fmt_len + 1, sizeof(*fmt_buf));
	if (fmt_buf == NULL) {
		fprintf(stderr, _("Failed to allocate buffer: %s\n"),
				strerror(errno));
		return -1;
	}

	sec = *arg_sec;  /* GCC is dumb. */
	got = false;
	for (t = 0; t < sizeof(FORMATS_TIME) / sizeof(FORMATS_TIME[0]); ++t) {
		_datetime_buf_cpy_p(fmt_buf, FORMATS_TIME[t]);
		_datetime_reset_tm(arg_tm);
		end = strptime(input, fmt_buf, arg_tm);
		if (end != NULL && *end == '\0') {
			_datetime_normalize(now_tm, arg_tm, arg_sec);
			if (arg_tm->tm_year >= 0) {
				free(fmt_buf);
				return 0;
			}
			sec = *arg_sec;
			got = true;
		}
	}
	for (d = 0; d < sizeof(FORMATS_DATE) / sizeof(FORMATS_DATE[0]); ++d) {
		if (FORMATS_DATE[d][0] == '\0') {
			break;
		}
		_datetime_buf_cpy_p(fmt_buf, FORMATS_DATE[d]);
		for (t = 0; t < sizeof(FORMATS_TIME) / sizeof(FORMATS_TIME[0]);
				++t) {
			_datetime_buf_cpy_p(fmt_buf + date_fmt_len,
					FORMATS_TIME[t]);
			_datetime_reset_tm(arg_tm);
			end = strptime(input, fmt_buf, arg_tm);
			if (end != NULL && *end == '\0') {
				_datetime_normalize(now_tm, arg_tm, arg_sec);
				if (arg_tm->tm_year >= 0) {
					free(fmt_buf);
					return 0;
				}
				sec = *arg_sec;
				got = true;
			}
		}
	}
	for (t = 0; t < sizeof(FORMATS_TIME) / sizeof(FORMATS_TIME[0]); ++t) {
		_datetime_buf_cpy_p(fmt_buf, FORMATS_TIME[t]);
		for (d = 0; d < sizeof(FORMATS_DATE) / sizeof(FORMATS_DATE[0]);
				++d) {
			if (FORMATS_DATE[d][0] == '\0') {
				break;
			}
			_datetime_buf_cpy_p(fmt_buf + time_fmt_len,
					FORMATS_DATE[d]);
			_datetime_reset_tm(arg_tm);
			end = strptime(input, fmt_buf, arg_tm);
			if (end != NULL && *end == '\0') {
				_datetime_normalize(now_tm, arg_tm, arg_sec);
				if (arg_tm->tm_year >= 0) {
					free(fmt_buf);
					return 0;
				}
				sec = *arg_sec;
				got = true;
			}
		}
	}
	for (d = 0; d < sizeof(FORMATS_MISC) / sizeof(FORMATS_MISC[0]); ++d) {
		_datetime_buf_cpy_p(fmt_buf, FORMATS_MISC[d]);
		_datetime_reset_tm(arg_tm);
		end = strptime(input, fmt_buf, arg_tm);
		if (end != NULL && *end == '\0') {
			_datetime_normalize(now_tm, arg_tm, arg_sec);
			if (arg_tm->tm_year >= 0) {
				free(fmt_buf);
				return 0;
			}
			sec = *arg_sec;
			got = true;
		}
	}

	if (got == true) {
		*arg_sec = sec;
		free(fmt_buf);
		return 0;
	} else {
		fputs(_("Unknown date format\n"), stderr);
		free(fmt_buf);
		return -1;
	}
}

static void
_datetime_copy_collapse(char *buf, const char *fmt)
{
	int  buf_i;
	int  l;
	bool was_space;
	int  i;

	buf_i = 0;
	l = strlen(fmt);
	was_space = true;
	for (i = 0; i < l; ++i) {
		if (isspace(fmt[i]) != 0) {
			if (was_space == false) {
				buf[buf_i++] = ' ';
				was_space = true;
			}
		} else {
			buf[buf_i++] = fmt[i];
			was_space = false;
		}
	}
	if (buf_i > 0) {
		buf[buf_i - 1] = '\0';
	} else {
		buf[0] = '\0';
	}
}

static int
_datetime_strftime(const char *fmts[], const struct tm *tm, char **out,
		size_t *buf_sz, char **buf, size_t *i)
{
	int resized;

	if (*i == 0) {
		*buf_sz = strlen(fmts[0]) + 1;
		*buf = calloc(*buf_sz, sizeof(**buf));
		if (*buf == NULL) {
			fprintf(stderr, _("Failed to allocate buffer: %s\n"),
					strerror(errno));
			return -1;
		}
		*out = calloc(*buf_sz, sizeof(**out));
		if (*out == NULL) {
			fprintf(stderr, _("Failed to allocate buffer: %s\n"),
					strerror(errno));
			return -1;
		}
	}

	if (fmts[*i][0] == '\0') {
		++*i;
	}

	_datetime_buf_cpy_f(*out, fmts[*i]);

	resized = 0;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
	while (strftime(*buf, *buf_sz, *out, tm) == 0) {
#pragma GCC diagnostic pop
		++*buf_sz;
		*buf = realloc(*buf, *buf_sz * sizeof(**buf));
		if (*buf == NULL) {
			fprintf(stderr, _("Failed to allocate buffer: %s\n"),
					strerror(errno));
			return -1;
		}
		resized = 1;
	}
	if (resized > 0) {
		*out = realloc(*out, *buf_sz * sizeof(**out));
		if (*out == NULL) {
			fprintf(stderr, _("Failed to allocate buffer: %s\n"),
					strerror(errno));
			return -1;
		}
	}

	_datetime_copy_collapse(*out, *buf);
	++*i;

	return 1;
}

int
datetime_strftime_date(const struct tm *tm, char **out, size_t *buf_sz,
		char **buf, size_t *i)
{
	if (*i >= sizeof(FORMATS_DATE) / sizeof(FORMATS_DATE[0])) {
		if (*out != NULL) {
			free(*out);
		}
		if (*buf != NULL) {
			free(*buf);
		}
		return 0;
	}

	return _datetime_strftime(FORMATS_DATE, tm, out, buf_sz, buf, i);
}

int
datetime_strftime_time(const struct tm *tm, char **out, size_t *buf_sz,
		char **buf, size_t *i)
{
	if (*i >= sizeof(FORMATS_TIME) / sizeof(FORMATS_TIME[0])) {
		if (*out != NULL) {
			free(*out);
		}
		if (*buf != NULL) {
			free(*buf);
		}
		return 0;
	}

	return _datetime_strftime(FORMATS_TIME, tm, out, buf_sz, buf, i);
}

int
datetime_strftime_misc(const struct tm *tm, char **out, size_t *buf_sz,
		char **buf, size_t *i)
{
	if (*i >= sizeof(FORMATS_MISC) / sizeof(FORMATS_MISC[0])) {
		if (*out != NULL) {
			free(*out);
		}
		if (*buf != NULL) {
			free(*buf);
		}
		return 0;
	}

	return _datetime_strftime(FORMATS_MISC, tm, out, buf_sz, buf, i);
}