unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
blob 901a4dde8ecbfe65444950a283637f0d830c8e70 7292 bytes (raw)
name: test/parse-time.c 	 # note: path name is non-authoritative(*)

  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
 
/*
 * parse time string - user friendly date and time parser
 * Copyright © 2012 Jani Nikula
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Jani Nikula <jani@nikula.org>
 */

#include <assert.h>
#include <ctype.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "parse-time-string.h"

#define ARRAY_SIZE(a) (sizeof (a) / sizeof (a[0]))

static const char *parse_time_error_strings[] = {
    [PARSE_TIME_OK]			= "OK",
    [PARSE_TIME_ERR]			= "ERR",
    [PARSE_TIME_ERR_LIB]		= "LIB",
    [PARSE_TIME_ERR_ALREADYSET]		= "ALREADYSET",
    [PARSE_TIME_ERR_FORMAT]		= "FORMAT",
    [PARSE_TIME_ERR_DATEFORMAT]		= "DATEFORMAT",
    [PARSE_TIME_ERR_TIMEFORMAT]		= "TIMEFORMAT",
    [PARSE_TIME_ERR_INVALIDDATE]	= "INVALIDDATE",
    [PARSE_TIME_ERR_INVALIDTIME]	= "INVALIDTIME",
    [PARSE_TIME_ERR_KEYWORD]		= "KEYWORD",
};

static const char *
parse_time_strerror (unsigned int errnum)
{
    if (errnum < ARRAY_SIZE (parse_time_error_strings))
	return parse_time_error_strings[errnum];
    else
	return NULL;
}

/*
 * concat argv[start]...argv[end - 1], separating them by a single
 * space, to a malloced string
 */
static char *
concat_args (int start, int end, char *argv[])
{
    int i;
    size_t len = 1;
    char *p;

    for (i = start; i < end; i++)
	len += strlen (argv[i]) + 1;

    p = malloc (len);
    if (!p)
	return NULL;

    *p = 0;

    for (i = start; i < end; i++) {
	if (i != start)
	    strcat (p, " ");
	strcat (p, argv[i]);
    }

    return p;
}

#define DEFAULT_FORMAT "%a %b %d %T %z %Y"

static void
usage (const char *name)
{
    printf ("Usage: %s [options ...] [<date/time>]\n\n", name);
    printf (
	"Parse <date/time> and display it in given format. If <date/time> is\n"
	"not given, parse each line in stdin according to:\n\n"
	"  <date/time> [(==>|==_>|==^>|==^^>)<ignored>] [#<comment>]\n\n"
	"and produce output:\n\n"
	"  <date/time> (==>|==_>|==^>|==^^>) <time in --format=FMT> [#<comment>]\n\n"
	"preserving whitespace and comment in input. The operators ==>, ==_>,\n"
	"==^>, and ==^^> define rounding as no rounding, round down, round up\n"
	"inclusive, and round up, respectively.\n\n"

	"  -f, --format=FMT output format, FMT according to strftime(3)\n"
	"                   (default: \"%s\")\n"
	"  -r, --ref=N      use N seconds since epoch as reference time\n"
	"                   (default: now)\n"
	"  -u, --^          round result up inclusive (default: no rounding)\n"
	"  -U, --^^         round result up (default: no rounding)\n"
	"  -d, --_          round result down (default: no rounding)\n"
	"  -h, --help       print this help\n",
	DEFAULT_FORMAT);
}

struct {
    const char *operator;
    int round;
} operators[] = {
    { "==>",	PARSE_TIME_NO_ROUND },
    { "==_>",	PARSE_TIME_ROUND_DOWN },
    { "==^>",	PARSE_TIME_ROUND_UP_INCLUSIVE },
    { "==^^>",	PARSE_TIME_ROUND_UP },
};

static const char *
find_operator_in_string (char *str, char **ptr, int *round)
{
    const char *oper = NULL;
    unsigned int i;

    for (i = 0; i < ARRAY_SIZE (operators); i++) {
	char *p = strstr (str, operators[i].operator);
	if (p) {
	    if (round)
		*round = operators[i].round;
	    if (ptr)
		*ptr = p;

	    oper = operators[i].operator;
	    break;
	}
    }

    return oper;
}

