Blob src/fuzzyclock.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
#include <stdio.h> #include <stdlib.h> #include "fuzzyclock.h" char* HOUR_NAMES[] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", }; char* FUZZY_MSG[] = { "%s o' clock", "five past %s", "ten past %s", "quarter past %s", "twenty past %s", "twenty five past %s", "half past %s", "twenty five to %s", "twenty to %s", "quarter to %s", "ten to %s", "five to %s", }; char* const get_hour_string(int hour) { if (hour < 1) hour = 12; return HOUR_NAMES[(hour - 1) % 12]; } void get_fuzzy_time(struct tm* clock, char* buffer) { int index = 0; int hour = clock->tm_hour; int min = clock->tm_min; if (min > 2) { index = ((min - 3) / 5) + 1; } if (index > 6) { hour++; } char* timestr = FUZZY_MSG[index % 12]; char* hourname = get_hour_string(hour); snprintf(buffer, MAX_MSG_SIZE, timestr, hourname); } |