summaryrefslogtreecommitdiff
path: root/vtime.c
blob: 782f1711bff23856918e545cde1db3f33339838e (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
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>

#define VTIME_IMPL
#include "vtime.h"

void
help()
{
	fprintf(stdout, "vtime [FORMAT]\n\
\t[YYYY/yy].[mm].dd            Sets the date. Can set only the day/month.\n\
\t                             Instead of dot(.) coult be any non-numeric character.\n\
\t+/-[k](d/w/m/y)              Add/subtract k units from the date. k is implicitly 1\n\
\tt[his]/n[ext] Wday\n\
\tl[ast]/p[rev] Wday           Go to next/previous week day. Wday is the week day prefix.\n\
\tb[egining]\n\ts[tart]\n\tf[irst]\n\
\te[nd] w[eek]/m[onth]/y[ear]  Go to first/last day in week/month/year.\n\
");
}

int
main(int argc, char **argv)
{
	int i;
	time_t now = time(NULL);
	struct tm out = *(localtime(&now));
	if (argc == 1) {
		help();
		return 0;
	}

	char *fmt, *pnt;
	size_t len = 0;
	for (i = 1; i < argc; i++) {
		len += 1 + strlen(argv[i]);
	}

	fmt = calloc(len, sizeof(*fmt));
	if (fmt == NULL) {

		fprintf(stderr, "Failed malloc\n");
		return 1;
	}

	for (i = 1, pnt = fmt; i < argc; i++) {
		pnt = stpcpy(pnt, argv[i]);
		if (i != argc-1)
			*(pnt++) = ' ';
	}

	if (vtime(&out, fmt, 0)) {
		fprintf(stderr, "Wrong format\n");
		return 1;
	}
	fprintf(stdout, "%s", asctime(&out));
	return 0;
}