[gitg] Fix for singular and plural relative time format.



commit 314471d20f43af537122222d4e7c5d278f8f9f91
Author: Sindhu S <sindhus live in>
Date:   Fri May 10 22:08:17 2013 +0530

    Fix for singular and plural relative time format.
    
    Use TimeSpan class to calculate difference epoch time.
    Commits as old as an hour now, read 'An hour ago'.
    Commits older than an hour now, read 'N hours ago'.
    Commits as old as a day now, read "A day ago".
    Commits as older than a day now, read 'N days ago'.

 libgitg/gitg-commit.vala |   39 ++++++++++++++++-----------------------
 1 files changed, 16 insertions(+), 23 deletions(-)
---
diff --git a/libgitg/gitg-commit.vala b/libgitg/gitg-commit.vala
index 0e27ab7..f2d0101 100644
--- a/libgitg/gitg-commit.vala
+++ b/libgitg/gitg-commit.vala
@@ -95,39 +95,32 @@ public class Commit : Ggit.Commit
 
        private string date_for_display(DateTime dt, TimeZone time_zone)
        {
-               var t = (new DateTime.now_local()).to_unix() - dt.to_unix();
-
-               if (t < 1)
-               {
-                       return "Less than a second ago";
-               }
-               else if (t < 60)
+               TimeSpan t = (new DateTime.now_local()).difference(dt);
+               
+               float time_in_seconds = (float) t / TimeSpan.SECOND;
+               float time_in_hours = (float) t / TimeSpan.HOUR;
+               
+               if (time_in_seconds < 60)
                {
-                       return "Less than a minute ago";
+                       return "A minute ago";
                }
-               else if (t < 600)
+               else if (time_in_seconds < 60 * 30)
                {
-                       return "Less than 10 minutes ago";
+                       return "%d minutes ago".printf((int) Math.round(time_in_seconds / 60));
                }
-               else if (t < 1800)
+               else if (time_in_seconds < 60 * 45)
                {
                        return "Half an hour ago";
                }
-               else if (t < 3600)
-               {
-                       return "An hour ago";
-               }
-               else if (t < 3600 * 24)
-               {
-                       return "%d hours ago".printf((int)Math.round(t / 3600));
-               }
-               else if (t < 3600 * 24 * 2)
+               else if (time_in_hours < 23.5)
                {
-                       return "A day ago";
+                       int rounded_hours = (int) Math.round(time_in_hours);
+                       return rounded_hours == 1 ? "An hour ago" : "%d hours ago".printf(rounded_hours);
                }
-               else if (t < 3600 * 24 * 6)
+               else if (time_in_hours < 24 * 7)
                {
-                       return "%d days ago".printf((int)Math.round(t / (3600 * 24)));
+                       int rounded_days = (int) Math.round(time_in_hours / 24);
+                       return rounded_days == 1 ? "A day ago" : "%d days ago".printf(rounded_days);
                }
                // FIXME: Localize these date formats, Bug 699196
                else if (dt.get_year() == new DateTime.now_local().get_year())


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