summaryrefslogtreecommitdiff
path: root/textclock.c
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 /textclock.c
parentfb5789662243a4353ae31ada6a4396071b669d60 (diff)
add textclock
Diffstat (limited to 'textclock.c')
-rw-r--r--textclock.c136
1 files changed, 136 insertions, 0 deletions
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;
+}