From 2b0586e8c2f6d705a02e4103a12b9a2557f0e69f Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Wed, 9 Sep 2009 16:49:11 +0000 Subject: text clock --- textclock/Makefile | 8 +++ textclock/textclock.c | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 textclock/Makefile create mode 100644 textclock/textclock.c diff --git a/textclock/Makefile b/textclock/Makefile new file mode 100644 index 0000000..c1ff3a4 --- /dev/null +++ b/textclock/Makefile @@ -0,0 +1,8 @@ +# $Id$ + +PROG= textclock +CFLAGS+= -Wall +NOMAN= +BINDIR= /var/www/cgi-bin + +.include diff --git a/textclock/textclock.c b/textclock/textclock.c new file mode 100644 index 0000000..dd48cfd --- /dev/null +++ b/textclock/textclock.c @@ -0,0 +1,144 @@ +/* $Id$ */ +/* + * Copyright (c) 2009 Dimitri Sokolyuk + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include + +char *approx[] = { + [0] = "exactly", + [1] = "just after", + [2] = "a little after", + [3] = "almost", + [4] = "exactly" +}; + +char *min[] = { + [0] = "", + [5] = " five past", + [10] = " ten past", + [15] = " quarter past", + [20] = " twenty past", + [25] = " twenty-five past", + [30] = " half past", + [35] = " twenty-five to", + [40] = " twenty to", + [45] = " quarter to", + [50] = " ten to", + [55] = " five to" +}; + +char *hour[] = { + [0] = " midnight", + [1] = " one", + [2] = " two", + [3] = " three", + [4] = " four", + [5] = " five", + [6] = " six", + [7] = " seven", + [8] = " eight", + [9] = " nine", + [10] = " ten", + [11] = " eleven", + [12] = " twelve" +}; + +char *tod[] = { + [0] = "", + [1] = ", in the evening", + [2] = ", in the afternoon", + [3] = "", + [4] = ", in the morning" +}; + +char * +time_approx(int h, int m) +{ + return approx[m % 5]; +} + +char * +time_min(int h, int m) +{ + int mm; + + mm = m / 5; + if ((m % 5) > 2) + ++mm; + mm *= 5; + + return min[mm % 60]; +} + +char * +time_hour(int h, int m) +{ + if (m >= 32) + ++h; + h %= 24; + if (h > 12) + h -= 12; + + return hour[h]; +} + +char * +time_tod(int h, int m) +{ + int hh = h; + int t; + + if (m > 58) + ++hh; + if (hh == 24) + t = 0; + else if (hh > 17) + t = 1; + else if (hh > 11) + t = 2; + else if (hh == 0 && m < 33) + t = 3; + else + t = 4; + return tod[t]; +} + +int +time_print(FILE *fd, int h, int m) +{ + return fprintf(fd, + "The time is now, %s%s%s%s.\n", + time_approx(h, m), + time_min(h, m), + time_hour(h, m), + time_tod(h, m)); +} + +int +main() +{ + time_t t; + struct tm *tm; + + time(&t); + tm = localtime(&t); + + fprintf(stdout, "Content-type: %s\r\n\r\n", "text/plain"); + time_print(stdout, tm->tm_hour, tm->tm_min); + + return 0; +} -- cgit v1.2.3