glib: Don’t use time(NULL) to get current time

Use either g_get_real_time() or g_date_time_new_now_local(). This means
we don’t need to worry about time_t being 32b in future (the year 2038
problem), and it makes the need for error handling a bit more explicit.
Improve the error handling in several cases.

Based on a patch by Niels De Graef
(https://gitlab.gnome.org/GNOME/glib/merge_requests/142).

Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://gitlab.gnome.org/GNOME/glib/issues/1402
This commit is contained in:
Philip Withnall
2018-07-06 13:49:55 +01:00
parent c5321810f4
commit 92e059280f
7 changed files with 33 additions and 22 deletions

View File

@@ -74,16 +74,14 @@ on_name_appeared (GDBusConnection *connection,
}
else
{
gchar now_buf[256];
time_t now;
gchar *now_buf = NULL;
gssize len;
gchar *str;
GDateTime *now = g_date_time_new_now_local ();
now = time (NULL);
strftime (now_buf,
sizeof now_buf,
"%Y-%m-%d %H:%M:%S",
localtime (&now));
g_assert_nonnull (now);
now_buf = g_date_time_format (now, "%Y-%m-%d %H:%M:%S");
g_date_time_unref (now);
str = g_strdup_printf ("On %s, gdbus-example-unix-fd-client with pid %d was here!\n",
now_buf,
@@ -95,6 +93,7 @@ on_name_appeared (GDBusConnection *connection,
g_print ("Wrote the following on server's stdout:\n%s", str);
g_free (str);
g_free (now_buf);
exit (0);
}
}