Skip to content

Instantly share code, notes, and snippets.

@ph1ee
Last active May 15, 2019 09:03
Show Gist options
  • Save ph1ee/05d641faf7fb5ef9f10207392c743ec9 to your computer and use it in GitHub Desktop.
Save ph1ee/05d641faf7fb5ef9f10207392c743ec9 to your computer and use it in GitHub Desktop.
print time periodically
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
char outstr[200];
int usec = 100;
if (argc > 1) {
usec = atoi(argv[1]);
}
while (usec < 0 || usleep(usec) == 0) {
time_t t = time(NULL);
struct tm *tmp = localtime(&t);
if (tmp == NULL) {
perror("localtime");
exit(EXIT_FAILURE);
}
if (strftime(outstr, sizeof(outstr), "%T", tmp)) {
fprintf(stdout, "%s\n", outstr);
}
}
exit(EXIT_FAILURE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment