[gnome-logs/wip/test] Take a now parameter when comparing time stamps



commit 08b49bd3cd178198067da3cf67267cb92f7ddfb4
Author: Ekaterina Gerasimova <kittykat3756 gmail com>
Date:   Mon Feb 9 11:07:07 2015 +0000

    Take a now parameter when comparing time stamps
    
    Move the current time stamp out of gl_util_timestamp_to_display() and
    pass it as a parameter so that the function does not depend on the
    current time and will be easier to test.

 src/gl-eventviewdetail.c |    6 +++++-
 src/gl-eventviewrow.c    |   10 ++++++++--
 src/gl-util.c            |   10 +++++-----
 src/gl-util.h            |    1 +
 4 files changed, 19 insertions(+), 8 deletions(-)
---
diff --git a/src/gl-eventviewdetail.c b/src/gl-eventviewdetail.c
index bda8582..a9f1e35 100644
--- a/src/gl-eventviewdetail.c
+++ b/src/gl-eventviewdetail.c
@@ -69,6 +69,7 @@ gl_event_view_detail_create_detail (GlEventViewDetail *detail)
     gchar *str_field;
     gchar *str_message;
     gchar *str_copy;
+    GDateTime *now;
 
     priv = gl_event_view_detail_get_instance_private (detail);
 
@@ -115,7 +116,10 @@ gl_event_view_detail_create_detail (GlEventViewDetail *detail)
         }
     }
 
-    str = gl_util_timestamp_to_display (result->timestamp, priv->clock_format);
+    now = g_date_time_new_now_local ();
+    str = gl_util_timestamp_to_display (result->timestamp, now,
+                                        priv->clock_format);
+    g_date_time_unref (now);
     gtk_label_set_text (GTK_LABEL (priv->time_label), str);
     g_free (str);
 
diff --git a/src/gl-eventviewrow.c b/src/gl-eventviewrow.c
index fd1f270..5d3df33 100644
--- a/src/gl-eventviewrow.c
+++ b/src/gl-eventviewrow.c
@@ -117,6 +117,7 @@ gl_event_view_row_create_cmdline (GlEventViewRow *row)
     gboolean rtl;
     GtkWidget *image;
     GlJournalResult *result;
+    GDateTime *now;
 
     priv = gl_event_view_row_get_instance_private (row);
     result = priv->result;
@@ -148,8 +149,10 @@ gl_event_view_row_create_cmdline (GlEventViewRow *row)
     gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
     gtk_grid_attach (GTK_GRID (grid), label, 0, 1, 2, 1);
 
-    time = gl_util_timestamp_to_display (result->timestamp,
+    now = g_date_time_new_now_local ();
+    time = gl_util_timestamp_to_display (result->timestamp, now,
                                          priv->clock_format);
+    g_date_time_unref (now);
     label = gtk_label_new (time);
     context = gtk_widget_get_style_context (GTK_WIDGET (label));
     gtk_style_context_add_class (context, "dim-label");
@@ -175,6 +178,7 @@ gl_event_view_row_create_simple (GlEventViewRow *row)
     gboolean rtl;
     GtkWidget *image;
     GlJournalResult *result;
+    GDateTime *now;
 
     priv = gl_event_view_row_get_instance_private (row);
     result = priv->result;
@@ -196,8 +200,10 @@ gl_event_view_row_create_simple (GlEventViewRow *row)
     gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
     gtk_grid_attach (GTK_GRID (grid), label, 0, 1, 1, 1);
 
-    time = gl_util_timestamp_to_display (result->timestamp,
+    now = g_date_time_new_now_local ();
+    time = gl_util_timestamp_to_display (result->timestamp, now,
                                          priv->clock_format);
+    g_date_time_unref (now);
     label = gtk_label_new (time);
     context = gtk_widget_get_style_context (GTK_WIDGET (label));
     gtk_style_context_add_class (context, "dim-label");
diff --git a/src/gl-util.c b/src/gl-util.c
index b6e3319..6c470f8 100644
--- a/src/gl-util.c
+++ b/src/gl-util.c
@@ -1,6 +1,7 @@
 /*
  *  GNOME Logs - View and search logs
  *  Copyright (C) 2013  Red Hat, Inc.
+ *  Copyright (C) 2015  Ekaterina Gerasimova
  *
  *  This program is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -98,21 +99,22 @@ compare_timestamps (GDateTime *a,
 /**
  * gl_util_timestamp_to_display:
  * @microsecs: number of microseconds since the Unix epoch in UTC
+ * @now: the time to compare with
  * @format: clock format (12 or 24 hour)
  *
  * Return a human readable time, corresponding to @microsecs, using an
- * appropriate @format after comparing it to the current time and discarding
- * unnecessary elements (for example, return only time if the date is today).
+ * appropriate @format after comparing it to @now and discarding unnecessary
+ * elements (for example, return only time if the date is today).
  *
  * Returns: a newly-allocated human readable string which represents @microsecs
  */
 gchar *
 gl_util_timestamp_to_display (guint64 microsecs,
+                              GDateTime *now,
                               GlUtilClockFormat format)
 {
     GDateTime *datetime;
     GDateTime *local;
-    GDateTime *now;
     gchar *time = NULL;
 
     datetime = g_date_time_new_from_unix_utc (microsecs / G_TIME_SPAN_SECOND);
@@ -124,7 +126,6 @@ gl_util_timestamp_to_display (guint64 microsecs,
     }
 
     local = g_date_time_to_local (datetime);
-    now = g_date_time_new_now_local ();
 
     switch (format)
     {
@@ -184,7 +185,6 @@ gl_util_timestamp_to_display (guint64 microsecs,
 
     g_date_time_unref (datetime);
     g_date_time_unref (local);
-    g_date_time_unref (now);
 
     if (time == NULL)
     {
diff --git a/src/gl-util.h b/src/gl-util.h
index 6dd12d5..b6cf53c 100644
--- a/src/gl-util.h
+++ b/src/gl-util.h
@@ -40,6 +40,7 @@ void gl_util_on_css_provider_parsing_error (GtkCssProvider *provider,
                                             GError *error,
                                             G_GNUC_UNUSED gpointer user_data);
 gchar * gl_util_timestamp_to_display (guint64 microsecs,
+                                      GDateTime *now,
                                       GlUtilClockFormat format);
 
 G_END_DECLS


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]