Skip to content
This repository has been archived by the owner on Dec 26, 2018. It is now read-only.

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
caio committed Mar 27, 2012
1 parent 0231a1c commit fe842a7
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions fuzzyclock.c
@@ -0,0 +1,63 @@
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define BUFSIZE 30

char* HOUR_NAMES[] = {
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
"twelve",
};

char* FUZZY_MSG[] = {
"%s o' clock\n",
"five past %s\n",
"ten past %s\n",
"quarter past %s\n",
"twenty past %s\n",
"twenty five past %s\n",
"half past %s\n",
"twenty five to %s\n",
"twenty to %s\n",
"quarter to %s\n",
"ten to %s\n",
"five to %s\n",
};

void get_fuzzy_time(time_t timer, char* buffer) {
struct tm* clock = localtime(&timer);

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];
char* hourname = HOUR_NAMES[(hour - 1) % 12];

snprintf(buffer, BUFSIZE, timestr, hourname);
}


int main(int argc, char const* argv[])
{
char msg[BUFSIZE];
get_fuzzy_time(time(NULL), msg);
printf(msg);
return 0;
}

0 comments on commit fe842a7

Please sign in to comment.