summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--textclock/textclock.c21
1 files changed, 16 insertions, 5 deletions
diff --git a/textclock/textclock.c b/textclock/textclock.c
index dd48cfd..e10c616 100644
--- a/textclock/textclock.c
+++ b/textclock/textclock.c
@@ -16,6 +16,8 @@
*/
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include <time.h>
char *approx[] = {
@@ -117,15 +119,21 @@ time_tod(int h, int m)
return tod[t];
}
-int
-time_print(FILE *fd, int h, int m)
+char *
+strtime(struct tm *now)
{
- return fprintf(fd,
- "The time is now, %s%s%s%s.\n",
+ char buf[80];
+ int h = now->tm_hour;
+ int m = now->tm_min;
+
+ snprintf(buf, sizeof(buf),
+ "The time is now, %s%s%s%s.",
time_approx(h, m),
time_min(h, m),
time_hour(h, m),
time_tod(h, m));
+
+ return strdup(buf);
}
int
@@ -133,12 +141,15 @@ main()
{
time_t t;
struct tm *tm;
+ char *s;
time(&t);
tm = localtime(&t);
+ s = strtime(tm);
fprintf(stdout, "Content-type: %s\r\n\r\n", "text/plain");
- time_print(stdout, tm->tm_hour, tm->tm_min);
+ fprintf(stdout, "%s\n", s);
+ free(s);
return 0;
}