From 2767024fce1a6afd8c8e300b47adbb477627a60d Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 26 Oct 2009 02:10:26 +0000 Subject: replace time_print() with strtime() --- textclock/textclock.c | 21 ++++++++++++++++----- 1 file 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 +#include +#include #include 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; } -- cgit v1.2.3