[gnome-logs/wip/friendly-timetamp: 2/3] Improve timestamp detail for different day or year



commit 3586cd03fcb08802b6ed03801d21824797aef871
Author: David King <davidk gnome org>
Date:   Tue Oct 29 12:43:56 2013 +0000

    Improve timestamp detail for different day or year
    
    https://bugzilla.gnome.org/show_bug.cgi?id=711020

 src/gl-util.c |   90 +++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 81 insertions(+), 9 deletions(-)
---
diff --git a/src/gl-util.c b/src/gl-util.c
index bc28b71..4f270d2 100644
--- a/src/gl-util.c
+++ b/src/gl-util.c
@@ -20,6 +20,13 @@
 
 #include <glib/gi18n.h>
 
+typedef enum
+{
+    GL_UTIL_TIMESTAMPS_SAME_DAY,
+    GL_UTIL_TIMESTAMPS_SAME_YEAR,
+    GL_UTIL_TIMESTAMPS_DIFFERENT_YEAR
+} GlUtilTimestamps;
+
 void
 gl_util_on_css_provider_parsing_error (GtkCssProvider *provider,
                                        GtkCssSection *section,
@@ -32,12 +39,38 @@ gl_util_on_css_provider_parsing_error (GtkCssProvider *provider,
                 error->message);
 }
 
+static GlUtilTimestamps
+compare_timestamps (GDateTime *a,
+                    GDateTime *b)
+{
+    gint ayear, amonth, aday;
+    gint byear, bmonth, bday;
+
+    g_date_time_get_ymd (a, &ayear, &amonth, &aday);
+    g_date_time_get_ymd (b, &byear, &bmonth, &bday);
+
+    if (ayear != byear)
+    {
+        return GL_UTIL_TIMESTAMPS_DIFFERENT_YEAR;
+    }
+
+    /* Same year, month and day. */
+    if ((amonth == bmonth) && (aday == bday))
+    {
+        return GL_UTIL_TIMESTAMPS_SAME_DAY;
+    }
+
+    /* Same year, but differing by month or day. */
+    return GL_UTIL_TIMESTAMPS_SAME_YEAR;
+}
+
 gchar *
 gl_util_timestamp_to_display (guint64 microsecs,
                               GlUtilClockFormat format)
 {
     GDateTime *datetime;
     GDateTime *local;
+    GDateTime *now;
     gchar *time = NULL;
 
     datetime = g_date_time_new_from_unix_utc (microsecs / G_TIME_SPAN_SECOND);
@@ -49,21 +82,59 @@ gl_util_timestamp_to_display (guint64 microsecs,
     }
 
     local = g_date_time_to_local (datetime);
-
-    /* TODO: Add logic to show day/date on timestamps where the day is not
-     * today. */
+    now = g_date_time_new_now_local ();
 
     switch (format)
     {
         case GL_UTIL_CLOCK_FORMAT_12HR:
-            /* Translators: timestamp format for events on the current day,
-             * showing the time (including seconds) in 12-hour format. */
-            time = g_date_time_format (local, _("%l:%M:%S %p"));
+            switch (compare_timestamps (local, now))
+            {
+                case GL_UTIL_TIMESTAMPS_SAME_DAY:
+                    /* Translators: timestamp format for events on the current
+                     * day, showing the time in 12-hour format. */
+                    time = g_date_time_format (local, _("%l:%M %p"));
+                    break;
+                case GL_UTIL_TIMESTAMPS_SAME_YEAR:
+                    /* Translators: timestamp format for events in the current
+                     * year, showing the abbreviated month name, day of the
+                     * month and the time in 24-hour format. */
+                    time = g_date_time_format (local, _("%b %e %l:%M %p"));
+                    break;
+                case GL_UTIL_TIMESTAMPS_DIFFERENT_YEAR:
+                    /* Translators: timestamp format for events in a different
+                     * year, showing the abbreviated month name, day of the
+                     * month, year and the time in 24-hour format. */
+                    time = g_date_time_format (local, _("%b %e %Y %l:%M %p"));
+                    break;
+                default:
+                    g_assert_not_reached ();
+            }
+
             break;
         case GL_UTIL_CLOCK_FORMAT_24HR:
-            /* Translators: timestamp format for events on the current day,
-             * showing the time (including seconds) in 24-hour format. */
-            time = g_date_time_format (local, _("%T"));
+            switch (compare_timestamps (local, now))
+            {
+                case GL_UTIL_TIMESTAMPS_SAME_DAY:
+                    /* Translators: timestamp format for events on the current
+                     * day, showing the time in 24-hour format. */
+                    time = g_date_time_format (local, _("%H:%M"));
+                    break;
+                case GL_UTIL_TIMESTAMPS_SAME_YEAR:
+                    /* Translators: timestamp format for events in the current
+                     * year, showing the abbreviated month name, day of the
+                     * month and the time in 24-hour format. */
+                    time = g_date_time_format (local, _("%b %e %H:%M"));
+                    break;
+                case GL_UTIL_TIMESTAMPS_DIFFERENT_YEAR:
+                    /* Translators: timestamp format for events in a different
+                     * year, showing the abbreviated month name, day of the
+                     * month, year and the time in 24-hour format. */
+                    time = g_date_time_format (local, _("%b %e %Y %H:%M"));
+                    break;
+                default:
+                    g_assert_not_reached ();
+            }
+
             break;
         default:
             g_assert_not_reached ();
@@ -71,6 +142,7 @@ 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)
     {


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