[Xfce4-commits] <xfdesktop:jannis/port-to-gio> Add xfdesktop_file_utils_format_time_for_display().

Jannis Pohlmann noreply at xfce.org
Wed Oct 27 22:14:01 CEST 2010


Updating branch refs/heads/jannis/port-to-gio
         to 459404a4060bf7ff0be3db53e8e4f8feb4e76459 (commit)
       from 3d69f14d6c71f45ca3aa445dc5c8807297f184ba (commit)

commit 459404a4060bf7ff0be3db53e8e4f8feb4e76459
Author: Jannis Pohlmann <jannis at xfce.org>
Date:   Wed Oct 27 22:10:39 2010 +0200

    Add xfdesktop_file_utils_format_time_for_display().
    
    This is a simplified method of thunar_util_humanize_file_time() that
    only supports one time format (short).

 src/xfdesktop-file-utils.c |   53 ++++++++++++++++++++++++++++++++++++++++++++
 src/xfdesktop-file-utils.h |    1 +
 2 files changed, 54 insertions(+), 0 deletions(-)

diff --git a/src/xfdesktop-file-utils.c b/src/xfdesktop-file-utils.c
index 6b64593..a803002 100644
--- a/src/xfdesktop-file-utils.c
+++ b/src/xfdesktop-file-utils.c
@@ -33,6 +33,9 @@
 #ifdef HAVE_STRING_H
 #include <string.h>
 #endif
+#ifdef HAVE_TIME_H
+#include <time.h>
+#endif
 
 #include <gtk/gtk.h>
 
@@ -206,6 +209,56 @@ xfdesktop_file_utils_file_is_executable(GFileInfo *info)
     return can_execute || xfdesktop_file_utils_is_desktop_file(info);
 }
 
+gchar *
+xfdesktop_file_utils_format_time_for_display(guint64 file_time)
+{
+  const gchar *date_format;
+  struct tm *tfile;
+  time_t ftime;
+  GDate dfile;
+  GDate dnow;
+  gchar buffer[128];
+  gint diff;
+
+  /* check if the file_time is valid */
+  if(file_time != 0) {
+      ftime = (time_t) file_time;
+
+      /* determine the local file time */
+      tfile = localtime(&ftime);
+
+      /* setup the dates for the time values */
+      g_date_set_time_t(&dfile, (time_t) ftime);
+      g_date_set_time_t(&dnow, time(NULL));
+
+      /* determine the difference in days */
+      diff = g_date_get_julian(&dnow) - g_date_get_julian(&dfile);
+      if(diff == 0) {
+          /* TRANSLATORS: file was modified less than one day ago */
+          strftime(buffer, 128, _("Today at %X"), tfile);
+          return g_strdup(buffer);
+      } else if(diff == 1) {
+          /* TRANSLATORS: file was modified less than two days ago */
+          strftime(buffer, 128, _("Yesterday at %X"), tfile);
+          return g_strdup(buffer);
+      } else {
+          if (diff > 1 && diff < 7) {
+              /* Days from last week */
+              date_format = _("%A at %X");
+          } else {
+              /* Any other date */
+              date_format = _("%x at %X");
+          }
+
+          /* format the date string accordingly */
+          strftime(buffer, 128, date_format, tfile);
+          return g_strdup(buffer);
+      }
+  }
+
+  /* the file_time is invalid */
+  return g_strdup(_("Unknown"));
+}
 
 GList *
 xfdesktop_file_utils_file_icon_list_to_file_list(GList *icon_list)
diff --git a/src/xfdesktop-file-utils.h b/src/xfdesktop-file-utils.h
index fa25960..603dfbf 100644
--- a/src/xfdesktop-file-utils.h
+++ b/src/xfdesktop-file-utils.h
@@ -59,6 +59,7 @@ gchar *xfdesktop_file_utils_get_file_kind(const ThunarVfsInfo *info,
                                           gboolean *is_link);
 gboolean xfdesktop_file_utils_is_desktop_file(GFileInfo *info);
 gboolean xfdesktop_file_utils_file_is_executable(GFileInfo *info);
+gchar *xfdesktop_file_utils_format_time_for_display(guint64 file_time);
 
 GList *xfdesktop_file_utils_file_icon_list_to_file_list(GList *icon_list);
 gchar *xfdesktop_file_utils_file_list_to_string(GList *file_list);



More information about the Xfce4-commits mailing list