[nautilus] nautilus-file: implement smarter dates



commit 2f4b6d6a8f2689b7d15db1a770666e1f289dff9a
Author: Carlos Soriano <csoriano gnome org>
Date:   Mon Feb 16 12:00:31 2015 +0100

    nautilus-file: implement smarter dates
    
    Design request. Now we show the name of the day of the week if the file
    was modified in the last week.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=744237

 libnautilus-private/nautilus-file.c |  114 ++++++++++++++++++-----------------
 1 files changed, 59 insertions(+), 55 deletions(-)
---
diff --git a/libnautilus-private/nautilus-file.c b/libnautilus-private/nautilus-file.c
index b4a9fca..5163c7e 100644
--- a/libnautilus-private/nautilus-file.c
+++ b/libnautilus-private/nautilus-file.c
@@ -4657,26 +4657,6 @@ nautilus_file_get_trash_original_file_parent_as_string (NautilusFile *file)
        return NULL;
 }
 
-/*
- * Note to localizers: You can look at man strftime
- * for details on the format, but you should only use
- * the specifiers from the C standard, not extensions.
- * These include "%" followed by one of
- * "aAbBcdHIjmMpSUwWxXyYZ". There are two extensions
- * in the Nautilus version of strftime that can be
- * used (and match GNU extensions). Putting a "-"
- * between the "%" and any numeric directive will turn
- * off zero padding, and putting a "_" there will use
- * space padding instead of zero padding.
- */
-#define TODAY_TIME_FORMAT_24 N_("%R")
-#define TODAY_TIME_FORMAT N_("%-I:%M %P")
-#define THIS_MONTH_TIME_FORMAT N_("%b %-e")
-#define THIS_YEAR_TIME_FORMAT N_("%b %-e")
-#define ANYTIME_TIME_FORMAT N_("%b %-d %Y")
-#define FULL_FORMAT N_("%a, %b %e %Y %I:%M:%S %p")
-#define FULL_FORMAT_24 N_("%a, %b %e %Y %T")
-
 /**
  * nautilus_file_get_date_as_string:
  * 
@@ -4688,49 +4668,73 @@ nautilus_file_get_trash_original_file_parent_as_string (NautilusFile *file)
  * 
  **/
 static char *
