summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2023-10-15 23:01:54 +0200
committerLeah Neukirchen <leah@vuxu.org>2023-10-15 23:01:54 +0200
commit03f0fa5f7cc859cb2f30f78c332be0a93515eafe (patch)
tree41657699001956ca6babcbcdfef14d6263572581
parent19a19e8e507174b339e4ddb331391775df75e385 (diff)
downloadmico-03f0fa5f7cc859cb2f30f78c332be0a93515eafe.tar.gz
mico-03f0fa5f7cc859cb2f30f78c332be0a93515eafe.tar.xz
mico-03f0fa5f7cc859cb2f30f78c332be0a93515eafe.zip
mico-dump: better time ranges
-rw-r--r--mico-dump.c79
1 files changed, 67 insertions, 12 deletions
diff --git a/mico-dump.c b/mico-dump.c
index ebca41d..ddf7a9e 100644
--- a/mico-dump.c
+++ b/mico-dump.c
@@ -1,7 +1,9 @@
 #define _XOPEN_SOURCE 700
+#define _DEFAULT_SOURCE
 
 #include <arpa/inet.h>
 #include <assert.h>
+#include <errno.h>
 #include <fnmatch.h>
 #include <getopt.h>
 #include <stdint.h>
@@ -108,26 +110,75 @@ mystrccpy(char *dst, char *src, int c)
 	return 0;
 }
 
-int64_t parsetimestamp(char *t)
+int64_t
+parsetimestamp(char *t)
 {
-	struct tm tm = { 0 };
+	int64_t ts;
+	char *end;
+
+	errno = 0;
+	ts = strtoll(t, &end, 10);
+	if (errno != 0 || *end) {
+		fprintf(stderr, "can't parse unix milli timestamp: %s\n", t);
+		exit(-1);
+	}
+
+	return ts;
+}
+
+void
+parsetimerange(char *t)
+{
+	struct tm tm_from = { .tm_mday = 1 };
+	struct tm tm_to;
 
 	/* XXX switch to iso format, treat number as raw timestamp */
 
 	switch(strlen(t)) {
-	case 4:  strptime(t, "%Y", &tm); break;
-	case 6:  strptime(t, "%Y%m", &tm); break;
-	case 8:  strptime(t, "%Y%m%d", &tm); break;
-	case 10: strptime(t, "%Y%m%d%H", &tm); break;
-	case 12: strptime(t, "%Y%m%d%H%M", &tm); break;
-	case 14: strptime(t, "%Y%m%d%H%M%S", &tm); break;
+	case 4:
+		if (!strptime(t, "%Y", &tm_from))
+			goto err;
+		tm_to = tm_from;
+		tm_to.tm_year++;
+		break;
+	case 7:
+		if (!strptime(t, "%Y-%m", &tm_from))
+			goto err;
+		tm_to = tm_from;
+		tm_to.tm_mon++;
+		break;
+	case 10:
+		if (!strptime(t, "%Y-%m-%d", &tm_from))
+			goto err;
+		tm_to = tm_from;
+		tm_to.tm_mday++;
+		break;
+	case 13:
+		if (!strptime(t, "%Y-%m-%dT%H", &tm_from))
+			goto err;
+		tm_to = tm_from;
+		tm_to.tm_hour++;
+		break;
+	case 16:
+		if (!strptime(t, "%Y-%m-%dT%H:%M", &tm_from))
+			goto err;
+		tm_to = tm_from;
+		tm_to.tm_min++;
+		break;
+	case 19:
+		if (!strptime(t, "%Y-%m-%dT%H:%M:%S", &tm_from))
+			goto err;
+		tm_to = tm_from;
+		tm_to.tm_sec++;
+		break;
 	default:
-		fprintf(stderr, "can't parse timestamp: %s\n", t);
+	err:
+		fprintf(stderr, "can't recognize timestamp: %s\n", t);
 		exit(-1);
 	}
 
-	// tm.tm_gmtoff = 0;
-	return mktime(&tm) * 1000;
+	fflag = timegm(&tm_from) * 1000;
+	tflag = (timegm(&tm_to) * 1000) - 1;
 }
 
 #define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))
@@ -136,11 +187,12 @@ int
 main(int argc, char *argv[])
 {
 	int c;
-	while ((c = getopt(argc, argv, "f:lt:")) != -1) {
+	while ((c = getopt(argc, argv, "f:lt:r:")) != -1) {
 		switch (c) {
 		case 'l': lflag = 1; break;
 		case 'f': fflag = parsetimestamp(optarg); break;
 		case 't': tflag = parsetimestamp(optarg); break;
+		case 'r': parsetimerange(optarg); break;
 		case '?':
 		usage:
 			fprintf(stderr, "usage: %s [-l] [-f TIME] [-t TIME] input.zip [filter...]\n", argv[0]);
@@ -151,6 +203,9 @@ main(int argc, char *argv[])
 	if (argc - optind < 1)
 		goto usage;
 
+	setenv("TZ", "", 1);
+	tzset();
+
 	zip = zip_open(argv[optind], ZIP_RDONLY, 0);
 
 	int entries = zip_get_num_entries(zip, 0);