summaryrefslogtreecommitdiffstats
path: root/src/datetime.c
blob: 8597ea2ce7e61b4f969e88e3e4c7455d242d912a (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
/*
 * Copyright (C) 2021  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

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

static const char *DATETIME_FMTS_[] = {
#include "datetime-formats.c"
	NULL
};

static char *
datetime_concat_args(int argc, const char *argv[])
{
	int   buf_l;
	int   i;
	char *buf;
	int   buf_i;
	int   j;

	buf_l = 0;
	for (i = 0; i < argc; ++i) {
		buf_l += strlen(argv[i]) + 1;
	}

	buf = calloc(buf_l, sizeof(*buf));
	if (buf == NULL) {
		fprintf(stderr, "Failed to allocate buffer: %s\n",
				strerror(errno));
		return NULL;
	}

	buf_i = 0;
	for (i = 0; i < argc; ++i) {
		for (j = 0; argv[i][j] != '\0'; ++j) {
			buf[buf_i++] = argv[i][j];
		}
		buf[buf_i++] = ' ';
	}
	buf[--buf_i] = '\0';

	return buf;
}

int
datetime_parse(int argc, const char *argv[], time_t *arg_sec)
{
	char      *buf;
	int        i;
	char      *end;
	struct tm  arg_tm;
	time_t     now_sec;
	struct tm *now_tm;

	buf = datetime_concat_args(argc, argv);
	if (buf == NULL) {
		return -1;
	}

	arg_tm.tm_mday = INT_MIN;  /* Sentinel */
	arg_tm.tm_sec  = 0;        /* Default */
	arg_tm.tm_isdst = -1;

	for (i = 0; DATETIME_FMTS_[i] != NULL; ++i) {
		/* printf("%s =~ %s\n", buf, DATETIME_FMTS_[i]); */
		end = strptime(buf, DATETIME_FMTS_[i], &arg_tm);
		if (end != NULL && *end == '\0') {
			goto found;
		}
	}

	free(buf);
	fprintf(stderr, "Unknown date format\n");
	return -1;

 found:
	free(buf);

	/* TODO: Support %a-only dates and optional years */
	if (arg_tm.tm_mday == INT_MIN) {
		/* No date specified; try today. */
		now_sec = time(NULL);
		now_tm = localtime(&now_sec);
		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_sec = mktime(&arg_tm);
		if (*arg_sec <= mktime(now_tm)) {
			/* Specified time already happened today; use tomorrow.
			 * Adding the number of seconds in a day is a shortcut
			 * that ignores leap seconds.  One better method would
			 * be to increment tm_mday % days in tm_mon, etc. */
			*arg_sec += 60 * 60 * 24;
			now_tm = localtime(arg_sec);
			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_sec = mktime(&arg_tm);
		}
	} else {
		*arg_sec = mktime(&arg_tm);
	}

	printf("%d-%02d-%02dT%02d:%02d:%02d\n", arg_tm.tm_year + 1900,
			arg_tm.tm_mon + 1, arg_tm.tm_mday,
			arg_tm.tm_hour, arg_tm.tm_min, arg_tm.tm_sec);

	return 0;
}