-nautilus_file_get_date_as_string (NautilusFile *file, NautilusDateType date_type, gboolean compact)
+nautilus_file_get_date_as_string (NautilusFile     *file,
+                                  NautilusDateType  date_type,
+                                  gboolean          full_date)
 {
        time_t file_time_raw;
-       const char *format;
-       char *result = NULL;
-       GDateTime *date_time, *today;
-       int y, m, d;
-       int y_now, m_now, d_now;
-       GDesktopClockFormat value;
+       GDateTime *file_date, *now;
+       gint daysAgo;
        gboolean use_24;
+       gchar *format;
+       gchar *result;
+       gchar *result_with_ratio;
 
-       if (!nautilus_file_get_date (file, date_type, &file_time_raw)) {
+       if (!nautilus_file_get_date (file, date_type, &file_time_raw))
                return NULL;
-       }
 
-       date_time = g_date_time_new_from_unix_local (file_time_raw);
+       file_date = g_date_time_new_from_unix_local (file_time_raw);
+       if (!full_date) {
+               now = g_date_time_new_now_local ();
 
-       g_date_time_get_ymd (date_time, &y, &m, &d);
+               daysAgo = g_date_time_difference (now, file_date) / (24 * 60 * 60 * 1000 * 1000L);
 
-       today = g_date_time_new_now_local ();
-       g_date_time_get_ymd (today, &y_now, &m_now, &d_now);
-       g_date_time_unref (today);
+               use_24 = g_settings_get_enum (gnome_interface_preferences, "clock-format") ==
+                        G_DESKTOP_CLOCK_FORMAT_24H;
 
-       value = g_settings_get_enum (gnome_interface_preferences, "clock-format");
-       use_24 = value == G_DESKTOP_CLOCK_FORMAT_24H;
-
-       if (!compact) {
-               format = use_24 ? FULL_FORMAT_24 : FULL_FORMAT;
-       } else if (y == y_now && m == m_now && d == d_now) {
-               format = use_24 ? TODAY_TIME_FORMAT_24 : TODAY_TIME_FORMAT;
-       } else if (y == y_now && m == m_now) {
-               format = THIS_MONTH_TIME_FORMAT;
-       } else if (y == y_now) {
-               format = THIS_YEAR_TIME_FORMAT;
+               // Show only the time if date is on today
+               if (daysAgo < 1) {
+                       if (use_24) {
+                               /* Translators: Time in 24h format */
+                               format = N_("%H:%M");
+                       } else {
+                               /* Translators: Time in 12h format */
+                               format = N_("%l:%M %p");
+                       }
+               }
+               // Show the word "Yesterday" and time if date is on yesterday
+               else if (daysAgo < 2) {
+                       // xgettext:no-c-format
+                       format = N_("Yesterday");
+               }
+               // Show a week day and time if date is in the last week
+               else if (daysAgo < 7) {
+                       // xgettext:no-c-format
+                       format = N_("%a");
+               } else if (g_date_time_get_year (file_date) == g_date_time_get_year (now)) {
+                       /* Translators: this is the day of the month plus the short
+                        * month name i.e. "3 Feb" */
+                       // xgettext:no-c-format
+                       format = N_("%-e %b");
+               } else {
+                       /* Translators: this is the day of the month followed by the short
+                        * month name followed by the year i.e. "3 Feb 2015" */
+                       // xgettext:no-c-format
+                       format = N_("%-e %b %Y");
+               }
        } else {
-               format = ANYTIME_TIME_FORMAT;
+               format = N_("%c");
        }
 
-       result = g_date_time_format (date_time, _(format));
+       result = g_date_time_format (file_date, format);
+       g_date_time_unref (file_date);
 
-       g_date_time_unref (date_time);
+        /* Replace ":" with ratio. Replacement is done afterward because g_date_time_format
+         * may fail with utf8 chars in some locales */
+       result_with_ratio = eel_str_replace_substring (result, ":", "∶");
+       g_free (result);
 
-       return result;
+        return  result_with_ratio;
 }
 
 static void
@@ -6188,32 +6192,32 @@ nautilus_file_get_string_attribute_q (NautilusFile *file, GQuark attribute_q)
        if (attribute_q == attribute_date_modified_q) {
                return nautilus_file_get_date_as_string (file, 
                                                         NAUTILUS_DATE_TYPE_MODIFIED,
-                                                        TRUE);
+                                                        FALSE);
        }
        if (attribute_q == attribute_date_modified_full_q) {
                return nautilus_file_get_date_as_string (file, 
                                                         NAUTILUS_DATE_TYPE_MODIFIED,
-                                                        FALSE);
+                                                        TRUE);
        }
        if (attribute_q == attribute_date_accessed_q) {
                return nautilus_file_get_date_as_string (file,
                                                         NAUTILUS_DATE_TYPE_ACCESSED,
-                                                        TRUE);
+                                                        FALSE);
        }
        if (attribute_q == attribute_date_accessed_full_q) {
                return nautilus_file_get_date_as_string (file,
                                                         NAUTILUS_DATE_TYPE_ACCESSED,
-                                                        FALSE);
+                                                        TRUE);
        }
        if (attribute_q == attribute_trashed_on_q) {
                return nautilus_file_get_date_as_string (file,
                                                         NAUTILUS_DATE_TYPE_TRASHED,
-                                                        TRUE);
+                                                        FALSE);
        }
        if (attribute_q == attribute_trashed_on_full_q) {
                return nautilus_file_get_date_as_string (file,
                                                         NAUTILUS_DATE_TYPE_TRASHED,
-                                                        FALSE);
+                                                        TRUE);
        }
        if (attribute_q == attribute_permissions_q) {
                return nautilus_file_get_permissions_as_string (file);


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