[bijiben/wip/sadiq/modernize] date-time: Rewrite logic to get date difference



commit 99066dfee29c8dbceca8780ac78c14e1b84d18df
Author: Mohammed Sadiq <sadiq sadiqpk org>
Date:   Fri Nov 24 13:07:56 2017 +0530

    date-time: Rewrite logic to get date difference
    
    Blindly getting week, month, and year based on number of days
    won't work right.
    
    Let's use GDate to deduce this the right way.
    
    Also GTimeVal is always 32 bit. Which means that the application
    will break on 2038. Let's fix this so that mission critical systems
    running bijiben won't fail on the date. :-)

 src/libbiji/biji-date-time.c |   44 ++++++++++++++++++++++++-----------------
 1 files changed, 26 insertions(+), 18 deletions(-)
---
diff --git a/src/libbiji/biji-date-time.c b/src/libbiji/biji-date-time.c
index abb4a6e..c08c2c6 100644
--- a/src/libbiji/biji-date-time.c
+++ b/src/libbiji/biji-date-time.c
@@ -1,5 +1,6 @@
 /* biji-date-time.c
  * Copyright (C) Pierre-Yves LUYTEN 2011 <py luyten fr>
+ * Copyright (C) 2017 Mohammed Sadiq <sadiq sadiqpk org>
  *
  * bijiben is free software: you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
@@ -21,29 +22,36 @@
 const gchar *
 biji_get_time_diff_with_time (glong sec_since_epoch)
 {
-  GTimeVal now;
-  glong    diff;
+  GDate *now = g_date_new ();
+  GDate *date = g_date_new ();
+  gchar *str;
+  gint   diff;
 
-  /* Retrieve the number of days */
-  g_get_current_time (&now);
-  diff = (now.tv_sec - sec_since_epoch) / 86400 ;
+  g_return_val_if_fail (sec_since_epoch >= 0, _("Unknown"));
 
-  if (diff < 1)
-    return _("Today");
+  g_date_set_time_t (date, sec_since_epoch);
+  g_date_set_time_t (now, time (NULL));
+  diff = g_date_days_between (date, now);
 
-  if (diff < 2)
-    return _("Yesterday");
+  if (diff == 0)
+    str = _("Today");
+  else if (diff == 1)
+    str = _("Yesterday");
+  else if (diff < 0 || g_date_get_year (date) != g_date_get_year (now))
+    str = _("Unknown");
+  else if (diff < 7 &&  /* FIXME: Won't work if the week overlap two years */
+           g_date_get_monday_week_of_year (date) == g_date_get_monday_week_of_year (now))
+    str = _("This week");
+  else if (diff < 31 &&
+           g_date_get_month (date) == g_date_get_month (now))
+    str = _("This month");
+  else
+    str = _("This year");
 
-  if (diff < 7)
-    return _("This week");
+  g_date_free (date);
+  g_date_free (now);
 
-  if (diff < 30)
-    return _("This month");
-
-  if (diff < 365)
-    return _("This year");
-
-  return _("Unknown");
+  return str;
 }
 
 


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