Add simple_time.c app
[cmccabe-bin] / simple_time.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <time.h>
4
5 /* Prints a time on a single line of the console.
6  * I wrote this app so that I could measure times by repeatedly pressing
7  * return.
8  *
9  * Colin McCabe
10  */
11 void strip_newlines(char *str)
12 {
13         size_t i;
14         size_t str_len = strlen(str);
15
16         for (i = 0; i < str_len; i++) {
17                 if (str[i] == '\n')
18                         str[i] = ' ';
19         }
20 }
21
22 int main(void)
23 {
24         while (1) {
25                 char str[27];
26                 size_t i, len;
27                 time_t t = time(NULL);
28                 struct tm *tm = localtime(&t);
29
30                 str[0] = '\0';
31                 asctime_r(tm, str);
32                 len = strlen(str);
33                 printf("\r");
34                 strip_newlines(str);
35                 printf("%s", str);
36                 fflush(stdout);
37                 sleep(1);
38         }
39
40         return 0;
41 }