[gitg] Better fix for relative time in Gitg



commit 006943c3238ae1e670cb07d27d9a150eacd15a1f
Author: Sindhu S <sindhus live in>
Date:   Tue May 14 23:48:17 2013 +0530

    Better fix for relative time in Gitg
    
    Resolves edge cases of relating to difference in minutes.
    Treats minutes and hours in the way for rounding off.
    Cleans code readability.

 libgitg/gitg-commit.vala |   26 ++++++++++----------------
 1 files changed, 10 insertions(+), 16 deletions(-)
---
diff --git a/libgitg/gitg-commit.vala b/libgitg/gitg-commit.vala
index f2d0101..0cfed52 100644
--- a/libgitg/gitg-commit.vala
+++ b/libgitg/gitg-commit.vala
@@ -93,33 +93,27 @@ public class Commit : Ggit.Commit
                }
        }
 
-       private string date_for_display(DateTime dt, TimeZone time_zone)
+private string date_for_display(DateTime dt, TimeZone time_zone)
        {
                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 "A minute ago";
-               }
-               else if (time_in_seconds < 60 * 30)
+
+               if (t < TimeSpan.MINUTE * 29.5)
                {
-                       return "%d minutes ago".printf((int) Math.round(time_in_seconds / 60));
+                       int rounded_minutes = (int) Math.round((float) t / TimeSpan.MINUTE);
+                       return rounded_minutes <= 1 ? "A minute ago" : "%d minutes 
ago".printf(rounded_minutes);
                }
-               else if (time_in_seconds < 60 * 45)
+               else if (t < TimeSpan.MINUTE * 45)
                {
                        return "Half an hour ago";
                }
-               else if (time_in_hours < 23.5)
+               else if (t < TimeSpan.HOUR * 23.5)
                {
-                       int rounded_hours = (int) Math.round(time_in_hours);
+                       int rounded_hours = (int) Math.round((float) t / TimeSpan.HOUR);
                        return rounded_hours == 1 ? "An hour ago" : "%d hours ago".printf(rounded_hours);
                }
-               else if (time_in_hours < 24 * 7)
+               else if (t < TimeSpan.DAY * 7)
                {
-                       int rounded_days = (int) Math.round(time_in_hours / 24);
+                       int rounded_days = (int) Math.round((float) t / TimeSpan.DAY);
                        return rounded_days == 1 ? "A day ago" : "%d days ago".printf(rounded_days);
                }
                // FIXME: Localize these date formats, Bug 699196


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