summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Smeding <tom@tomsmeding.com>2023-06-08 10:18:11 +0200
committerTom Smeding <tom@tomsmeding.com>2023-06-08 10:18:49 +0200
commitff65df1228d77143a806a37f75f10c06ad17df79 (patch)
treeed63c22c4d17f7d15226ae9ba01a493003f8e313
parent88bd6945554aea926123c39d9bc44cc1f525d218 (diff)
Allow printing timestamps with -sHEADmaster
-rw-r--r--.gitignore1
-rw-r--r--main.cpp38
2 files changed, 34 insertions, 5 deletions
diff --git a/.gitignore b/.gitignore
index 38a0404..6b106b0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
/obj
datetags
+.ccls-cache/
diff --git a/main.cpp b/main.cpp
index 51262e9..047b556 100644
--- a/main.cpp
+++ b/main.cpp
@@ -3,17 +3,45 @@
#include <sys/time.h>
-int main() {
+static void print_usage(const char *argv0) {
+ std::cerr
+ << "Usage: " << argv0 << " [OPTIONS]\n"
+ << " -h Show this help\n"
+ << " -t Print tags in local time format\n"
+ << " -s Print tags in timestamp format\n"
+ << std::flush;
+}
+
+int main(int argc, char **argv) {
+ bool print_local_time = true;
+ for (int i = 1; i < argc; i++) {
+ if (argv[i][0] != '-') {
+ std::cerr << "Unrecognised argument: '" << argv[i] << "'" << std::endl;
+ print_usage(argv[0]);
+ return 1;
+ }
+ for (int j = 1; argv[i][j]; j++) {
+ switch (argv[i][j]) {
+ case 'h': print_usage(argv[0]); return 0;
+ case 't': print_local_time = true; break;
+ case 's': print_local_time = false; break;
+ }
+ }
+ }
+
std::string line;
while (std::getline(std::cin, line)) {
struct timeval tv;
gettimeofday(&tv, nullptr);
- const struct tm *tm = localtime(&tv.tv_sec);
-
char buffer[128];
- const ssize_t nw =
- strftime(buffer, sizeof buffer - 7, "%Y-%m-%d %H:%M:%S.", tm);
+ ssize_t nw = 0;
+ if (print_local_time) {
+ const struct tm *tm = localtime(&tv.tv_sec);
+ nw = strftime(buffer, sizeof buffer - 7, "%Y-%m-%d %H:%M:%S.", tm);
+ } else {
+ nw = snprintf(buffer, sizeof buffer - 7, "%ld.", tv.tv_sec);
+ }
int multiplier = 100000;
for (int i = 0; i < 6; i++) {