Skip to content

Instantly share code, notes, and snippets.

@llllllllll
Last active December 13, 2016 19:51
Show Gist options
  • Save llllllllll/159cad736b928cbe64fa00bd38955968 to your computer and use it in GitHub Desktop.
Save llllllllll/159cad736b928cbe64fa00bd38955968 to your computer and use it in GitHub Desktop.
$ ./ts < ts.c
2016-12-13 14:52:33-0500 #include <stdlib.h>
2016-12-13 14:52:33-0500 #include <stdio.h>
2016-12-13 14:52:33-0500 #include <time.h>
2016-12-13 14:52:33-0500
2016-12-13 14:52:33-0500 int main(void) {
2016-12-13 14:52:33-0500 char c;
2016-12-13 14:52:33-0500 time_t t;
2016-12-13 14:52:33-0500 struct tm *tm;
2016-12-13 14:52:33-0500 char buf[BUFSIZ];
2016-12-13 14:52:33-0500 while ((c = getchar()) != EOF) {
2016-12-13 14:52:33-0500 if (c == '\n') {
2016-12-13 14:52:33-0500 t = time(NULL);
2016-12-13 14:52:33-0500 tm = localtime(&t);
2016-12-13 14:52:33-0500 if (!tm) {
2016-12-13 14:52:33-0500 perror("localtime");
2016-12-13 14:52:33-0500 return EXIT_FAILURE;
2016-12-13 14:52:33-0500 }
2016-12-13 14:52:33-0500 if (!strftime(buf, BUFSIZ, "%F %T%z%t", tm)) {
2016-12-13 14:52:33-0500 perror("strftime");
2016-12-13 14:52:33-0500 return EXIT_FAILURE;
2016-12-13 14:52:33-0500 }
2016-12-13 14:52:33-0500 if (fputs(buf, stderr) < 0) {
2016-12-13 14:52:33-0500 perror("fputs");
2016-12-13 14:52:33-0500 return EXIT_FAILURE;
2016-12-13 14:52:33-0500 }
2016-12-13 14:52:33-0500 }
2016-12-13 14:52:33-0500 if (putchar(c) == EOF) {
2016-12-13 14:52:33-0500 perror("putchar");
2016-12-13 14:52:33-0500 return EXIT_FAILURE;
2016-12-13 14:52:33-0500 }
2016-12-13 14:52:33-0500 }
2016-12-13 14:52:33-0500 return 0;
2016-12-13 14:52:33-0500 }
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void) {
char c;
time_t t;
struct tm *tm;
char buf[BUFSIZ];
while ((c = getchar()) != EOF) {
if (c == '\n') {
t = time(NULL);
tm = localtime(&t);
if (!tm) {
perror("localtime");
return EXIT_FAILURE;
}
if (!strftime(buf, BUFSIZ, "%F %T%z%t", tm)) {
perror("strftime");
return EXIT_FAILURE;
}
if (fputs(buf, stderr) < 0) {
perror("fputs");
return EXIT_FAILURE;
}
}
if (putchar(c) == EOF) {
perror("putchar");
return EXIT_FAILURE;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment