--- bug4119/panel-plugin/datetime.c 2008/05/28 21:33:20 1.1 +++ bug4119/panel-plugin/datetime.c 2008/05/29 12:06:16 @@ -39,6 +39,15 @@ #define USE_GTK_TOOLTIP_API GTK_CHECK_VERSION(2,12,0) #define DATETIME_MAX_STRLEN 256 + +/* + * Compute the difference a-b between two GTimeVal structs. + * Returns the result as a gdouble in milliseconds. + */ +#define DATETIME_DIFFTIME_MILLISECONDS(a, b) \ + ((gdouble) ((a).tv_sec - (b).tv_sec ) * 1000.0 + \ + (gdouble) ((a).tv_usec - (b).tv_usec) / 1000.0) + /* * Get date/time string */ @@ -72,7 +81,7 @@ .tm_hour = 0, .tm_mday = 1, .tm_mon = 0, - .tm_year = 0, + .tm_year = 100, .tm_wday = 0, .tm_yday = 0, .tm_isdst = 0 @@ -106,6 +115,8 @@ gchar *utf8str; struct tm *current; t_datetime *datetime; + GTimeVal next_update_time; /* time of next update */ + guint update_interval; /* milliseconds to next update */ if (data == NULL) { @@ -114,6 +125,12 @@ datetime = (t_datetime*)data; + /* stop timer */ + if (datetime->timeout_id) + { + g_source_remove(datetime->timeout_id); + } + g_get_current_time(&timeval); current = localtime((time_t *)&timeval.tv_sec); if (datetime->date_format != NULL && GTK_IS_LABEL(datetime->date_label)) @@ -180,6 +197,21 @@ break; } + /* + * Compute the time to the next update and start the timer. + * The calculation finds the next larger integral multiple of the given update interval. + * This results in the next update occurring on the second or minute + * when the update interval is 1 or 60 seconds, respectively. + */ + if (datetime->update_interval == 1) + next_update_time.tv_sec = timeval.tv_sec + 1; /* this branch is an optimization */ + else + next_update_time.tv_sec = timeval.tv_sec + + datetime->update_interval - timeval.tv_sec % datetime->update_interval; + next_update_time.tv_usec = 0; + update_interval = DATETIME_DIFFTIME_MILLISECONDS(next_update_time, timeval); + datetime->timeout_id = g_timeout_add(update_interval, datetime_update, datetime); + return TRUE; } @@ -450,19 +482,14 @@ gtk_widget_show(GTK_WIDGET(datetime->time_label)); } - if (datetime->timeout_id) - { - g_source_remove(datetime->timeout_id); - } - if (datetime_format_has_seconds(datetime->date_format) || datetime_format_has_seconds(datetime->time_format)) { - datetime->timeout_id = g_timeout_add(1000, datetime_update, datetime); + datetime->update_interval = 1; } else { - datetime->timeout_id = g_timeout_add(10000, datetime_update, datetime); + datetime->update_interval = 60; } } @@ -616,6 +643,7 @@ gtk_widget_show_all(datetime->button); /* set date and time labels */ + datetime->timeout_id = 0; datetime_update(datetime); return datetime; --- bug4119/panel-plugin/datetime.h 2008/05/29 09:03:23 1.1 +++ bug4119/panel-plugin/datetime.h 2008/05/29 10:49:20 @@ -46,6 +46,7 @@ GtkWidget *date_label; GtkWidget *time_label; GtkTooltips *tips; + guint update_interval; /* time between updates in seconds */ guint timeout_id; /* settings */