summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2009-10-26 02:17:51 +0000
committerDimitri Sokolyuk <demon@dim13.org>2009-10-26 02:17:51 +0000
commit75e087d23ff74441387475d5bb022f293f8e7a21 (patch)
tree5b251a8cb7d5a9dcb6d3c5607d6b6d7b78856f45
parentfb5789662243a4353ae31ada6a4396071b669d60 (diff)
add textclock
-rw-r--r--Makefile1
-rw-r--r--aclock.c12
-rw-r--r--textclock.c136
3 files changed, 146 insertions, 3 deletions
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 <tenox@tenox.tc>\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 <demon@dim13.org>
+ *
+ * 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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+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;
+}