[gedit] Document: use GDateTime, not GTimeVal (deprecated)



commit d23535a9853f1c8d7b2d7700eb71ad6a961b3e93
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Mon Dec 2 08:56:01 2019 +0100

    Document: use GDateTime, not GTimeVal (deprecated)
    
    GTimeVal is not year-2038-safe.

 gedit/gedit-document.c | 47 +++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 39 insertions(+), 8 deletions(-)
---
diff --git a/gedit/gedit-document.c b/gedit/gedit-document.c
index 5a3dd1cf9..ebd492e3e 100644
--- a/gedit/gedit-document.c
+++ b/gedit/gedit-document.c
@@ -56,7 +56,7 @@ typedef struct
 
        gchar       *content_type;
 
-       GTimeVal     time_of_last_save_or_load;
+       GDateTime   *time_of_last_save_or_load;
 
        /* The search context for the incremental search, or the search and
         * replace. They are mutually exclusive.
@@ -140,6 +140,19 @@ release_untitled_number (gint n)
        g_hash_table_remove (allocated_untitled_numbers, GINT_TO_POINTER (n));
 }
 
+static void
+update_time_of_last_save_or_load (GeditDocument *doc)
+{
+       GeditDocumentPrivate *priv = gedit_document_get_instance_private (doc);
+
+       if (priv->time_of_last_save_or_load != NULL)
+       {
+               g_date_time_unref (priv->time_of_last_save_or_load);
+       }
+
+       priv->time_of_last_save_or_load = g_date_time_new_now_utc ();
+}
+
 static const gchar *
 get_language_string (GeditDocument *doc)
 {
@@ -227,6 +240,11 @@ gedit_document_finalize (GObject *object)
 
        g_free (priv->content_type);
 
+       if (priv->time_of_last_save_or_load != NULL)
+       {
+               g_date_time_unref (priv->time_of_last_save_or_load);
+       }
+
        G_OBJECT_CLASS (gedit_document_parent_class)->finalize (object);
 }
 
@@ -761,7 +779,7 @@ gedit_document_init (GeditDocument *doc)
        priv->language_set_by_user = FALSE;
        priv->empty_search = TRUE;
 
-       g_get_current_time (&priv->time_of_last_save_or_load);
+       update_time_of_last_save_or_load (doc);
 
        priv->file = gtk_source_file_new ();
 
@@ -1081,8 +1099,7 @@ gedit_document_loaded_real (GeditDocument *doc)
                set_language (doc, language, FALSE);
        }
 
-       g_get_current_time (&priv->time_of_last_save_or_load);
-
+       update_time_of_last_save_or_load (doc);
        set_content_type (doc, NULL);
 
        location = gtk_source_file_get_location (priv->file);
@@ -1138,7 +1155,7 @@ saved_query_info_cb (GFile         *location,
                g_object_unref (info);
        }
 
-       g_get_current_time (&priv->time_of_last_save_or_load);
+       update_time_of_last_save_or_load (doc);
 
        priv->create = FALSE;
 
@@ -1303,16 +1320,30 @@ glong
 _gedit_document_get_seconds_since_last_save_or_load (GeditDocument *doc)
 {
        GeditDocumentPrivate *priv;
-       GTimeVal current_time;
+       GDateTime *now;
+       GTimeSpan n_microseconds;
+
        gedit_debug (DEBUG_DOCUMENT);
 
        g_return_val_if_fail (GEDIT_IS_DOCUMENT (doc), -1);
 
        priv = gedit_document_get_instance_private (doc);
 
-       g_get_current_time (&current_time);
+       if (priv->time_of_last_save_or_load == NULL)
+       {
+               return -1;
+       }
+
+       now = g_date_time_new_now_utc ();
+       if (now == NULL)
+       {
+               return -1;
+       }
+
+       n_microseconds = g_date_time_difference (now, priv->time_of_last_save_or_load);
+       g_date_time_unref (now);
 
-       return (current_time.tv_sec - priv->time_of_last_save_or_load.tv_sec);
+       return n_microseconds / (1000 * 1000);
 }
 
 /**


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