[tracker/needle: 22/53] tracker-needle: Moved time format to utils module



commit 068be38c3217e851fd518f6ea1b83a11d471c347
Author: Martyn Russell <martyn lanedo com>
Date:   Sun Aug 15 16:23:56 2010 +0100

    tracker-needle: Moved time format to utils module

 src/tracker-needle/Makefile.am         |    8 ++-
 src/tracker-needle/tracker-needle.vala |   47 +----------------------
 src/tracker-needle/tracker-utils.vala  |   66 ++++++++++++++++++++++++++++++++
 3 files changed, 72 insertions(+), 49 deletions(-)
---
diff --git a/src/tracker-needle/Makefile.am b/src/tracker-needle/Makefile.am
index 7255f20..0139987 100644
--- a/src/tracker-needle/Makefile.am
+++ b/src/tracker-needle/Makefile.am
@@ -4,8 +4,9 @@ bin_PROGRAMS = tracker-needle
 
 tracker_needle_VALASOURCES = 					\
 	tracker-cell-renderer-text.vala				\
-	tracker-needle.vala                         \
-	tracker-stats.vala
+	tracker-needle.vala \
+	tracker-stats.vala \
+	tracker-utils.vala
 
 tracker_needle_SOURCES = 					\
 	$(tracker_needle_VALASOURCES:.vala=.c)
@@ -21,7 +22,8 @@ tracker_needle_CFLAGS = 					\
 	$(TRACKER_APPS_CFLAGS)					\
 	$(TRACKER_VALA_CFLAGS)					\
 	$(WARN_CFLAGS)						\
-	$(GCOV_CFLAGS)
+	$(GCOV_CFLAGS)						\
+	-include config.h
 
 tracker_needle_LDADD = 						\
 	$(TRACKER_APPS_LIBS)					\
diff --git a/src/tracker-needle/tracker-needle.vala b/src/tracker-needle/tracker-needle.vala
index 4cb88c1..1a626d3 100644
--- a/src/tracker-needle/tracker-needle.vala
+++ b/src/tracker-needle/tracker-needle.vala
@@ -53,7 +53,6 @@ public class TrackerNeedle {
 	static bool current_view = true;
 	static bool current_find_in = true;
 
-	private const int secs_per_day = 60 * 60 * 24;
 
 	public void show () {
 		setup_dbus ();
@@ -227,51 +226,7 @@ public class TrackerNeedle {
 		return pixbuf;
 	}
 
-	private string item_get_time (string s) {
-		GLib.Time t = GLib.Time ();
-		t.strptime (s, "%FT%T");
 
-		var tv_now = GLib.TimeVal ();
-		tv_now.get_current_time ();
-
-		var tv_then = GLib.TimeVal ();
-		tv_then.from_iso8601 (s);
-
-		var diff_sec = tv_now.tv_sec - tv_then.tv_sec;
-		var diff_days = diff_sec / secs_per_day;
-		var diff_days_abs = diff_days.abs ();
-
-		// stdout.printf ("timeval now:%ld, then:%ld, diff secs:%ld, diff days:%ld, abs: %ld, seconds per day:%d\n", tv_now.tv_sec, tv_then.tv_sec, diff_sec, diff_days, diff_days_abs, secs_per_day);
-
-		// if it's more than a week, use the default date format
-		if (diff_days_abs > 7) {
-			return t.format ("%x");
-		}
-
-		if (diff_days_abs == 0) {
-			return "Today";
-		} else {
-			bool future = false;
-
-			if (diff_days < 0)
-				future = true;
-
-			if (diff_days <= 1) {
-				if (future)
-					return "Tomorrow";
-				else
-					return "Yesterday";
-			} else {
-				if (future) {
-					/* Translators: %d is replaced with a number of days. It's always greater than 1 */
-					return ngettext ("%ld day from now", "%ld days from now", diff_days_abs).printf (diff_days_abs);
-				} else {
-					/* Translators: %d is replaced with a number of days. It's always greater than 1 */
-					return ngettext ("%ld day ago", "%ld days ago", diff_days_abs).printf (diff_days_abs);
-				}
-			}
-		}
-	}
 
 	private bool search_run () {
 		// Need to escape this string
@@ -319,7 +274,7 @@ public class TrackerNeedle {
 				Gdk.Pixbuf pixbuf_small = item_get_pixbuf (theme, result[i,1], size_small);
 				Gdk.Pixbuf pixbuf_big = item_get_pixbuf (theme, result[i,1], size_big);
 				string file_size = GLib.format_size_for_display (result[i,4].to_int());
-				string file_time = item_get_time (result[i,3]);
+				string file_time = tracker_item_format_time (result[i,3]);
 
 				// Insert into model
 				TreeIter iter;
diff --git a/src/tracker-needle/tracker-utils.vala b/src/tracker-needle/tracker-utils.vala
new file mode 100644
index 0000000..7aaf34d
--- /dev/null
+++ b/src/tracker-needle/tracker-utils.vala
@@ -0,0 +1,66 @@
+//
+// Copyright 2010, Martyn Russell <martyn lanedo com>
+//
+// 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 the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+// 02110-1301, USA.
+//
+
+private const int secs_per_day = 60 * 60 * 24;
+
+public string tracker_item_format_time (string s) {
+	GLib.Time t = GLib.Time ();
+	t.strptime (s, "%FT%T");
+
+	var tv_now = GLib.TimeVal ();
+	tv_now.get_current_time ();
+
+	var tv_then = GLib.TimeVal ();
+	tv_then.from_iso8601 (s);
+
+	var diff_sec = tv_now.tv_sec - tv_then.tv_sec;
+	var diff_days = diff_sec / secs_per_day;
+	var diff_days_abs = diff_days.abs ();
+
+	// stdout.printf ("timeval now:%ld, then:%ld, diff secs:%ld, diff days:%ld, abs: %ld, seconds per day:%d\n", tv_now.tv_sec, tv_then.tv_sec, diff_sec, diff_days, diff_days_abs, secs_per_day);
+
+	// if it's more than a week, use the default date format
+	if (diff_days_abs > 7) {
+		return t.format ("%x");
+	}
+
+	if (diff_days_abs == 0) {
+		return "Today";
+	} else {
+		bool future = false;
+
+		if (diff_days < 0)
+			future = true;
+
+		if (diff_days <= 1) {
+			if (future)
+				return "Tomorrow";
+			else
+				return "Yesterday";
+		} else {
+			if (future) {
+				/* Translators: %d is replaced with a number of days. It's always greater than 1 */
+				return ngettext ("%ld day from now", "%ld days from now", diff_days_abs).printf (diff_days_abs);
+			} else {
+				/* Translators: %d is replaced with a number of days. It's always greater than 1 */
+				return ngettext ("%ld day ago", "%ld days ago", diff_days_abs).printf (diff_days_abs);
+			}
+		}
+	}
+}



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