From 75e087d23ff74441387475d5bb022f293f8e7a21 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 26 Oct 2009 02:17:51 +0000 Subject: add textclock --- Makefile | 1 + aclock.c | 12 ++++-- textclock.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+), 3 deletions(-) create mode 100644 textclock.c diff --git a/Makefile b/Makefile index 49aaf91..d253627 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ # $Id$ PROG= aclock +SRCS= aclock.c textclock.c LDADD= -lcurses -lm NOMAN= diff --git a/aclock.c b/aclock.c index 1b5733e..9ac7051 100644 --- a/aclock.c +++ b/aclock.c @@ -21,6 +21,8 @@ int die = 0; int redraw = 0; enum {RED = 1, CYAN, BLUE}; +char *strtime(char *, size_t, struct tm *); + void catch(int sig) { @@ -87,7 +89,7 @@ const char INFO[] = "Copyright (c) 2002 by Antek Sawicki \n" int main(void) { - char digital_time[32]; + char digital_time[32], stime[80]; int FontHW = 2; int sXmax, sYmax, smax, hand_max, sXcen, sYcen; time_t t; @@ -151,10 +153,14 @@ main(void) attron(COLOR_PAIR(CYAN)); strftime(digital_time, sizeof(digital_time), "%d-%b-%y", ltime); - mvprintw(sYmax / 4, sXcen - 5, digital_time); + mvprintw(sYmax / 4, sXcen - strlen(digital_time) / 2, digital_time); strftime(digital_time, sizeof(digital_time), "%H:%M:%S", ltime); - mvprintw(3 * sYmax / 4, sXcen - 4, digital_time); + mvprintw(3 * sYmax / 4, sXcen - strlen(digital_time) / 2, digital_time); attroff(COLOR_PAIR(CYAN)); + strtime(stime, sizeof(stime), ltime); + attron(COLOR_PAIR(BLUE)); + mvprintw(sYmax - 1, sXcen - strlen(stime) / 2, stime); + attroff(COLOR_PAIR(BLUE)); refresh(); pause(); diff --git a/textclock.c b/textclock.c new file mode 100644 index 0000000..3d26c74 --- /dev/null +++ b/textclock.c @@ -0,0 +1,136 @@ +/* $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 +#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]; +} + +char * +strtime(char *buf, size_t sz, struct tm *now) +{ + int h = now->tm_hour; + int m = now->tm_min; + + snprintf(buf, sz, + "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 buf; +} -- cgit v1.2.3