diff options
author | Tom Smeding <tom@tomsmeding.com> | 2023-06-08 10:18:11 +0200 |
---|---|---|
committer | Tom Smeding <tom@tomsmeding.com> | 2023-06-08 10:18:49 +0200 |
commit | ff65df1228d77143a806a37f75f10c06ad17df79 (patch) | |
tree | ed63c22c4d17f7d15226ae9ba01a493003f8e313 | |
parent | 88bd6945554aea926123c39d9bc44cc1f525d218 (diff) |
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | main.cpp | 38 |
2 files changed, 34 insertions, 5 deletions
@@ -1,2 +1,3 @@ /obj datetags +.ccls-cache/ @@ -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++) { |