commit f943ff35b6e259dddf567eef83458e8a95a132b2
parent e9a9be8ec677b48739ed5f0a99cba29531ec11e6
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Sat,  9 Dec 2017 14:22:52 +0100
record: don't call external date process
also print in the same format to stderr.
Diffstat:
1 file changed, 14 insertions(+), 13 deletions(-)
diff --git a/pomodoro.c b/pomodoro.c
@@ -210,18 +210,19 @@ evpredicate(void)
 void
 record(FILE *f)
 {
-	const char *cmd = "date";
-	char *timestamp = malloc(128);
-	FILE *o;
-
-	o = popen(cmd, "re");
-	if (fgets(timestamp, 128, o) != NULL) {
-		fprintf(stderr, "%s", timestamp);
-		fprintf(f, "%s\n", timestamp);
-	}
-
-	free(timestamp);
-	pclose(o);
-	return;
+	char timestamp[64];
+	struct tm *tm;
+	time_t now;
+
+	if ((now = time(NULL)) == (time_t)-1)
+		err(1, "time");
+	if (!(tm = localtime(&now)))
+		err(1, "localtime");
+	if (!strftime(timestamp, sizeof(timestamp), "%a %b %e %H:%M:%S %Z %Y",
+	              tm))
+		err(1, "strftime");
+
+	fprintf(stderr, "%s\n", timestamp);
+	fprintf(f, "%s\n", timestamp);
 }