mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-02 07:23:41 +02:00
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:
@@ -603,7 +603,7 @@ keyring_generate_entry (const gchar *cookie_context,
|
||||
gchar **lines;
|
||||
gint max_line_id;
|
||||
GString *new_contents;
|
||||
guint64 now;
|
||||
gint64 now;
|
||||
gboolean have_id;
|
||||
gint use_id;
|
||||
gchar *use_cookie;
|
||||
@@ -658,7 +658,7 @@ keyring_generate_entry (const gchar *cookie_context,
|
||||
}
|
||||
|
||||
new_contents = g_string_new (NULL);
|
||||
now = (guint64) time (NULL);
|
||||
now = g_get_real_time () / G_USEC_PER_SEC;
|
||||
changed_file = FALSE;
|
||||
|
||||
max_line_id = 0;
|
||||
@@ -672,7 +672,7 @@ keyring_generate_entry (const gchar *cookie_context,
|
||||
gchar **tokens;
|
||||
gchar *endp;
|
||||
gint line_id;
|
||||
guint64 line_when;
|
||||
gint64 line_when;
|
||||
gboolean keep_entry;
|
||||
|
||||
if (line[0] == '\0')
|
||||
@@ -807,9 +807,9 @@ keyring_generate_entry (const gchar *cookie_context,
|
||||
g_free (raw_cookie);
|
||||
|
||||
g_string_append_printf (new_contents,
|
||||
"%d %" G_GUINT64_FORMAT " %s\n",
|
||||
"%d %" G_GINT64_FORMAT " %s\n",
|
||||
*out_id,
|
||||
(guint64) time (NULL),
|
||||
g_get_real_time () / G_USEC_PER_SEC,
|
||||
*out_cookie);
|
||||
changed_file = TRUE;
|
||||
}
|
||||
|
@@ -1909,7 +1909,7 @@ g_local_file_trash (GFile *file,
|
||||
int i;
|
||||
char *data;
|
||||
gboolean is_homedir_trash;
|
||||
char delete_time[32];
|
||||
char *delete_time = NULL;
|
||||
int fd;
|
||||
GStatBuf trash_stat, global_stat;
|
||||
char *dirname, *globaldir;
|
||||
@@ -2137,16 +2137,17 @@ g_local_file_trash (GFile *file,
|
||||
g_free (topdir);
|
||||
|
||||
{
|
||||
time_t t;
|
||||
struct tm now;
|
||||
t = time (NULL);
|
||||
localtime_r (&t, &now);
|
||||
delete_time[0] = 0;
|
||||
strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
|
||||
GDateTime *now = g_date_time_new_now_local ();
|
||||
if (now != NULL)
|
||||
delete_time = g_date_time_format (now, "%Y-%m-%dT%H:%M:%S");
|
||||
else
|
||||
delete_time = g_strdup ("9999-12-31T23:59:59");
|
||||
g_date_time_unref (now);
|
||||
}
|
||||
|
||||
data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
|
||||
original_name_escaped, delete_time);
|
||||
g_free (delete_time);
|
||||
|
||||
g_file_set_contents (infofile, data, -1, NULL);
|
||||
|
||||
|
@@ -273,7 +273,8 @@ main (int argc, char *argv[])
|
||||
"Negotiated capabilities: unix-fd-passing=%d\n",
|
||||
g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
|
||||
|
||||
greeting = g_strdup_printf ("Hey, it's %" G_GUINT64_FORMAT " already!", (guint64) time (NULL));
|
||||
greeting = g_strdup_printf ("Hey, it's %" G_GINT64_FORMAT " already!",
|
||||
g_get_real_time () / G_USEC_PER_SEC);
|
||||
value = g_dbus_connection_call_sync (connection,
|
||||
NULL, /* bus_name */
|
||||
"/org/gtk/GDBus/TestObject",
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user