test-date.c 1.48 KB
Newer Older
1 2
#include "cache.h"

Jeff King's avatar
Jeff King committed
3 4 5 6 7 8
static const char *usage_msg = "\n"
"  test-date show [time_t]...\n"
"  test-date parse [date]...\n"
"  test-date approxidate [date]...\n";

static void show_dates(char **argv, struct timeval *now)
9
{
10
	struct strbuf buf = STRBUF_INIT;
Jeff King's avatar
Jeff King committed
11 12 13

	for (; *argv; argv++) {
		time_t t = atoi(*argv);
14 15
		show_date_relative(t, 0, now, &buf);
		printf("%s -> %s\n", *argv, buf.buf);
Jeff King's avatar
Jeff King committed
16
	}
17
	strbuf_release(&buf);
Jeff King's avatar
Jeff King committed
18
}
19

Jeff King's avatar
Jeff King committed
20 21
static void parse_dates(char **argv, struct timeval *now)
{
22 23
	struct strbuf result = STRBUF_INIT;

Jeff King's avatar
Jeff King committed
24
	for (; *argv; argv++) {
25
		unsigned long t;
Jeff King's avatar
Jeff King committed
26
		int tz;
27

28 29 30
		strbuf_reset(&result);
		parse_date(*argv, &result);
		if (sscanf(result.buf, "%lu %d", &t, &tz) == 2)
Jeff King's avatar
Jeff King committed
31
			printf("%s -> %s\n",
32
			       *argv, show_date(t, tz, DATE_MODE(ISO8601)));
Jeff King's avatar
Jeff King committed
33 34
		else
			printf("%s -> bad\n", *argv);
Jeff King's avatar
Jeff King committed
35
	}
36
	strbuf_release(&result);
Jeff King's avatar
Jeff King committed
37
}
38

Jeff King's avatar
Jeff King committed
39 40 41 42 43
static void parse_approxidate(char **argv, struct timeval *now)
{
	for (; *argv; argv++) {
		time_t t;
		t = approxidate_relative(*argv, now);
44
		printf("%s -> %s\n", *argv, show_date(t, 0, DATE_MODE(ISO8601)));
Jeff King's avatar
Jeff King committed
45 46 47 48 49 50 51 52 53 54 55 56
	}
}

int main(int argc, char **argv)
{
	struct timeval now;
	const char *x;

	x = getenv("TEST_DATE_NOW");
	if (x) {
		now.tv_sec = atoi(x);
		now.tv_usec = 0;
57
	}
Jeff King's avatar
Jeff King committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71
	else
		gettimeofday(&now, NULL);

	argv++;
	if (!*argv)
		usage(usage_msg);
	if (!strcmp(*argv, "show"))
		show_dates(argv+1, &now);
	else if (!strcmp(*argv, "parse"))
		parse_dates(argv+1, &now);
	else if (!strcmp(*argv, "approxidate"))
		parse_approxidate(argv+1, &now);
	else
		usage(usage_msg);
72 73
	return 0;
}