diff options
Diffstat (limited to 'main.cpp')
-rw-r--r-- | main.cpp | 38 |
1 files changed, 33 insertions, 5 deletions
@@ -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++) { |