From: Hugo Villeneuve Date: Fri, 10 Sep 2010 04:24:25 +0000 (+0000) Subject: Combined common code to get time into a function X-Git-Tag: hvclock-1.0.1~4 X-Git-Url: http://gitweb.hugovil.com/?a=commitdiff_plain;h=c41db728003ab6c595549f207943d013b0858262;p=dockapps%2Fhvclock.git Combined common code to get time into a function Removed leading zero in display of day of month --- diff --git a/src/clock.c b/src/clock.c index ef835ad..23f8dc3 100644 --- a/src/clock.c +++ b/src/clock.c @@ -205,6 +205,18 @@ draw_clock_background(GtkWidget *clock, cairo_t *cr) cairo_restore(cr); } +static struct tm * +clock_get_time(void) +{ + time_t now; /* Current system time */ + + /* Get the current time */ + now = time(NULL); + + /* Format and print the time, "ddd yyyy-mm-dd hh:mm:ss zzz" */ + return localtime(&now); +} + static void draw_clock_hands(GtkWidget *clock, cairo_t *cr) { @@ -213,14 +225,9 @@ draw_clock_hands(GtkWidget *clock, cairo_t *cr) double radius, length; int hours, minutes, seconds; double sin_x, cos_y; - time_t now; /* Current system time */ struct tm *ts; - /* Get the current time */ - now = time(NULL); - - /* Format and print the time, "ddd yyyy-mm-dd hh:mm:ss zzz" */ - ts = localtime(&now); + ts = clock_get_time(); center_x = clock->allocation.width / 2; center_y = clock->allocation.height / 2; @@ -301,7 +308,6 @@ draw_calendar(GtkWidget *clock, cairo_t *cr) char str[32]; double x, y; cairo_text_extents_t extents; - time_t now; /* Current system time */ struct tm *ts; center_x = clock->allocation.width / 2; @@ -314,11 +320,7 @@ draw_calendar(GtkWidget *clock, cairo_t *cr) cairo_set_font_size(cr, CALENDAR_FONTS_SIZE); cairo_set_source_rgb(cr, 0, 0, 0); - /* Get the current time */ - now = time(NULL); - - /* Format and print the time, "ddd yyyy-mm-dd hh:mm:ss zzz" */ - ts = localtime(&now); + ts = clock_get_time(); /* Weekday */ strftime(str, 256, "%A", ts); @@ -338,6 +340,17 @@ draw_calendar(GtkWidget *clock, cairo_t *cr) /* Day of month */ cairo_set_font_size(cr, 30); strftime(str, 256, "%d", ts); + + fprintf(stderr, "TIME=[%s]\n", str); + + if (str[0] == '0') { + /* Remove leading zero */ + str[0] = str[1]; + str[1] = '\0'; + } + + fprintf(stderr, "TIME=[%s]\n", str); + /* Get text dimensions */ cairo_text_extents(cr, str, &extents); x = center_x - extents.width / 2;