[Spice-devel] [PATCH spice-gtk] file-xfer: Simplify time handling

Pavel Grunt pgrunt at redhat.com
Tue Aug 25 10:05:37 PDT 2015


Use g_get_monotonic_time() instead of g_date_time_new_now_local().
g_get_monotonic_time doesn't suffer discontinuities and it is sufficient
for our purposes. There is no need for the complexity of GDateTime.
---
 src/channel-main.c | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)

diff --git a/src/channel-main.c b/src/channel-main.c
index f5115aa..91e57b1 100644
--- a/src/channel-main.c
+++ b/src/channel-main.c
@@ -71,8 +71,8 @@ typedef struct SpiceFileXferTask {
     char                           buffer[FILE_XFER_CHUNK_SIZE];
     uint64_t                       read_bytes;
     uint64_t                       file_size;
-    GDateTime                      *start_time;
-    GDateTime                      *last_update;
+    gint64                         start_time;
+    gint64                         last_update;
     GError                         *error;
 } SpiceFileXferTask;
 
@@ -1697,10 +1697,6 @@ static void file_xfer_task_free(SpiceFileXferTask *task)
     g_clear_object(&task->channel);
     g_clear_object(&task->file);
     g_clear_object(&task->file_stream);
-    if (spice_util_get_debug()) {
-        g_date_time_unref(task->start_time);
-        g_date_time_unref(task->last_update);
-    }
     g_free(task);
 }
 
@@ -1737,10 +1733,9 @@ static void file_xfer_close_cb(GObject      *object,
     } else {
         g_simple_async_result_set_op_res_gboolean(res, TRUE);
         if (spice_util_get_debug()) {
-            GDateTime *now = g_date_time_new_now_local();
+            gint64 now = g_get_monotonic_time();
             gchar *basename = g_file_get_basename(task->file);
-            double seconds =
-                (double) g_date_time_difference(now, task->start_time) / G_TIME_SPAN_SECOND;
+            double seconds = (double) (now - task->start_time) / G_TIME_SPAN_SECOND;
             gchar *file_size_str = g_format_size(task->file_size);
             gchar *transfer_speed_str = g_format_size(task->file_size / seconds);
 
@@ -1751,7 +1746,6 @@ static void file_xfer_close_cb(GObject      *object,
             g_free(basename);
             g_free(file_size_str);
             g_free(transfer_speed_str);
-            g_date_time_unref(now);
         }
     }
     g_simple_async_result_complete_in_idle(res);
@@ -1777,17 +1771,15 @@ static void file_xfer_data_flushed_cb(GObject *source_object,
 
     if (spice_util_get_debug()) {
         const GTimeSpan interval = 20 * G_TIME_SPAN_SECOND;
-        GDateTime *now = g_date_time_new_now_local();
+        gint64 now = g_get_monotonic_time();
 
-        if (interval < g_date_time_difference(now, task->last_update)) {
+        if (interval < now - task->last_update) {
             gchar *basename = g_file_get_basename(task->file);
-            g_date_time_unref(task->last_update);
-            task->last_update = g_date_time_ref(now);
+            task->last_update = now;
             SPICE_DEBUG("transferred %.2f%% of the file %s",
                         100.0 * task->read_bytes / task->file_size, basename);
             g_free(basename);
         }
-        g_date_time_unref(now);
     }
 
     if (task->progress_callback)
@@ -2956,8 +2948,8 @@ static void file_xfer_send_start_msg_async(SpiceMainChannel *channel,
 
         if (spice_util_get_debug()) {
             gchar *basename = g_file_get_basename(task->file);
-            task->start_time = g_date_time_new_now_local();
-            task->last_update = g_date_time_ref(task->start_time);
+            task->start_time = g_get_monotonic_time();
+            task->last_update = task->start_time;
 
             SPICE_DEBUG("transfer of file %s has started", basename);
             g_free(basename);
-- 
2.5.0



More information about the Spice-devel mailing list