Home

Advertisement

Wed, Aug. 9th, 2006, 06:38 pm
Fuzzy Clock

It seems that no matter where I go, KDE is the only desktop environment with a fuzzy clock. This is unacceptable! Thus, I have implemented my own method for determining a fuzzy string based on the time. I've integrated it to some degree with the orageclock panel-plugin for Xfce4. Either way, here is a generic C method to find the fuzzy time. I release this into the public domain, because everybody should be allowed the fuzziness!

Update: There was a bug in the fuzzy clock! It is now fixed. I had twelve at the end of the hour array rather than at 0 where it belonged.

Update: Another bug. I forgot to actually use the next hour. How did I miss that one?

#include 
[Error: Irreparable invalid markup ('<time.h>') in entry. Owner must fix manually. Raw contents below.]

<p>It seems that no matter where I go, KDE is the only desktop environment with a fuzzy clock. This is unacceptable! Thus, I have implemented my own method for determining a fuzzy string based on the time. I've integrated it to some degree with the orageclock panel-plugin for Xfce4. Either way, here is a generic C method to find the fuzzy time. I release this into the public domain, because everybody should be allowed the fuzziness!</p> <p><b>Update</b>: There was a bug in the fuzzy clock! It is now fixed. I had twelve at the end of the hour array rather than at 0 where it belonged.</p> <p><b>Update</b>: Another bug. I forgot to actually use the next hour. How did I miss that one?</p> <pre> #include <time.h> #include <string.h> #include <stdlib.h> #include <stdio.h> static char *get_fuzzy_time_impl(struct tm tm) { size_t fuzzy_index, hours_index, disp_n; char *disp_s = NULL; static const size_t fuzzy_n = 12; static char *fuzzy[] = { "%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" }; static const size_t hours_n = 12; static char *hours[] = {"twelve", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven" }; fuzzy_index = (tm.tm_min - (tm.tm_min % 5)) / 5; hours_index = tm.tm_hour; if (fuzzy_index >= 7) { hours_index = (hours_index + 1) % 24; } if (hours_index >= 12) { hours_index -= 12; } disp_n = strlen(fuzzy[fuzzy_index]) + strlen(hours[hours_index]) - 1; disp_s = (char *)calloc(disp_n, sizeof(char)); if (NULL == disp_s) { return NULL; } snprintf(disp_s, disp_n, fuzzy[fuzzy_index], hours[hours_index]); disp_s[0] = (char)toupper((int)disp_s[0]); return disp_s; } static char *get_fuzzy_time() { time_t t; struct tm tm; time(&t); localtime_r(&t, &tm); return get_fuzzy_time_impl(tm); } int main(int argc, char **argv) { struct tm tm; int i, j; for (i = 0; i < 24; ++i) { for (j = 0; j < 12; ++j) { char *fuzzy_time = NULL; tm.tm_min = j * 5; tm.tm_hour = i; fuzzy_time = get_fuzzy_time_impl(tm); printf("%s,", fuzzy_time); free(fuzzy_time); } printf("\n"); } } </pre>

Wed, Nov. 1st, 2006 02:28 am (UTC)
(Anonymous): how?

so i am very excited to see that you have made a fuzzy clock for the xfce panel but i have no idea how to set it up. could you send me, or post some step by step instructions.

i am using xubuntu (if that makes any difference)

thanks,
b.
bradleysatchel@zworg.com

Advertisement