/* $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; }