[Xfce4-commits] <xfce4-weather-plugin:master> Summary window: Add date of last update to details tab (bug #9499).

Harald Judt noreply at xfce.org
Wed Dec 19 11:32:02 CET 2012


Updating branch refs/heads/master
         to 15950e3ecd0c9f8df1ff2e71af9c4196b434884f (commit)
       from 2f811dd18c8337992281eaa0059018663d9eccf0 (commit)

commit 15950e3ecd0c9f8df1ff2e71af9c4196b434884f
Author: Harald Judt <h.judt at gmx.at>
Date:   Sun Dec 16 12:02:59 2012 +0100

    Summary window: Add date of last update to details tab (bug #9499).

 panel-plugin/weather-data.c    |   25 ++++++++++++++++
 panel-plugin/weather-data.h    |    4 ++
 panel-plugin/weather-debug.c   |   41 ++++++++------------------
 panel-plugin/weather-debug.h   |    2 -
 panel-plugin/weather-summary.c |   60 +++++++++++++++++----------------------
 panel-plugin/weather.c         |   52 ++++++++++------------------------
 6 files changed, 84 insertions(+), 100 deletions(-)

diff --git a/panel-plugin/weather-data.c b/panel-plugin/weather-data.c
index c23eec1..4043030 100644
--- a/panel-plugin/weather-data.c
+++ b/panel-plugin/weather-data.c
@@ -75,6 +75,31 @@ string_to_double(const gchar *str,
 }
 
 
+gchar *
+format_date(const time_t date_t,
+            gchar *format,
+            gboolean local)
+{
+    struct tm *tm;
+    time_t t = date_t;
+    gchar buf[40];
+    size_t size;
+
+    if (G_LIKELY(local))
+        tm = localtime(&t);
+    else
+        tm = gmtime(&t);
+
+    /* A year <= 1970 means date has not been set */
+    if (G_UNLIKELY(tm == NULL) || tm->tm_year <= 70)
+        return g_strdup("-");
+    if (format == NULL)
+        format = "%Y-%m-%d %H:%M:%S";
+    size = strftime(buf, 40, format, tm);
+    return (size ? g_strdup(buf) : g_strdup("-"));
+}
+
+
 /* check whether timeslice is interval or point data */
 gboolean
 timeslice_is_interval(xml_time *timeslice)
diff --git a/panel-plugin/weather-data.h b/panel-plugin/weather-data.h
index 7eff287..d0115b8 100644
--- a/panel-plugin/weather-data.h
+++ b/panel-plugin/weather-data.h
@@ -90,6 +90,10 @@ typedef struct {
 gdouble string_to_double(const gchar *str,
                          gdouble backup);
 
+gchar *format_date(const time_t t,
+                   gchar *format,
+                   gboolean local);
+
 gboolean timeslice_is_interval(xml_time *timeslice);
 
 gchar *get_data(const xml_time *timeslice,
diff --git a/panel-plugin/weather-debug.c b/panel-plugin/weather-debug.c
index 3fe0677..5fb6b50 100644
--- a/panel-plugin/weather-debug.c
+++ b/panel-plugin/weather-debug.c
@@ -29,8 +29,6 @@
 
 #include "weather-debug.h"
 
-#define INVALID_TIME "INVALID"
-
 #define YESNO(bool) ((bool) ? "yes" : "no")
 
 
@@ -110,19 +108,6 @@ weather_debug_real(const gchar *log_domain,
 
 
 gchar *
-weather_debug_strftime_t(const time_t t)
-{
-    struct tm tm;
-    gchar str[20];
-    size_t size;
-
-    tm = *localtime(&t);
-    size = strftime(str, 20, "%Y-%m-%d %H:%M:%S", &tm);
-    return (size ? g_strdup(str) : g_strdup(INVALID_TIME));
-}
-
-
-gchar *
 weather_dump_geolocation(const xml_geolocation *geo)
 {
     gchar *out;
@@ -230,10 +215,10 @@ weather_dump_astrodata(const xml_astro *astro)
     if (!astro)
         return g_strdup("No astronomical data.");
 
-    sunrise = weather_debug_strftime_t(astro->sunrise);
-    sunset = weather_debug_strftime_t(astro->sunset);
-    moonrise = weather_debug_strftime_t(astro->moonrise);
-    moonset = weather_debug_strftime_t(astro->moonset);
+    sunrise = format_date(astro->sunrise, "%c", TRUE);
+    sunset = format_date(astro->sunset, "%c", TRUE);
+    moonrise = format_date(astro->moonrise, "%c", TRUE);
+    moonset = format_date(astro->moonset, "%c", TRUE);
 
     out = g_strdup_printf("Astronomical data:\n"
                           "  --------------------------------------------\n"
@@ -349,8 +334,8 @@ weather_dump_timeslice(const xml_time *timeslice)
         return g_strdup("No timeslice data.");
 
     out = g_string_sized_new(512);
-    start = weather_debug_strftime_t(timeslice->start);
-    end = weather_debug_strftime_t(timeslice->end);
+    start = format_date(timeslice->start, "%c", TRUE);
+    end = format_date(timeslice->end, "%c", TRUE);
     is_interval = (gboolean) strcmp(start, end);
     loc = weather_dump_location((timeslice) ? timeslice->location : NULL,
                                 is_interval);
@@ -411,15 +396,15 @@ weather_dump_plugindata(const plugin_data *data)
     gchar *next_astro_update, *next_weather_update, *next_conditions_update;
     gchar *next_wakeup, *result;
 
-    last_astro_update = weather_debug_strftime_t(data->astro_update->last);
-    last_weather_update = weather_debug_strftime_t(data->weather_update->last);
+    last_astro_update = format_date(data->astro_update->last, "%c", TRUE);
+    last_weather_update = format_date(data->weather_update->last, "%c", TRUE);
     last_conditions_update =
-        weather_debug_strftime_t(data->conditions_update->last);
-    next_astro_update = weather_debug_strftime_t(data->astro_update->next);
-    next_weather_update = weather_debug_strftime_t(data->weather_update->next);
+        format_date(data->conditions_update->last, "%c", TRUE);
+    next_astro_update = format_date(data->astro_update->next, "%c", TRUE);
+    next_weather_update = format_date(data->weather_update->next, "%c", TRUE);
     next_conditions_update =
-        weather_debug_strftime_t(data->conditions_update->next);
-    next_wakeup = weather_debug_strftime_t(data->next_wakeup);
+        format_date(data->conditions_update->next, "%c", TRUE);
+    next_wakeup = format_date(data->next_wakeup, "%c", TRUE);
 
     out = g_string_sized_new(1024);
     g_string_assign(out, "xfce_weatherdata:\n");
diff --git a/panel-plugin/weather-debug.h b/panel-plugin/weather-debug.h
index 3034b76..5d71c5e 100644
--- a/panel-plugin/weather-debug.h
+++ b/panel-plugin/weather-debug.h
@@ -58,8 +58,6 @@ void weather_debug_real(const gchar *log_domain,
                         const gchar *format,
                         ...);
 
-gchar *weather_debug_strftime_t(const time_t t);
-
 gchar *weather_dump_geolocation(const xml_geolocation *geo);
 
 gchar *weather_dump_place(const xml_place *place);
diff --git a/panel-plugin/weather-summary.c b/panel-plugin/weather-summary.c
index 8fce6f3..d2b2a5b 100644
--- a/panel-plugin/weather-summary.c
+++ b/panel-plugin/weather-summary.c
@@ -278,18 +278,6 @@ weather_summary_get_logo(plugin_data *data)
 }
 
 
-static gchar *
-format_date(time_t t)
-{
-    struct tm *tm;
-    gchar buf[80];
-
-    tm = localtime(&t);
-    strftime(buf, 80, "%c", tm);
-    return g_strdup(buf);
-}
-
-
 static GtkWidget *
 create_summary_tab(plugin_data *data)
 {
@@ -301,11 +289,9 @@ create_summary_tab(plugin_data *data)
     GdkColor lnk_color;
     xml_time *conditions;
     const gchar *unit;
-    struct tm *start_tm, *end_tm, *point_tm;
-    struct tm *sunrise_tm, *sunset_tm, *moonrise_tm, *moonset_tm;
     gchar *value, *rawvalue, *wind;
-    gchar interval_start[80], interval_end[80], point[80];
-    gchar sunrise[80], sunset[80], moonrise[80], moonset[80];
+    gchar *interval_start, *interval_end, *point, *last_weather_update;
+    gchar *sunrise, *sunset, *moonrise, *moonset;
     summary_details *sum;
 
     sum = g_slice_new0(summary_details);
@@ -352,17 +338,21 @@ create_summary_tab(plugin_data *data)
     APPEND_TEXT_ITEM(_("Longitude"), LONGITUDE);
 
     APPEND_BTEXT(_("\nTime\n"));
-    point_tm = localtime(&conditions->point);
-    strftime(point, 80, "%c", point_tm);
+    last_weather_update = format_date(data->weather_update->last, NULL, TRUE);
+    value = g_strdup_printf(_("\tLast updated:\t%s\n\n"),
+                            last_weather_update);
+    g_free(last_weather_update);
+    APPEND_TEXT_ITEM_REAL(value);
+
+    point = format_date(conditions->point, NULL, TRUE);
     value = g_strdup_printf
-        (_("\tTemperature, wind, atmosphere and cloud data apply to:\n\t%s\n"),
+        (_("\tTemperature, wind, atmosphere and cloud data calculated for:\n\t%s\n"),
          point);
+    g_free(point);
     APPEND_TEXT_ITEM_REAL(value);
 
-    start_tm = localtime(&conditions->start);
-    strftime(interval_start, 80, "%c", start_tm);
-    end_tm = localtime(&conditions->end);
-    strftime(interval_end, 80, "%c", end_tm);
+    interval_start = format_date(conditions->start, NULL, TRUE);
+    interval_end = format_date(conditions->end, NULL, TRUE);
     value = g_strdup_printf
         (_("\n\tPrecipitation and the weather symbol have been calculated\n"
            "\tfor the following time interval:\n"
@@ -370,6 +360,8 @@ create_summary_tab(plugin_data *data)
            "\tEnd:\t%s\n"),
          interval_start,
          interval_end);
+    g_free(interval_end);
+    g_free(interval_start);
     APPEND_TEXT_ITEM_REAL(value);
 
     /* sun and moon */
@@ -382,14 +374,14 @@ create_summary_tab(plugin_data *data)
             value = g_strdup(_("\tSunset:\t\tThe sun never sets today.\n"));
             APPEND_TEXT_ITEM_REAL(value);
         } else {
-            sunrise_tm = localtime(&data->astrodata->sunrise);
-            strftime(sunrise, 80, "%c", sunrise_tm);
+            sunrise = format_date(data->astrodata->sunrise, NULL, TRUE);
             value = g_strdup_printf(_("\tSunrise:\t\t%s\n"), sunrise);
+            g_free(sunrise);
             APPEND_TEXT_ITEM_REAL(value);
 
-            sunset_tm = localtime(&data->astrodata->sunset);
-            strftime(sunset, 80, "%c", sunset_tm);
+            sunset = format_date(data->astrodata->sunset, NULL, TRUE);
             value = g_strdup_printf(_("\tSunset:\t\t%s\n\n"), sunset);
+            g_free(sunset);
             APPEND_TEXT_ITEM_REAL(value);
         }
 
@@ -408,14 +400,14 @@ create_summary_tab(plugin_data *data)
             value = g_strdup(_("\tMoonset:\tThe moon never sets today.\n"));
             APPEND_TEXT_ITEM_REAL(value);
         } else {
-            moonrise_tm = localtime(&data->astrodata->moonrise);
-            strftime(moonrise, 80, "%c", moonrise_tm);
+            moonrise = format_date(data->astrodata->moonrise, NULL, TRUE);
             value = g_strdup_printf(_("\tMoonrise:\t%s\n"), moonrise);
+            g_free(moonrise);
             APPEND_TEXT_ITEM_REAL(value);
 
-            moonset_tm = localtime(&data->astrodata->moonset);
-            strftime(moonset, 80, "%c", moonset_tm);
+            moonset = format_date(data->astrodata->moonset, NULL, TRUE);
             value = g_strdup_printf(_("\tMoonset:\t%s\n"), moonset);
+            g_free(moonset);
             APPEND_TEXT_ITEM_REAL(value);
         }
     } else {
@@ -539,13 +531,13 @@ forecast_cell_get_tooltip_text(plugin_data *data,
 
     /* TRANSLATORS: Please use \t as needed to properly align the values */
     text = g_string_new(_("<b>Times used for calculation</b>\n"));
-    value = format_date(fcdata->start);
+    value = format_date(fcdata->start, NULL, TRUE);
     g_string_append_printf(text, _("Interval start:\t\t\t%s\n"), value);
     g_free(value);
-    value = format_date(fcdata->end);
+    value = format_date(fcdata->end, NULL, TRUE);
     g_string_append_printf(text, _("Interval end:\t\t\t%s\n"), value);
     g_free(value);
-    value = format_date(fcdata->point);
+    value = format_date(fcdata->point, NULL, TRUE);
     g_string_append_printf(text, _("Data calculated for:\t%s\n\n"), value);
     g_free(value);
 
diff --git a/panel-plugin/weather.c b/panel-plugin/weather.c
index 5149b81..07edbd9 100644
--- a/panel-plugin/weather.c
+++ b/panel-plugin/weather.c
@@ -452,12 +452,10 @@ cb_weather_update(SoupSession *session,
     xmlDoc *doc;
     xmlNode *root_node;
     time_t now_t;
-    struct tm now_tm;
     gboolean parsing_error = TRUE;
 
     weather_debug("Processing downloaded weather data.");
     time(&now_t);
-    now_tm = *localtime(&now_t);
     data->weather_update->attempt++;
     if (msg->status_code == 200 || msg->status_code == 203) {
         if (msg->status_code == 203)
@@ -868,23 +866,6 @@ make_cache_filename(plugin_data *data)
 }
 
 
-/*
- * Convert localtime to gmtime and format it to a string
- * that can be parsed later by parse_timestring.
- */
-static gchar *
-cache_file_strftime_t(const time_t t)
-{
-    struct tm *tm;
-    gchar str[21];
-    size_t size;
-
-    tm = gmtime(&t);
-    size = strftime(str, 21, "%Y-%m-%dT%H:%M:%SZ", tm);
-    return (size ? g_strdup(str) : g_strdup(""));
-}
-
-
 static void
 write_cache_file(plugin_data *data)
 {
@@ -893,6 +874,7 @@ write_cache_file(plugin_data *data)
     xml_time *timeslice;
     xml_location *loc;
     gchar *file, *start, *end, *point, *now;
+    gchar *date_format = "%Y-%m-%dT%H:%M:%SZ";
     time_t now_t = time(NULL);
     gint i, j;
 
@@ -907,7 +889,7 @@ write_cache_file(plugin_data *data)
     CACHE_APPEND("lon=%s\n", data->lon);
     g_string_append_printf(out, "msl=%d\ntimezone=%d\ntimeslices=%d\n",
                            data->msl, data->timezone, wd->timeslices->len);
-    now = cache_file_strftime_t(now_t);
+    now = format_date(now_t, date_format, FALSE);
     CACHE_APPEND("cache_date=%s\n\n", now);
     g_free(now);
 
@@ -916,9 +898,9 @@ write_cache_file(plugin_data *data)
         if (G_UNLIKELY(timeslice == NULL || timeslice->location == NULL))
             continue;
         loc = timeslice->location;
-        start = cache_file_strftime_t(timeslice->start);
-        end = cache_file_strftime_t(timeslice->end);
-        point = cache_file_strftime_t(timeslice->point);
+        start = format_date(timeslice->start, date_format, FALSE);
+        end = format_date(timeslice->end, date_format, FALSE);
+        point = format_date(timeslice->point, date_format, FALSE);
         g_string_append_printf(out, "[timeslice%d]\n", i);
         CACHE_APPEND("start=%s\n", start);
         CACHE_APPEND("end=%s\n", end);
@@ -1281,13 +1263,11 @@ static gchar *
 weather_get_tooltip_text(const plugin_data *data)
 {
     xml_time *conditions;
-    struct tm *point_tm, *start_tm, *end_tm, *sunrise_tm, *sunset_tm;
     gchar *text, *sym, *alt, *temp;
     gchar *windspeed, *windbeau, *winddir, *winddir_trans, *winddeg;
     gchar *pressure, *humidity, *precipitations;
     gchar *fog, *cloudiness, *sunval, *value;
-    gchar sunrise[40], sunset[40];
-    gchar point[40], interval_start[40], interval_end[40];
+    gchar *point, *interval_start, *interval_end, *sunrise, *sunset;
     const gchar *unit;
 
     conditions = get_current_conditions(data->weatherdata);
@@ -1297,12 +1277,9 @@ weather_get_tooltip_text(const plugin_data *data)
     }
 
     /* times for forecast and point data */
-    point_tm = localtime(&conditions->point);
-    strftime(point, 40, "%X", point_tm);
-    start_tm = localtime(&conditions->start);
-    strftime(interval_start, 40, "%X", start_tm);
-    end_tm = localtime(&conditions->end);
-    strftime(interval_end, 40, "%X", end_tm);
+    point = format_date(conditions->point, "%H:%M", TRUE);
+    interval_start = format_date(conditions->start, "%H:%M", TRUE);
+    interval_end = format_date(conditions->end, "%H:%M", TRUE);
 
     /* use sunrise and sunset times if available */
     if (data->astrodata)
@@ -1311,12 +1288,12 @@ weather_get_tooltip_text(const plugin_data *data)
         } else if (data->astrodata->sun_never_sets) {
             sunval = g_strdup(_("The sun never sets today."));
         } else {
-            sunrise_tm = localtime(&data->astrodata->sunrise);
-            strftime(sunrise, 40, "%X", sunrise_tm);
-            sunset_tm = localtime(&data->astrodata->sunset);
-            strftime(sunset, 40, "%X", sunset_tm);
+            sunrise = format_date(data->astrodata->sunrise, "%H:%M:%S", TRUE);
+            sunset = format_date(data->astrodata->sunset, "%H:%M:%S", TRUE);
             sunval = g_strdup_printf(_("The sun rises at %s and sets at %s."),
                                      sunrise, sunset);
+            g_free(sunrise);
+            g_free(sunset);
         }
     else
         sunval = g_strdup_printf("");
@@ -1391,6 +1368,9 @@ weather_get_tooltip_text(const plugin_data *data)
     g_free(sym);
     g_free(alt);
     g_free(temp);
+    g_free(interval_start);
+    g_free(interval_end);
+    g_free(point);
     g_free(windspeed);
     g_free(windbeau);
     g_free(winddir_trans);


More information about the Xfce4-commits mailing list