static const char *
get_operator (int round)
{
    const char *oper = NULL;
    unsigned int i;

    for (i = 0; i < ARRAY_SIZE(operators); i++) {
	if (round == operators[i].round) {
	    oper = operators[i].operator;
	    break;
	}
    }

    return oper;
}

static int
parse_stdin (FILE *infile, time_t *ref, int round, const char *format)
{
    char *input = NULL;
    char result[1024];
    size_t inputsize;
    ssize_t len;
    struct tm tm;
    time_t t;
    int r;

    while ((len = getline (&input, &inputsize, infile)) != -1) {
	const char *oper;
	char *trail, *tmp;

	/* trail is trailing whitespace and (optional) comment */
	trail = strchr (input, '#');
	if (!trail)
	    trail = input + len;

	while (trail > input && isspace ((unsigned char) *(trail-1)))
	    trail--;

	if (trail == input) {
	    printf ("%s", input);
	    continue;
	}

	tmp = strdup (trail);
	if (!tmp) {
	    fprintf (stderr, "strdup() failed\n");
	    continue;
	}
	*trail = '\0';
	trail = tmp;

	/* operator */
	oper = find_operator_in_string (input, &tmp, &round);
	if (oper) {
	    *tmp = '\0';
	} else {
	    oper = get_operator (round);
	    assert (oper);
	}

	r = parse_time_string (input, &t, ref, round);
	if (!r) {
	    if (!localtime_r (&t, &tm)) {
		fprintf (stderr, "localtime_r() failed\n");
		free (trail);
		continue;
	    }

	    strftime (result, sizeof (result), format, &tm);
	} else {
	    const char *errstr = parse_time_strerror (r);
	    if (errstr)
		snprintf (result, sizeof (result), "ERROR: %s", errstr);
	    else
		snprintf (result, sizeof (result), "ERROR: %d", r);
	}

	printf ("%s%s %s%s", input, oper, result, trail);
	free (trail);
    }

    free (input);

    return 0;
}

int
main (int argc, char *argv[])
{
    int r;
    struct tm tm;
    time_t result;
    time_t now;
    time_t *nowp = NULL;
    char *argstr;
    int round = PARSE_TIME_NO_ROUND;
    char buf[1024];
    const char *format = DEFAULT_FORMAT;
    struct option options[] = {
	{ "help",	no_argument,		NULL,	'h' },
	{ "^",		no_argument,		NULL,	'u' },
	{ "^^",		no_argument,		NULL,	'U' },
	{ "_",		no_argument,		NULL,	'd' },
	{ "format",	required_argument,	NULL,	'f' },
	{ "ref",	required_argument,	NULL,	'r' },
	{ NULL, 0, NULL, 0 },
    };

    for (;;) {
	int c;

	c = getopt_long (argc, argv, "huUdf:r:", options, NULL);
	if (c == -1)
	    break;

	switch (c) {
	case 'f':
	    /* output format */
	    format = optarg;
	    break;
	case 'u':
	    round = PARSE_TIME_ROUND_UP_INCLUSIVE;
	    break;
	case 'U':
	    round = PARSE_TIME_ROUND_UP;
	    break;
	case 'd':
	    round = PARSE_TIME_ROUND_DOWN;
	    break;
	case 'r':
	    /* specify now in seconds since epoch */
	    now = (time_t) strtol (optarg, NULL, 10);
	    if (now >= (time_t) 0)
		nowp = &now;
	    break;
	case 'h':
	case '?':
	default:
	    usage (argv[0]);
	    return 1;
	}
    }

    if (optind == argc)
	return parse_stdin (stdin, nowp, round, format);

    argstr = concat_args (optind, argc, argv);
    if (!argstr)
	return 1;

    r = parse_time_string (argstr, &result, nowp, round);

    free (argstr);

    if (r) {
	const char *errstr = parse_time_strerror (r);
	if (errstr)
	    fprintf (stderr, "ERROR: %s\n", errstr);
	else
	    fprintf (stderr, "ERROR: %d\n", r);

	return r;
    }

    if (!localtime_r (&result, &tm))
	return 1;

    strftime (buf, sizeof (buf), format, &tm);
    printf ("%s\n", buf);

    return 0;
}

debug log:

solving 901a4dd ...
found 901a4dd in https://yhetil.org/notmuch.git/

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).