[evince] libdocument: Replace deprecated GTime by GDateTime



commit 35b297759e460011c28c743bcc595aee53461b2d
Author: vanadiae <vanadiae35 gmail com>
Date:   Wed Jun 10 19:51:51 2020 +0200

    libdocument: Replace deprecated GTime by GDateTime
    
    Add a new function to localize the time, and deprecate the old
    one adding corresponding safeguards for GTime.
    
    GTime is a 32bits in on some systems, and it is not safe year-2038 safe.
    
    Signed-off-by: Germán Poo-Caamaño <gpoo gnome org>

 libdocument/ev-document-misc.c | 51 +++++++++++++++++++++++++-----------------
 libdocument/ev-document-misc.h |  4 +++-
 meson.build                    |  2 --
 3 files changed, 34 insertions(+), 23 deletions(-)
---
diff --git a/libdocument/ev-document-misc.c b/libdocument/ev-document-misc.c
index bcb11cb4..cf983bde 100644
--- a/libdocument/ev-document-misc.c
+++ b/libdocument/ev-document-misc.c
@@ -596,30 +596,41 @@ ev_document_misc_get_widget_dpi (GtkWidget *widget)
        }
 }
 
-/* Returns a locale specific date and time representation */
+/**
+ * ev_document_misc_format_date:
+ * @utime: a #GTime
+ *
+ * Returns: (transfer full): a locale specific date and time representation.
+ *
+ * Deprecated: 3.38: use ev_document_misc_format_datetime instead as GTime is
+ *                   deprecated because it is not year-2038 safe.
+ */
+G_GNUC_BEGIN_IGNORE_DEPRECATIONS
 gchar *
 ev_document_misc_format_date (GTime utime)
 {
-       time_t time = (time_t) utime;
-       char s[256];
-       const char fmt_hack[] = "%c";
-       size_t len;
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wformat-y2k"
-#ifdef HAVE_LOCALTIME_R
-       struct tm t;
-       if (time == 0 || !localtime_r (&time, &t)) return NULL;
-       len = strftime (s, sizeof (s), fmt_hack, &t);
-#else
-       struct tm *t;
-       if (time == 0 || !(t = localtime (&time)) ) return NULL;
-       len = strftime (s, sizeof (s), fmt_hack, t);
-#endif
-#pragma GCC diagnostic pop
-
-       if (len == 0 || s[0] == '\0') return NULL;
+       g_autoptr (GDateTime) dt = g_date_time_new_from_unix_utc ((gint64)utime);
+       return ev_document_misc_format_datetime (dt);
+}
+G_GNUC_END_IGNORE_DEPRECATIONS
 
-       return g_locale_to_utf8 (s, -1, NULL, NULL, NULL);
+/**
+ * ev_document_misc_format_datetime:
+ * @dt: a #GDateTime
+ *
+ * Determine the preferred date and time representation for the current locale
+ * for @dt.
+ *
+ * Returns: (transfer full): a new allocated string or NULL in the case
+ * that there was an error (such as a format specifier not being supported
+ * in the current locale). The string should be freed with g_free().
+ *
+ * Since: 3.38
+ */
+gchar *
+ev_document_misc_format_datetime (GDateTime *dt)
+{
+       return g_date_time_format (dt, "%c");
 }
 
 /**
diff --git a/libdocument/ev-document-misc.h b/libdocument/ev-document-misc.h
index 4c644eb9..e6d0d34f 100644
--- a/libdocument/ev-document-misc.h
+++ b/libdocument/ev-document-misc.h
@@ -85,8 +85,10 @@ gdouble          ev_document_misc_get_screen_dpi (GdkScreen *screen);
 gdouble          ev_document_misc_get_widget_dpi (GtkWidget *widget);
 
 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
-gchar           *ev_document_misc_format_date (GTime utime);
+EV_DEPRECATED_FOR (ev_document_misc_format_datetime)
+gchar           *ev_document_misc_format_date     (GTime utime);
 G_GNUC_END_IGNORE_DEPRECATIONS
+gchar           *ev_document_misc_format_datetime (GDateTime *dt);
 
 void             ev_document_misc_get_pointer_position (GtkWidget *widget,
                                                        gint      *x,
diff --git a/meson.build b/meson.build
index d1676186..64c2e670 100644
--- a/meson.build
+++ b/meson.build
@@ -78,8 +78,6 @@ config_h.set_quoted('VERSION', ev_version)
 config_h.set_quoted('GETTEXT_PACKAGE', ev_name)
 config_h.set('ENABLE_NLS', true)
 
-config_h.set('HAVE_LOCALTIME_R', cc.has_function('localtime_r'))
-
 # portability checks
 # FIXME: there is no use of backtrace in the code
 # for backtrace()


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