From c09e5228537e4bcf5c9a074c696b698ff8ead1a5 Mon Sep 17 00:00:00 2001
From: rbuj <robert.buj@gmail.com>
Date: Fri, 19 Apr 2019 21:01:10 +0200
Subject: Use g_date_time_format instead of filter_date

https://developer.gnome.org/glib/stable/glib-GDateTime.html#g-date-time-format
---
 src/Makefile.am        |   1 -
 src/e_date.c           | 207 -------------------------------------------------
 src/e_date.h           |  14 ----
 src/procproperties.cpp |   1 -
 src/util.cpp           |  35 ++++++++-
 src/util.h             |   4 +
 6 files changed, 35 insertions(+), 227 deletions(-)
 delete mode 100644 src/e_date.c
 delete mode 100644 src/e_date.h

(limited to 'src')

diff --git a/src/Makefile.am b/src/Makefile.am
index b32983d..0518134 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -38,7 +38,6 @@ mate_system_monitor_cpp_files = \
 	iconthemewrapper.cpp
 
 mate_system_monitor_c_files = \
-	e_date.c \
 	gsm_color_button.c
 
 mate_system_monitor_h_files = \
diff --git a/src/e_date.c b/src/e_date.c
deleted file mode 100644
index c145c8b..0000000
--- a/src/e_date.c
+++ /dev/null
@@ -1,207 +0,0 @@
-#include <config.h>
-
-#include <glib.h>
-#include <glib/gi18n.h>
-
-#include <string.h>
-
-#include "e_date.h"
-
-/*
-  all this code comes from evolution
-  - e-util.c
-  - message-list.c
-*/
-
-
-static size_t e_strftime(char *s, size_t max, const char *fmt, const struct tm *tm)
-{
-#ifdef HAVE_LKSTRFTIME
-    return strftime(s, max, fmt, tm);
-#else
-    char *c, *ffmt, *ff;
-    size_t ret;
-
-    ffmt = g_strdup(fmt);
-    ff = ffmt;
-    while ((c = strstr(ff, "%l")) != NULL) {
-        c[1] = 'I';
-        ff = c;
-    }
-
-    ff = ffmt;
-    while ((c = strstr(ff, "%k")) != NULL) {
-        c[1] = 'H';
-        ff = c;
-    }
-
-    ret = strftime(s, max, ffmt, tm);
-    g_free(ffmt);
-    return ret;
-#endif
-}
-
-
-/**
- * Function to do a last minute fixup of the AM/PM stuff if the locale
- * and gettext haven't done it right. Most English speaking countries
- * except the USA use the 24 hour clock (UK, Australia etc). However
- * since they are English nobody bothers to write a language
- * translation (gettext) file. So the locale turns off the AM/PM, but
- * gettext does not turn on the 24 hour clock. Leaving a mess.
- *
- * This routine checks if AM/PM are defined in the locale, if not it
- * forces the use of the 24 hour clock.
- *
- * The function itself is a front end on strftime and takes exactly
- * the same arguments.
- *
- * TODO: Actually remove the '%p' from the fixed up string so that
- * there isn't a stray space.
- **/
-
-static size_t e_strftime_fix_am_pm(char *s, size_t max, const char *fmt, const struct tm *tm)
-{
-    char buf[10];
-    char *sp;
-    char *ffmt;
-    size_t ret;
-
-    if (strstr(fmt, "%p")==NULL && strstr(fmt, "%P")==NULL) {
-        /* No AM/PM involved - can use the fmt string directly */
-        ret=e_strftime(s, max, fmt, tm);
-    } else {
-        /* Get the AM/PM symbol from the locale */
-        e_strftime (buf, 10, "%p", tm);
-
-        if (buf[0]) {
-            /**
-             * AM/PM have been defined in the locale
-             * so we can use the fmt string directly
-             **/
-            ret=e_strftime(s, max, fmt, tm);
-        } else {
-            /**
-             * No AM/PM defined by locale
-             * must change to 24 hour clock
-             **/
-            ffmt=g_strdup(fmt);
-            for (sp=ffmt; (sp=strstr(sp, "%l")); sp++) {
-                /**
-                 * Maybe this should be 'k', but I have never
-                 * seen a 24 clock actually use that format
-                 **/
-                sp[1]='H';
-            }
-            for (sp=ffmt; (sp=strstr(sp, "%I")); sp++) {
-                sp[1]='H';
-            }
-            ret=e_strftime(s, max, ffmt, tm);
-            g_free(ffmt);
-        }
-    }
-    return(ret);
-}
-
-static size_t
-e_utf8_strftime_fix_am_pm(char *s, size_t max, const char *fmt, const struct tm *tm)
-{
-    size_t sz, ret;
-    char *locale_fmt, *buf;
-
-    locale_fmt = g_locale_from_utf8(fmt, -1, NULL, &sz, NULL);
-    if (!locale_fmt)
-        return 0;
-
-    ret = e_strftime_fix_am_pm(s, max, locale_fmt, tm);
-    if (!ret) {
-        g_free (locale_fmt);
-        return 0;
-    }
-
-    buf = g_locale_to_utf8(s, ret, NULL, &sz, NULL);
-    if (!buf) {
-        g_free (locale_fmt);
-        return 0;
-    }
-
-    if (sz >= max) {
-        char *tmp = buf + max - 1;
-        tmp = g_utf8_find_prev_char(buf, tmp);
-        if (tmp)
-            sz = tmp - buf;
-        else
-            sz = 0;
-    }
-    memcpy(s, buf, sz);
-    s[sz] = '\0';
-    g_free(locale_fmt);
-    g_free(buf);
-    return sz;
-}
-
-
-static char *
-filter_date (time_t date)
-{
-    time_t nowdate = time(NULL);
-    time_t yesdate;
-    struct tm then, now, yesterday;
-    char buf[26];
-    gboolean done = FALSE;
-
-    if (date == 0)
-        // xgettext: ? stands for unknown
-        return g_strdup (_("?"));
-
-    localtime_r (&date, &then);
-    localtime_r (&nowdate, &now);
-    if (then.tm_mday == now.tm_mday &&
-        then.tm_mon == now.tm_mon &&
-        then.tm_year == now.tm_year) {
-        e_utf8_strftime_fix_am_pm (buf, 26, _("Today %l:%M %p"), &then);
-        done = TRUE;
-    }
-    if (!done) {
-        yesdate = nowdate - 60 * 60 * 24;
-        localtime_r (&yesdate, &yesterday);
-        if (then.tm_mday == yesterday.tm_mday &&
-            then.tm_mon == yesterday.tm_mon &&
-            then.tm_year == yesterday.tm_year) {
-            e_utf8_strftime_fix_am_pm (buf, 26, _("Yesterday %l:%M %p"), &then);
-            done = TRUE;
-        }
-    }
-    if (!done) {
-        int i;
-        for (i = 2; i < 7; i++) {
-            yesdate = nowdate - 60 * 60 * 24 * i;
-            localtime_r (&yesdate, &yesterday);
-            if (then.tm_mday == yesterday.tm_mday &&
-                then.tm_mon == yesterday.tm_mon &&
-                then.tm_year == yesterday.tm_year) {
-                e_utf8_strftime_fix_am_pm (buf, 26, _("%a %l:%M %p"), &then);
-                done = TRUE;
-                break;
-            }
-        }
-    }
-    if (!done) {
-        if (then.tm_year == now.tm_year) {
-            e_utf8_strftime_fix_am_pm (buf, 26, _("%b %d %l:%M %p"), &then);
-        } else {
-            e_utf8_strftime_fix_am_pm (buf, 26, _("%b %d %Y"), &then);
-        }
-    }
-
-    return g_strdup (buf);
-}
-
-
-
-
-char *
-procman_format_date_for_display(time_t d)
-{
-    return filter_date(d);
-}
diff --git a/src/e_date.h b/src/e_date.h
deleted file mode 100644
index 2332612..0000000
--- a/src/e_date.h
+++ /dev/null
@@ -1,14 +0,0 @@
-#ifndef H_PROCMAN_E_DATE_1135695432
-#define H_PROCMAN_E_DATE_1135695432
-
-#include <time.h>
-#include <glib.h>
-
-G_BEGIN_DECLS
-
-char *
-procman_format_date_for_display(time_t d);
-
-G_END_DECLS
-
-#endif /* H_PROCMAN_E_DATE_1135695432 */
diff --git a/src/procproperties.cpp b/src/procproperties.cpp
index d07da69..69266d7 100644
--- a/src/procproperties.cpp
+++ b/src/procproperties.cpp
@@ -29,7 +29,6 @@
 #include "procproperties.h"
 #include "proctable.h"
 #include "util.h"
