mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-01-27 06:26:15 +01: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:
parent
c5321810f4
commit
92e059280f
@ -603,7 +603,7 @@ keyring_generate_entry (const gchar *cookie_context,
|
|||||||
gchar **lines;
|
gchar **lines;
|
||||||
gint max_line_id;
|
gint max_line_id;
|
||||||
GString *new_contents;
|
GString *new_contents;
|
||||||
guint64 now;
|
gint64 now;
|
||||||
gboolean have_id;
|
gboolean have_id;
|
||||||
gint use_id;
|
gint use_id;
|
||||||
gchar *use_cookie;
|
gchar *use_cookie;
|
||||||
@ -658,7 +658,7 @@ keyring_generate_entry (const gchar *cookie_context,
|
|||||||
}
|
}
|
||||||
|
|
||||||
new_contents = g_string_new (NULL);
|
new_contents = g_string_new (NULL);
|
||||||
now = (guint64) time (NULL);
|
now = g_get_real_time () / G_USEC_PER_SEC;
|
||||||
changed_file = FALSE;
|
changed_file = FALSE;
|
||||||
|
|
||||||
max_line_id = 0;
|
max_line_id = 0;
|
||||||
@ -672,7 +672,7 @@ keyring_generate_entry (const gchar *cookie_context,
|
|||||||
gchar **tokens;
|
gchar **tokens;
|
||||||
gchar *endp;
|
gchar *endp;
|
||||||
gint line_id;
|
gint line_id;
|
||||||
guint64 line_when;
|
gint64 line_when;
|
||||||
gboolean keep_entry;
|
gboolean keep_entry;
|
||||||
|
|
||||||
if (line[0] == '\0')
|
if (line[0] == '\0')
|
||||||
@ -807,9 +807,9 @@ keyring_generate_entry (const gchar *cookie_context,
|
|||||||
g_free (raw_cookie);
|
g_free (raw_cookie);
|
||||||
|
|
||||||
g_string_append_printf (new_contents,
|
g_string_append_printf (new_contents,
|
||||||
"%d %" G_GUINT64_FORMAT " %s\n",
|
"%d %" G_GINT64_FORMAT " %s\n",
|
||||||
*out_id,
|
*out_id,
|
||||||
(guint64) time (NULL),
|
g_get_real_time () / G_USEC_PER_SEC,
|
||||||
*out_cookie);
|
*out_cookie);
|
||||||
changed_file = TRUE;
|
changed_file = TRUE;
|
||||||
}
|
}
|
||||||
|
@ -1909,7 +1909,7 @@ g_local_file_trash (GFile *file,
|
|||||||
int i;
|
int i;
|
||||||
char *data;
|
char *data;
|
||||||
gboolean is_homedir_trash;
|
gboolean is_homedir_trash;
|
||||||
char delete_time[32];
|
char *delete_time = NULL;
|
||||||
int fd;
|
int fd;
|
||||||
GStatBuf trash_stat, global_stat;
|
GStatBuf trash_stat, global_stat;
|
||||||
char *dirname, *globaldir;
|
char *dirname, *globaldir;
|
||||||
@ -2137,16 +2137,17 @@ g_local_file_trash (GFile *file,
|
|||||||
g_free (topdir);
|
g_free (topdir);
|
||||||
|
|
||||||
{
|
{
|
||||||
time_t t;
|
GDateTime *now = g_date_time_new_now_local ();
|
||||||
struct tm now;
|
if (now != NULL)
|
||||||
t = time (NULL);
|
delete_time = g_date_time_format (now, "%Y-%m-%dT%H:%M:%S");
|
||||||
localtime_r (&t, &now);
|
else
|
||||||
delete_time[0] = 0;
|
delete_time = g_strdup ("9999-12-31T23:59:59");
|
||||||
strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
|
g_date_time_unref (now);
|
||||||
}
|
}
|
||||||
|
|
||||||
data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
|
data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
|
||||||
original_name_escaped, delete_time);
|
original_name_escaped, delete_time);
|
||||||
|
g_free (delete_time);
|
||||||
|
|
||||||
g_file_set_contents (infofile, data, -1, NULL);
|
g_file_set_contents (infofile, data, -1, NULL);
|
||||||
|
|
||||||
|
@ -273,7 +273,8 @@ main (int argc, char *argv[])
|
|||||||
"Negotiated capabilities: unix-fd-passing=%d\n",
|
"Negotiated capabilities: unix-fd-passing=%d\n",
|
||||||
g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
|
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,
|
value = g_dbus_connection_call_sync (connection,
|
||||||
NULL, /* bus_name */
|
NULL, /* bus_name */
|
||||||
"/org/gtk/GDBus/TestObject",
|
"/org/gtk/GDBus/TestObject",
|
||||||
|
@ -74,16 +74,14 @@ on_name_appeared (GDBusConnection *connection,
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
gchar now_buf[256];
|
gchar *now_buf = NULL;
|
||||||
time_t now;
|
|
||||||
gssize len;
|
gssize len;
|
||||||
gchar *str;
|
gchar *str;
|
||||||
|
GDateTime *now = g_date_time_new_now_local ();
|
||||||
|
|
||||||
now = time (NULL);
|
g_assert_nonnull (now);
|
||||||
strftime (now_buf,
|
now_buf = g_date_time_format (now, "%Y-%m-%d %H:%M:%S");
|
||||||
sizeof now_buf,
|
g_date_time_unref (now);
|
||||||
"%Y-%m-%d %H:%M:%S",
|
|
||||||
localtime (&now));
|
|
||||||
|
|
||||||
str = g_strdup_printf ("On %s, gdbus-example-unix-fd-client with pid %d was here!\n",
|
str = g_strdup_printf ("On %s, gdbus-example-unix-fd-client with pid %d was here!\n",
|
||||||
now_buf,
|
now_buf,
|
||||||
@ -95,6 +93,7 @@ on_name_appeared (GDBusConnection *connection,
|
|||||||
g_print ("Wrote the following on server's stdout:\n%s", str);
|
g_print ("Wrote the following on server's stdout:\n%s", str);
|
||||||
|
|
||||||
g_free (str);
|
g_free (str);
|
||||||
|
g_free (now_buf);
|
||||||
exit (0);
|
exit (0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1387,7 +1387,10 @@ g_date_set_parse (GDate *d,
|
|||||||
*
|
*
|
||||||
* To set the value of a date to the current day, you could write:
|
* To set the value of a date to the current day, you could write:
|
||||||
* |[<!-- language="C" -->
|
* |[<!-- language="C" -->
|
||||||
* g_date_set_time_t (date, time (NULL));
|
* time_t now = time (NULL);
|
||||||
|
* if (now == (time_t) -1)
|
||||||
|
* // handle the error
|
||||||
|
* g_date_set_time_t (date, now);
|
||||||
* ]|
|
* ]|
|
||||||
*
|
*
|
||||||
* Since: 2.10
|
* Since: 2.10
|
||||||
|
@ -89,11 +89,14 @@ test_dates (void)
|
|||||||
{
|
{
|
||||||
GDate *d;
|
GDate *d;
|
||||||
GTimeVal tv;
|
GTimeVal tv;
|
||||||
|
time_t now;
|
||||||
|
|
||||||
d = g_date_new ();
|
d = g_date_new ();
|
||||||
|
|
||||||
/* today */
|
/* today */
|
||||||
g_date_set_time (d, time (NULL));
|
now = time (NULL);
|
||||||
|
g_assert_cmpint (now, !=, (time_t) -1);
|
||||||
|
g_date_set_time (d, now);
|
||||||
g_assert (g_date_valid (d));
|
g_assert (g_date_valid (d));
|
||||||
|
|
||||||
/* Unix epoch */
|
/* Unix epoch */
|
||||||
|
@ -112,6 +112,7 @@ test_GDateTime_new_from_unix (void)
|
|||||||
|
|
||||||
memset (&tm, 0, sizeof (tm));
|
memset (&tm, 0, sizeof (tm));
|
||||||
t = time (NULL);
|
t = time (NULL);
|
||||||
|
g_assert_cmpint (t, !=, (time_t) -1);
|
||||||
get_localtime_tm (t, &tm);
|
get_localtime_tm (t, &tm);
|
||||||
|
|
||||||
dt = g_date_time_new_from_unix_local (t);
|
dt = g_date_time_new_from_unix_local (t);
|
||||||
@ -787,6 +788,7 @@ test_GDateTime_to_unix (void)
|
|||||||
time_t t;
|
time_t t;
|
||||||
|
|
||||||
t = time (NULL);
|
t = time (NULL);
|
||||||
|
g_assert_cmpint (t, !=, (time_t) -1);
|
||||||
dt = g_date_time_new_from_unix_local (t);
|
dt = g_date_time_new_from_unix_local (t);
|
||||||
g_assert_cmpint (g_date_time_to_unix (dt), ==, t);
|
g_assert_cmpint (g_date_time_to_unix (dt), ==, t);
|
||||||
g_date_time_unref (dt);
|
g_date_time_unref (dt);
|
||||||
@ -1283,6 +1285,7 @@ test_GDateTime_to_utc (void)
|
|||||||
struct tm tm;
|
struct tm tm;
|
||||||
|
|
||||||
t = time (NULL);
|
t = time (NULL);
|
||||||
|
g_assert_cmpint (t, !=, (time_t) -1);
|
||||||
#ifdef HAVE_GMTIME_R
|
#ifdef HAVE_GMTIME_R
|
||||||
gmtime_r (&t, &tm);
|
gmtime_r (&t, &tm);
|
||||||
#else
|
#else
|
||||||
@ -1362,6 +1365,7 @@ GDateTime *__dt = g_date_time_new_local (2009, 10, 24, 0, 0, 0);\
|
|||||||
* that of the generated timezone.
|
* that of the generated timezone.
|
||||||
*/
|
*/
|
||||||
t = time (NULL);
|
t = time (NULL);
|
||||||
|
g_assert_cmpint (t, !=, (time_t) -1);
|
||||||
memset (&tt, 0, sizeof(tt));
|
memset (&tt, 0, sizeof(tt));
|
||||||
get_localtime_tm (t, &tt);
|
get_localtime_tm (t, &tt);
|
||||||
tt.tm_year = 2009 - 1900;
|
tt.tm_year = 2009 - 1900;
|
||||||
|
Loading…
Reference in New Issue
Block a user