summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2023-10-15 21:45:20 +0200
committerLeah Neukirchen <leah@vuxu.org>2023-10-15 21:45:20 +0200
commit57e66f56d3044caf2d5d6904ad917968018489c1 (patch)
tree1b45d55897a08dab27b5f725d6755ef0493fd6db
parentc9fcb6debe082259160c5b641f7df7cb952cce09 (diff)
downloadmico-57e66f56d3044caf2d5d6904ad917968018489c1.tar.gz
mico-57e66f56d3044caf2d5d6904ad917968018489c1.tar.xz
mico-57e66f56d3044caf2d5d6904ad917968018489c1.zip
mico-dump: add filtering by time
-rw-r--r--mico-dump.c45
1 files changed, 38 insertions, 7 deletions
diff --git a/mico-dump.c b/mico-dump.c
index aea7138..75219d5 100644
--- a/mico-dump.c
+++ b/mico-dump.c
@@ -6,11 +6,15 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <time.h>
 
 #include <zip.h>
 
 #define MICO_HEADER "\211MiC\r\n\032\n"
 
+static int lflag;
+static int64_t fflag, tflag;
+
 zip_t *zip;
 
 struct bitreader {
@@ -102,20 +106,42 @@ mystrccpy(char *dst, char *src, int c)
 	return 0;
 }
 
-#define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))
+int64_t parsetimestamp(char *t)
+{
+	struct tm tm = { 0 };
+
+	/* 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;
+	default:
+		fprintf(stderr, "can't parse timestamp: %s\n", t);
+		exit(-1);
+	}
 
-static int tflag;
+	// tm.tm_gmtoff = 0;
+	return mktime(&tm) * 1000;
+}
+
+#define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))
 
 int
 main(int argc, char *argv[])
 {
 	int c;
-	while ((c = getopt(argc, argv, "t")) != -1) {
+	while ((c = getopt(argc, argv, "f:lt:")) != -1) {
 		switch (c) {
-		case 't': tflag = 1; break;
+		case 'l': lflag = 1; break;
+		case 'f': fflag = parsetimestamp(optarg); break;
+		case 't': tflag = parsetimestamp(optarg); break;
 		case '?':
 		usage:
-			fprintf(stderr, "usage: %s [-t] input.zip [filter...]\n", argv[0]);
+			fprintf(stderr, "usage: %s [-l] [-f TIME] [-t TIME] input.zip [filter...]\n", argv[0]);
 			exit(-1);
 		}
 	}
@@ -177,7 +203,7 @@ main(int argc, char *argv[])
 		if (!keep)
 			continue;
 
-		if (tflag) {
+		if (lflag) {
 			printf("%s\n", name);
 			continue;
 		}
@@ -219,7 +245,7 @@ main(int argc, char *argv[])
 		lenv = ntohll(lenv);
 
 		if (len != lenv) {
-			fprintf(stderr, "time and value length don't agree\n");
+			fprintf(stderr, "time and value length don't agree: %ld != %ld\n", len, lenv);
 			exit(-1);
 		}
 
@@ -234,6 +260,11 @@ main(int argc, char *argv[])
 			t += td;
 			v += vd;
 
+			if (fflag && t < fflag)
+				continue;
+			if (tflag && t > tflag)
+				break;
+
 			printf("%s %f %ld\n", name, v/10000.0, t);
 		}
 	}