-#include "e_date.h"
 
 enum
 {
diff --git a/src/util.cpp b/src/util.cpp
index 14c559d..8548249 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -1,5 +1,6 @@
 #include <config.h>
 
+#include <glib/gprintf.h>
 #include <glib/gi18n.h>
 #include <glib.h>
 #include <gtk/gtk.h>
@@ -14,10 +15,36 @@
 #include "util.h"
 #include "procman.h"
 
-extern "C" {
-#include "e_date.h"
-}
+gchar *
+procman_format_date_for_display(time_t time_raw)
+{
+    gchar *result = NULL;
+    const char *format;
+    GDateTime *date_time, *today;
+    GTimeSpan date_age;
+
+    date_time = g_date_time_new_from_unix_local (time_raw);
+    today = g_date_time_new_now_local ();
+
+    date_age = g_date_time_difference (today, date_time);
+    if (date_age < G_TIME_SPAN_DAY) {
+        format = _("Today %l:%M %p");
+    } else if (date_age < 2 * G_TIME_SPAN_DAY) {
+        format = _("Yesterday %l:%M %p");
+    } else if (date_age < 7 * G_TIME_SPAN_DAY) {
+        format = _("%a %l:%M %p");
+    } else if (g_date_time_get_year (date_time) == g_date_time_get_year (today)) {
+        format = _("%b %d %l:%M %p");
+    } else {
+	format = _("%b %d %Y");
+    }
+
+    g_date_time_unref (today);
+    result = g_date_time_format (date_time, format);
+    g_date_time_unref (date_time);
 
+    return result;
+}
 
 const char*
 format_process_state(guint state)
@@ -518,7 +545,7 @@ namespace procman
 
         g_value_unset(&value);
 
-        char *str = procman_format_date_for_display(time);
+        gchar *str = procman_format_date_for_display(time);
         g_object_set(renderer, "text", str, NULL);
         g_free(str);
     }
diff --git a/src/util.h b/src/util.h
index 0381698..75975d7 100644
--- a/src/util.h
+++ b/src/util.h
@@ -5,6 +5,7 @@
 
 #include <glib.h>
 #include <gtk/gtk.h>
+#include <time.h>
 #include <string>
 
 using std::string;
@@ -20,6 +21,9 @@ procman_make_label_for_mmaps_or_ofiles(const char *format,
 gboolean
 load_symbols(const char *module, ...) G_GNUC_NULL_TERMINATED;
 
+gchar *
+procman_format_date_for_display(time_t time_raw);
+
 const char*
 format_process_state(guint state);
 
-- 
cgit v1.2.1