Merge branch '1438-deprecate-gtimeval' into 'master'

Deprecate GTimeVal- and GTime-based APIs

Closes #1438

See merge request GNOME/glib!1004
This commit is contained in:
Philip Withnall 2019-07-29 12:59:50 +00:00
commit 018eaf5169
27 changed files with 395 additions and 145 deletions

View File

@ -395,6 +395,7 @@ g_file_info_get_symbolic_icon
g_file_info_get_content_type
g_file_info_get_size
g_file_info_get_modification_time
g_file_info_get_modification_date_time
g_file_info_get_symlink_target
g_file_info_get_etag
g_file_info_get_sort_order
@ -412,6 +413,7 @@ g_file_info_set_symbolic_icon
g_file_info_set_content_type
g_file_info_set_size
g_file_info_set_modification_time
g_file_info_set_modification_date_time
g_file_info_set_symlink_target
g_file_info_set_sort_order
g_file_attribute_matcher_new

View File

@ -1983,6 +1983,7 @@ g_date_time_to_utc
<SUBSECTION>
g_date_time_format
g_date_time_format_iso8601
</SECTION>
<SECTION>

View File

@ -291,22 +291,22 @@ gchar *
g_dbus_generate_guid (void)
{
GString *s;
GTimeVal now;
guint32 r1;
guint32 r2;
guint32 r3;
gint64 now_us;
s = g_string_new (NULL);
r1 = g_random_int ();
r2 = g_random_int ();
r3 = g_random_int ();
g_get_current_time (&now);
now_us = g_get_real_time ();
g_string_append_printf (s, "%08x", r1);
g_string_append_printf (s, "%08x", r2);
g_string_append_printf (s, "%08x", r3);
g_string_append_printf (s, "%08x", (guint32) now.tv_sec);
g_string_append_printf (s, "%08x", (guint32) (now_us / G_USEC_PER_SEC));
return g_string_free (s, FALSE);
}

View File

@ -1464,7 +1464,8 @@ g_file_info_get_deletion_date (GFileInfo *info)
static guint32 attr = 0;
GFileAttributeValue *value;
const char *date_str;
GTimeVal tv;
GTimeZone *local_tz = NULL;
GDateTime *dt = NULL;
g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
@ -1476,10 +1477,11 @@ g_file_info_get_deletion_date (GFileInfo *info)
if (!date_str)
return NULL;
if (g_time_val_from_iso8601 (date_str, &tv) == FALSE)
return NULL;
local_tz = g_time_zone_new_local ();
dt = g_date_time_new_from_iso8601 (date_str, local_tz);
g_time_zone_unref (local_tz);
return g_date_time_new_from_timeval_local (&tv);
return g_steal_pointer (&dt);
}
/**
@ -1753,7 +1755,11 @@ g_file_info_get_size (GFileInfo *info)
*
* Gets the modification time of the current @info and sets it
* in @result.
*
* Deprecated: 2.62: Use g_file_info_get_modification_date_time() instead, as
* #GTimeVal is deprecated due to the year 2038 problem.
**/
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
void
g_file_info_get_modification_time (GFileInfo *info,
GTimeVal *result)
@ -1775,6 +1781,47 @@ g_file_info_get_modification_time (GFileInfo *info,
value = g_file_info_find_value (info, attr_mtime_usec);
result->tv_usec = _g_file_attribute_value_get_uint32 (value);
}
G_GNUC_END_IGNORE_DEPRECATIONS
/**
* g_file_info_get_modification_date_time:
* @info: a #GFileInfo.
*
* Gets the modification time of the current @info and returns it as a
* #GDateTime.
*
* Returns: (transfer full) (nullable): modification time, or %NULL if unknown
* Since: 2.62
*/
GDateTime *
g_file_info_get_modification_date_time (GFileInfo *info)
{
static guint32 attr_mtime = 0, attr_mtime_usec;
GFileAttributeValue *value, *value_usec;
GDateTime *dt = NULL, *dt2 = NULL;
g_return_val_if_fail (G_IS_FILE_INFO (info), NULL);
if (attr_mtime == 0)
{
attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
}
value = g_file_info_find_value (info, attr_mtime);
if (value == NULL)
return NULL;
value_usec = g_file_info_find_value (info, attr_mtime_usec);
if (value_usec == NULL)
return NULL;
dt = g_date_time_new_from_unix_utc (_g_file_attribute_value_get_uint64 (value));
dt2 = g_date_time_add_seconds (dt, _g_file_attribute_value_get_uint32 (value_usec) / (gdouble) G_USEC_PER_SEC);
g_date_time_unref (dt);
return g_steal_pointer (&dt2);
}
/**
* g_file_info_get_symlink_target:
@ -2113,7 +2160,11 @@ g_file_info_set_size (GFileInfo *info,
*
* Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute in the file
* info to the given time value.
*
* Deprecated: 2.62: Use g_file_info_set_modification_date_time() instead, as
* #GTimeVal is deprecated due to the year 2038 problem.
**/
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
void
g_file_info_set_modification_time (GFileInfo *info,
GTimeVal *mtime)
@ -2137,6 +2188,41 @@ g_file_info_set_modification_time (GFileInfo *info,
if (value)
_g_file_attribute_value_set_uint32 (value, mtime->tv_usec);
}
G_GNUC_END_IGNORE_DEPRECATIONS
/**
* g_file_info_set_modification_date_time:
* @info: a #GFileInfo.
* @mtime: (not nullable): a #GDateTime.
*
* Sets the %G_FILE_ATTRIBUTE_TIME_MODIFIED attribute in the file
* info to the given date/time value.
*
* Since: 2.62
*/
void
g_file_info_set_modification_date_time (GFileInfo *info,
GDateTime *mtime)
{
static guint32 attr_mtime = 0, attr_mtime_usec;
GFileAttributeValue *value;
g_return_if_fail (G_IS_FILE_INFO (info));
g_return_if_fail (mtime != NULL);
if (attr_mtime == 0)
{
attr_mtime = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED);
attr_mtime_usec = lookup_attribute (G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
}
value = g_file_info_create_value (info, attr_mtime);
if (value)
_g_file_attribute_value_set_uint64 (value, g_date_time_to_unix (mtime));
value = g_file_info_create_value (info, attr_mtime_usec);
if (value)
_g_file_attribute_value_set_uint32 (value, g_date_time_get_microsecond (mtime));
}
/**
* g_file_info_set_symlink_target:

View File

@ -1046,9 +1046,13 @@ GLIB_AVAILABLE_IN_ALL
const char * g_file_info_get_content_type (GFileInfo *info);
GLIB_AVAILABLE_IN_ALL
goffset g_file_info_get_size (GFileInfo *info);
GLIB_AVAILABLE_IN_ALL
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
GLIB_DEPRECATED_IN_2_62_FOR(g_file_info_get_modification_date_time)
void g_file_info_get_modification_time (GFileInfo *info,
GTimeVal *result);
GTimeVal *result);
G_GNUC_END_IGNORE_DEPRECATIONS
GLIB_AVAILABLE_IN_2_62
GDateTime * g_file_info_get_modification_date_time (GFileInfo *info);
GLIB_AVAILABLE_IN_ALL
const char * g_file_info_get_symlink_target (GFileInfo *info);
GLIB_AVAILABLE_IN_ALL
@ -1093,9 +1097,14 @@ void g_file_info_set_content_type (GFileInfo *info,
GLIB_AVAILABLE_IN_ALL
void g_file_info_set_size (GFileInfo *info,
goffset size);
GLIB_AVAILABLE_IN_ALL
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
GLIB_DEPRECATED_IN_2_62_FOR(g_file_info_set_modification_date_time)
void g_file_info_set_modification_time (GFileInfo *info,
GTimeVal *mtime);
GTimeVal *mtime);
G_GNUC_END_IGNORE_DEPRECATIONS
GLIB_AVAILABLE_IN_2_62
void g_file_info_set_modification_date_time (GFileInfo *info,
GDateTime *mtime);
GLIB_AVAILABLE_IN_ALL
void g_file_info_set_symlink_target (GFileInfo *info,
const char *symlink_target);

View File

@ -361,13 +361,12 @@ test_method_calls_on_proxy (GDBusProxy *proxy)
SyncThreadData data1;
SyncThreadData data2;
SyncThreadData data3;
GTimeVal start_time;
GTimeVal end_time;
gint64 start_time, end_time;
guint elapsed_msec;
do_async = (n == 0);
g_get_current_time (&start_time);
start_time = g_get_real_time ();
data1.proxy = proxy;
data1.msec = 40;
@ -397,10 +396,9 @@ test_method_calls_on_proxy (GDBusProxy *proxy)
g_thread_join (thread2);
g_thread_join (thread3);
g_get_current_time (&end_time);
end_time = g_get_real_time ();
elapsed_msec = ((end_time.tv_sec * G_USEC_PER_SEC + end_time.tv_usec) -
(start_time.tv_sec * G_USEC_PER_SEC + start_time.tv_usec)) / 1000;
elapsed_msec = (end_time - start_time) / 1000;
//g_debug ("Elapsed time for %s = %d msec", n == 0 ? "async" : "sync", elapsed_msec);

View File

@ -66,14 +66,14 @@ socket_client_event (GSocketClient *client,
GSocketConnection *connection)
{
static GEnumClass *event_class;
GTimeVal tv;
gint64 now_us;
if (!event_class)
event_class = g_type_class_ref (G_TYPE_SOCKET_CLIENT_EVENT);
g_get_current_time (&tv);
printf ("% 12ld.%06ld GSocketClient => %s [%s]\n",
tv.tv_sec, tv.tv_usec,
now_us = g_get_real_time ();
g_print ("%" G_GINT64_FORMAT " GSocketClient => %s [%s]\n",
now_us,
g_enum_get_value (event_class, event)->value_nick,
connection ? G_OBJECT_TYPE_NAME (connection) : "");
}

View File

@ -439,31 +439,6 @@ g_winhttp_file_set_display_name (GFile *file,
return NULL;
}
static time_t
mktime_utc (SYSTEMTIME *t)
{
time_t retval;
static const gint days_before[] =
{
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
};
if (t->wMonth < 1 || t->wMonth > 12)
return (time_t) -1;
retval = (t->wYear - 1970) * 365;
retval += (t->wYear - 1968) / 4;
retval += days_before[t->wMonth-1] + t->wDay - 1;
if (t->wYear % 4 == 0 && t->wMonth < 3)
retval -= 1;
retval = ((((retval * 24) + t->wHour) * 60) + t->wMinute) * 60 + t->wSecond;
return retval;
}
static GFileInfo *
g_winhttp_file_query_info (GFile *file,
const char *attributes,
@ -603,12 +578,15 @@ g_winhttp_file_query_info (GFile *file,
last_modified.wYear >= 1970 &&
last_modified.wYear < 2038)
{
GTimeVal tv;
GDateTime *dt = NULL, *dt2 = NULL;
tv.tv_sec = mktime_utc (&last_modified);
tv.tv_usec = last_modified.wMilliseconds * 1000;
dt = g_date_time_new_from_unix_utc (last_modified.wMilliseconds / 1000);
dt2 = g_date_time_add_seconds (dt, (last_modified.wMilliseconds % 1000) / 1000);
g_file_info_set_modification_time (info, &tv);
g_file_info_set_modification_date_time (info, dt2);
g_date_time_unref (dt2);
g_date_time_unref (dt);
}
g_file_attribute_matcher_unref (matcher);

View File

@ -1540,7 +1540,7 @@ g_cond_free (GCond *cond)
* This function can be used even if g_thread_init() has not yet been
* called, and, in that case, will immediately return %TRUE.
*
* To easily calculate @abs_time a combination of g_get_current_time()
* To easily calculate @abs_time a combination of g_get_real_time()
* and g_time_val_add() can be used.
*
* Returns: %TRUE if @cond was signalled, or %FALSE on timeout

View File

@ -584,7 +584,7 @@ g_async_queue_timeout_pop_unlocked (GAsyncQueue *queue,
*
* If no data is received before @end_time, %NULL is returned.
*
* To easily calculate @end_time, a combination of g_get_current_time()
* To easily calculate @end_time, a combination of g_get_real_time()
* and g_time_val_add() can be used.
*
* Returns: data from the queue or %NULL, when no data is
@ -592,6 +592,7 @@ g_async_queue_timeout_pop_unlocked (GAsyncQueue *queue,
*
* Deprecated: use g_async_queue_timeout_pop().
*/
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
gpointer
g_async_queue_timed_pop (GAsyncQueue *queue,
GTimeVal *end_time)
@ -615,6 +616,7 @@ g_async_queue_timed_pop (GAsyncQueue *queue,
return retval;
}
G_GNUC_END_IGNORE_DEPRECATIONS
/**
* g_async_queue_timed_pop_unlocked:
@ -626,7 +628,7 @@ g_async_queue_timed_pop (GAsyncQueue *queue,
*
* If no data is received before @end_time, %NULL is returned.
*
* To easily calculate @end_time, a combination of g_get_current_time()
* To easily calculate @end_time, a combination of g_get_real_time()
* and g_time_val_add() can be used.
*
* This function must be called while holding the @queue's lock.
@ -636,6 +638,7 @@ g_async_queue_timed_pop (GAsyncQueue *queue,
*
* Deprecated: use g_async_queue_timeout_pop_unlocked().
*/
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
gpointer
g_async_queue_timed_pop_unlocked (GAsyncQueue *queue,
GTimeVal *end_time)
@ -654,6 +657,7 @@ g_async_queue_timed_pop_unlocked (GAsyncQueue *queue,
return g_async_queue_pop_intern_unlocked (queue, TRUE, m_end_time);
}
G_GNUC_END_IGNORE_DEPRECATIONS
/**
* g_async_queue_length:

View File

@ -110,12 +110,14 @@ GLIB_AVAILABLE_IN_2_46
void g_async_queue_push_front_unlocked (GAsyncQueue *queue,
gpointer item);
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
GLIB_DEPRECATED_FOR(g_async_queue_timeout_pop)
gpointer g_async_queue_timed_pop (GAsyncQueue *queue,
GTimeVal *end_time);
GLIB_DEPRECATED_FOR(g_async_queue_timeout_pop_unlocked)
gpointer g_async_queue_timed_pop_unlocked (GAsyncQueue *queue,
GTimeVal *end_time);
G_GNUC_END_IGNORE_DEPRECATIONS
G_END_DECLS

View File

@ -245,8 +245,10 @@ static void g_bookmark_file_add_item (GBookmarkFile *bookmark,
BookmarkItem *item,
GError **error);
static time_t timestamp_from_iso8601 (const gchar *iso_date);
static gchar * timestamp_to_iso8601 (time_t timestamp);
static gboolean timestamp_from_iso8601 (const gchar *iso_date,
time_t *out_timestamp,
GError **error);
static gchar *timestamp_to_iso8601 (time_t timestamp);
/********************************
* BookmarkAppInfo *
@ -772,14 +774,14 @@ parse_bookmark_element (GMarkupParseContext *context,
item = bookmark_item_new (uri);
if (added)
item->added = timestamp_from_iso8601 (added);
if (added != NULL && !timestamp_from_iso8601 (added, &item->added, error))
return;
if (modified)
item->modified = timestamp_from_iso8601 (modified);
if (modified != NULL && !timestamp_from_iso8601 (modified, &item->modified, error))
return;
if (visited)
item->visited = timestamp_from_iso8601 (visited);
if (visited != NULL && !timestamp_from_iso8601 (visited, &item->visited, error))
return;
add_error = NULL;
g_bookmark_file_add_item (parse_data->bookmark_file,
@ -872,8 +874,11 @@ parse_application_element (GMarkupParseContext *context,
else
ai->count = 1;
if (modified)
ai->stamp = timestamp_from_iso8601 (modified);
if (modified != NULL)
{
if (!timestamp_from_iso8601 (modified, &ai->stamp, error))
return;
}
else
{
/* the timestamp attribute has been deprecated but we still parse
@ -1591,28 +1596,32 @@ out:
static gchar *
timestamp_to_iso8601 (time_t timestamp)
{
GTimeVal stamp;
GDateTime *dt = g_date_time_new_from_unix_utc (timestamp);
gchar *iso8601_string = g_date_time_format_iso8601 (dt);
g_date_time_unref (dt);
if (timestamp == (time_t) -1)
g_get_current_time (&stamp);
else
{
stamp.tv_sec = timestamp;
stamp.tv_usec = 0;
}
return g_time_val_to_iso8601 (&stamp);
return g_steal_pointer (&iso8601_string);
}
static time_t
timestamp_from_iso8601 (const gchar *iso_date)
static gboolean
timestamp_from_iso8601 (const gchar *iso_date,
time_t *out_timestamp,
GError **error)
{
GTimeVal stamp;
gint64 time_val;
GDateTime *dt = g_date_time_new_from_iso8601 (iso_date, NULL);
if (dt == NULL)
{
g_set_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_READ,
_("Invalid date/time %s in bookmark file"), iso_date);
return FALSE;
}
if (!g_time_val_from_iso8601 (iso_date, &stamp))
return (time_t) -1;
time_val = g_date_time_to_unix (dt);
g_date_time_unref (dt);
return (time_t) stamp.tv_sec;
*out_timestamp = time_val;
return TRUE;
}
G_DEFINE_QUARK (g-bookmark-file-error-quark, g_bookmark_file_error)

View File

@ -118,11 +118,13 @@
* Similar to the struct timeval returned by the gettimeofday()
* UNIX system call.
*
* GLib is attempting to unify around the use of 64bit integers to
* GLib is attempting to unify around the use of 64-bit integers to
* represent microsecond-precision time. As such, this type will be
* removed from a future version of GLib. A consequence of using `glong` for
* `tv_sec` is that on 32-bit systems `GTimeVal` is subject to the year 2038
* problem.
*
* Deprecated: 2.62: Use #GDateTime or #guint64 instead.
*/
/**
@ -152,12 +154,12 @@
/**
* GTime:
*
* Simply a replacement for time_t. It has been deprecated
* since it is not equivalent to time_t on 64-bit platforms
* with a 64-bit time_t. Unrelated to #GTimer.
* Simply a replacement for `time_t`. It has been deprecated
* since it is not equivalent to `time_t` on 64-bit platforms
* with a 64-bit `time_t`. Unrelated to #GTimer.
*
* Note that #GTime is defined to always be a 32-bit integer,
* unlike time_t which may be 64-bit on some systems. Therefore,
* unlike `time_t` which may be 64-bit on some systems. Therefore,
* #GTime will overflow in the year 2038, and you cannot use the
* address of a #GTime variable as argument to the UNIX time()
* function.
@ -1447,12 +1449,14 @@ g_date_set_time_t (GDate *date,
*
* Deprecated: 2.10: Use g_date_set_time_t() instead.
*/
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
void
g_date_set_time (GDate *date,
GTime time_)
{
g_date_set_time_t (date, (time_t) time_);
}
G_GNUC_END_IGNORE_DEPRECATIONS
/**
* g_date_set_time_val:
@ -1466,13 +1470,17 @@ g_date_set_time (GDate *date,
* The time to date conversion is done using the user's current timezone.
*
* Since: 2.10
* Deprecated: 2.62: #GTimeVal is not year-2038-safe. Use g_date_set_time_t()
* instead.
*/
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
void
g_date_set_time_val (GDate *date,
GTimeVal *timeval)
{
g_date_set_time_t (date, (time_t) timeval->tv_sec);
}
G_GNUC_END_IGNORE_DEPRECATIONS
/**
* g_date_set_month:

View File

@ -45,7 +45,7 @@ G_BEGIN_DECLS
* Pennington <hp@pobox.com>
*/
typedef gint32 GTime;
typedef gint32 GTime GLIB_DEPRECATED_TYPE_IN_2_62_FOR(GDateTime);
typedef guint16 GDateYear;
typedef guint8 GDateDay; /* day of the month */
typedef struct _GDate GDate;
@ -195,12 +195,14 @@ void g_date_set_parse (GDate *date,
GLIB_AVAILABLE_IN_ALL
void g_date_set_time_t (GDate *date,
time_t timet);
GLIB_AVAILABLE_IN_ALL
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
GLIB_DEPRECATED_IN_2_62_FOR(g_date_set_time_t)
void g_date_set_time_val (GDate *date,
GTimeVal *timeval);
GLIB_DEPRECATED_FOR(g_date_set_time_t)
void g_date_set_time (GDate *date,
GTime time_);
G_GNUC_END_IGNORE_DEPRECATIONS
GLIB_AVAILABLE_IN_ALL
void g_date_set_month (GDate *date,
GDateMonth month);

View File

@ -134,10 +134,16 @@ struct _GDateTime
#define UNIX_EPOCH_START 719163
#define INSTANT_TO_UNIX(instant) \
((instant)/USEC_PER_SECOND - UNIX_EPOCH_START * SEC_PER_DAY)
#define INSTANT_TO_UNIX_USECS(instant) \
((instant) - UNIX_EPOCH_START * SEC_PER_DAY * USEC_PER_SECOND)
#define UNIX_TO_INSTANT(unix) \
(((gint64) (unix) + UNIX_EPOCH_START * SEC_PER_DAY) * USEC_PER_SECOND)
#define UNIX_USECS_TO_INSTANT(unix_usecs) \
((gint64) (unix_usecs) + UNIX_EPOCH_START * SEC_PER_DAY * USEC_PER_SECOND)
#define UNIX_TO_INSTANT_IS_VALID(unix) \
((gint64) (unix) <= INSTANT_TO_UNIX (G_MAXINT64))
#define UNIX_USECS_TO_INSTANT_IS_VALID(unix_usecs) \
((gint64) (unix_usecs) <= INSTANT_TO_UNIX_USECS (G_MAXINT64))
#define DAYS_IN_4YEARS 1461 /* days in 4 years */
#define DAYS_IN_100YEARS 36524 /* days in 100 years */
@ -854,6 +860,7 @@ g_date_time_replace_days (GDateTime *datetime,
/* now/unix/timeval Constructors {{{1 */
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
/*< internal >
* g_date_time_new_from_timeval:
* @tz: a #GTimeZone
@ -887,13 +894,14 @@ g_date_time_new_from_timeval (GTimeZone *tz,
return g_date_time_from_instant (tz, tv->tv_usec +
UNIX_TO_INSTANT (tv->tv_sec));
}
G_GNUC_END_IGNORE_DEPRECATIONS
/*< internal >
* g_date_time_new_from_unix:
* @tz: a #GTimeZone
* @t: the Unix time
* @usecs: the Unix time, in microseconds since the epoch
*
* Creates a #GDateTime corresponding to the given Unix time @t in the
* Creates a #GDateTime corresponding to the given Unix time @t_us in the
* given time zone @tz.
*
* Unix time is the number of seconds that have elapsed since 1970-01-01
@ -911,12 +919,12 @@ g_date_time_new_from_timeval (GTimeZone *tz,
**/
static GDateTime *
g_date_time_new_from_unix (GTimeZone *tz,
gint64 secs)
gint64 usecs)
{
if (!UNIX_TO_INSTANT_IS_VALID (secs))
if (!UNIX_USECS_TO_INSTANT_IS_VALID (usecs))
return NULL;
return g_date_time_from_instant (tz, UNIX_TO_INSTANT (secs));
return g_date_time_from_instant (tz, UNIX_USECS_TO_INSTANT (usecs));
}
/**
@ -941,11 +949,11 @@ g_date_time_new_from_unix (GTimeZone *tz,
GDateTime *
g_date_time_new_now (GTimeZone *tz)
{
GTimeVal tv;
gint64 now_us;
g_get_current_time (&tv);
now_us = g_get_real_time ();
return g_date_time_new_from_timeval (tz, &tv);
return g_date_time_new_from_unix (tz, now_us);
}
/**
@ -1025,8 +1033,11 @@ g_date_time_new_from_unix_local (gint64 t)
GDateTime *datetime;
GTimeZone *local;
if (t > G_MAXINT64 / USEC_PER_SECOND)
return NULL;
local = g_time_zone_new_local ();
datetime = g_date_time_new_from_unix (local, t);
datetime = g_date_time_new_from_unix (local, t * USEC_PER_SECOND);
g_time_zone_unref (local);
return datetime;
@ -1057,8 +1068,11 @@ g_date_time_new_from_unix_utc (gint64 t)
GDateTime *datetime;
GTimeZone *utc;
if (t > G_MAXINT64 / USEC_PER_SECOND)
return NULL;
utc = g_time_zone_new_utc ();
datetime = g_date_time_new_from_unix (utc, t);
datetime = g_date_time_new_from_unix (utc, t * USEC_PER_SECOND);
g_time_zone_unref (utc);
return datetime;
@ -1084,7 +1098,10 @@ g_date_time_new_from_unix_utc (gint64 t)
* Returns: a new #GDateTime, or %NULL
*
* Since: 2.26
* Deprecated: 2.62: #GTimeVal is not year-2038-safe. Use
* g_date_time_new_from_unix_local() instead.
**/
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
GDateTime *
g_date_time_new_from_timeval_local (const GTimeVal *tv)
{
@ -1097,6 +1114,7 @@ g_date_time_new_from_timeval_local (const GTimeVal *tv)
return datetime;
}
G_GNUC_END_IGNORE_DEPRECATIONS
/**
* g_date_time_new_from_timeval_utc:
@ -1116,7 +1134,10 @@ g_date_time_new_from_timeval_local (const GTimeVal *tv)
* Returns: a new #GDateTime, or %NULL
*
* Since: 2.26
* Deprecated: 2.62: #GTimeVal is not year-2038-safe. Use
* g_date_time_new_from_unix_utc() instead.
**/
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
GDateTime *
g_date_time_new_from_timeval_utc (const GTimeVal *tv)
{
@ -1129,6 +1150,7 @@ g_date_time_new_from_timeval_utc (const GTimeVal *tv)
return datetime;
}
G_GNUC_END_IGNORE_DEPRECATIONS
/* Parse integers in the form d (week days), dd (hours etc), ddd (ordinal days) or dddd (years) */
static gboolean
@ -2533,7 +2555,10 @@ g_date_time_to_unix (GDateTime *datetime)
* Returns: %TRUE if successful, else %FALSE
*
* Since: 2.26
* Deprecated: 2.62: #GTimeVal is not year-2038-safe. Use
* g_date_time_to_unix() instead.
**/
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
gboolean
g_date_time_to_timeval (GDateTime *datetime,
GTimeVal *tv)
@ -2543,6 +2568,7 @@ g_date_time_to_timeval (GDateTime *datetime,
return TRUE;
}
G_GNUC_END_IGNORE_DEPRECATIONS
/* Timezone queries {{{1 */
/**
@ -3393,6 +3419,49 @@ g_date_time_format (GDateTime *datetime,
return g_string_free (outstr, FALSE);
}
/**
* g_date_time_format_iso8601:
* @datetime: A #GDateTime
*
* Format @datetime in [ISO 8601 format](https://en.wikipedia.org/wiki/ISO_8601),
* including the date, time and time zone, and return that as a UTF-8 encoded
* string.
*
* Returns: a newly allocated string formatted in ISO 8601 format
* or %NULL in the case that there was an error. The string
* should be freed with g_free().
* Since: 2.62
*/
gchar *
g_date_time_format_iso8601 (GDateTime *datetime)
{
GString *outstr = NULL;
gchar *main_date = NULL;
gint64 offset;
/* Main date and time. */
main_date = g_date_time_format (datetime, "%Y-%m-%dT%H:%M:%S");
outstr = g_string_new (main_date);
g_free (main_date);
/* Timezone. Format it as `%:::z` unless the offset is zero, in which case
* we can simply use `Z`. */
offset = g_date_time_get_utc_offset (datetime);
if (offset == 0)
{
g_string_append_c (outstr, 'Z');
}
else
{
gchar *time_zone = g_date_time_format (datetime, "%:::z");
g_string_append (outstr, time_zone);
g_free (time_zone);
}
return g_string_free (outstr, FALSE);
}
/* Epilogue {{{1 */
/* vim:set foldmethod=marker: */

View File

@ -113,10 +113,12 @@ GDateTime * g_date_time_new_from_unix_local (gint64
GLIB_AVAILABLE_IN_ALL
GDateTime * g_date_time_new_from_unix_utc (gint64 t);
GLIB_AVAILABLE_IN_ALL
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
GLIB_DEPRECATED_IN_2_62_FOR(g_date_time_new_from_unix_local)
GDateTime * g_date_time_new_from_timeval_local (const GTimeVal *tv);
GLIB_AVAILABLE_IN_ALL
GLIB_DEPRECATED_IN_2_62_FOR(g_date_time_new_from_unix_utc)
GDateTime * g_date_time_new_from_timeval_utc (const GTimeVal *tv);
G_GNUC_END_IGNORE_DEPRECATIONS
GLIB_AVAILABLE_IN_2_56
GDateTime * g_date_time_new_from_iso8601 (const gchar *text,
@ -238,9 +240,11 @@ gdouble g_date_time_get_seconds (GDateTi
GLIB_AVAILABLE_IN_ALL
gint64 g_date_time_to_unix (GDateTime *datetime);
GLIB_AVAILABLE_IN_ALL
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
GLIB_DEPRECATED_IN_2_62_FOR(g_date_time_to_unix)
gboolean g_date_time_to_timeval (GDateTime *datetime,
GTimeVal *tv);
G_GNUC_END_IGNORE_DEPRECATIONS
GLIB_AVAILABLE_IN_ALL
GTimeSpan g_date_time_get_utc_offset (GDateTime *datetime);
@ -262,6 +266,8 @@ GDateTime * g_date_time_to_utc (GDateTi
GLIB_AVAILABLE_IN_ALL
gchar * g_date_time_format (GDateTime *datetime,
const gchar *format) G_GNUC_MALLOC;
GLIB_AVAILABLE_IN_2_62
gchar * g_date_time_format_iso8601 (GDateTime *datetime) G_GNUC_MALLOC;
G_END_DECLS

View File

@ -1318,7 +1318,7 @@ get_tmp_file (gchar *tmpl,
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
static const int NLETTERS = sizeof (letters) - 1;
glong value;
GTimeVal tv;
gint64 now_us;
static int counter = 0;
g_return_val_if_fail (tmpl != NULL, -1);
@ -1333,8 +1333,8 @@ get_tmp_file (gchar *tmpl,
}
/* Get some more or less random data. */
g_get_current_time (&tv);
value = (tv.tv_usec ^ tv.tv_sec) + counter++;
now_us = g_get_real_time ();
value = ((now_us % G_USEC_PER_SEC) ^ (now_us / G_USEC_PER_SEC)) + counter++;
for (count = 0; count < 100; value += 7777, ++count)
{

View File

@ -2631,39 +2631,24 @@ g_source_query_unix_fd (GSource *source,
* Equivalent to the UNIX gettimeofday() function, but portable.
*
* You may find g_get_real_time() to be more convenient.
*
* Deprecated: 2.62: #GTimeVal is not year-2038-safe. Use g_get_real_time()
* instead.
**/
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
void
g_get_current_time (GTimeVal *result)
{
#ifndef G_OS_WIN32
struct timeval r;
gint64 tv;
g_return_if_fail (result != NULL);
/*this is required on alpha, there the timeval structs are int's
not longs and a cast only would fail horribly*/
gettimeofday (&r, NULL);
result->tv_sec = r.tv_sec;
result->tv_usec = r.tv_usec;
#else
FILETIME ft;
guint64 time64;
tv = g_get_real_time ();
g_return_if_fail (result != NULL);
GetSystemTimeAsFileTime (&ft);
memmove (&time64, &ft, sizeof (FILETIME));
/* Convert from 100s of nanoseconds since 1601-01-01
* to Unix epoch. Yes, this is Y2038 unsafe.
*/
time64 -= G_GINT64_CONSTANT (116444736000000000);
time64 /= 10;
result->tv_sec = time64 / 1000000;
result->tv_usec = time64 % 1000000;
#endif
result->tv_sec = tv / 1000000;
result->tv_usec = tv % 1000000;
}
G_GNUC_END_IGNORE_DEPRECATIONS
/**
* g_get_real_time:
@ -2685,11 +2670,29 @@ g_get_current_time (GTimeVal *result)
gint64
g_get_real_time (void)
{
GTimeVal tv;
#ifndef G_OS_WIN32
struct timeval r;
g_get_current_time (&tv);
/* this is required on alpha, there the timeval structs are ints
* not longs and a cast only would fail horribly */
gettimeofday (&r, NULL);
return (((gint64) tv.tv_sec) * 1000000) + tv.tv_usec;
return (((gint64) r.tv_sec) * 1000000) + r.tv_usec;
#else
FILETIME ft;
guint64 time64;
GetSystemTimeAsFileTime (&ft);
memmove (&time64, &ft, sizeof (FILETIME));
/* Convert from 100s of nanoseconds since 1601-01-01
* to Unix epoch. This is Y2038 safe.
*/
time64 -= G_GINT64_CONSTANT (116444736000000000);
time64 /= 10;
return time64;
#endif
}
/**
@ -4412,12 +4415,14 @@ g_main_context_remove_poll_unlocked (GMainContext *context,
*
* Deprecated: 2.28: use g_source_get_time() instead
**/
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
void
g_source_get_current_time (GSource *source,
GTimeVal *timeval)
{
g_get_current_time (timeval);
}
G_GNUC_END_IGNORE_DEPRECATIONS
/**
* g_source_get_time:

View File

@ -544,9 +544,11 @@ GLIB_AVAILABLE_IN_ALL
void g_source_remove_child_source (GSource *source,
GSource *child_source);
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
GLIB_DEPRECATED_IN_2_28_FOR(g_source_get_time)
void g_source_get_current_time (GSource *source,
GTimeVal *timeval);
G_GNUC_END_IGNORE_DEPRECATIONS
GLIB_AVAILABLE_IN_ALL
gint64 g_source_get_time (GSource *source);
@ -568,8 +570,11 @@ GSource *g_timeout_source_new_seconds (guint interval);
/* Miscellaneous functions
*/
GLIB_AVAILABLE_IN_ALL
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
GLIB_DEPRECATED_IN_2_62_FOR(g_get_real_time)
void g_get_current_time (GTimeVal *result);
G_GNUC_END_IGNORE_DEPRECATIONS
GLIB_AVAILABLE_IN_ALL
gint64 g_get_monotonic_time (void);
GLIB_AVAILABLE_IN_ALL

View File

@ -49,6 +49,7 @@
#include "gmem.h"
#include "gtestutils.h"
#include "gthread.h"
#include "gtimer.h"
#ifdef G_OS_UNIX
#include <unistd.h>
@ -220,7 +221,6 @@ g_rand_new (void)
guint32 seed[4];
#ifdef G_OS_UNIX
static gboolean dev_urandom_exists = TRUE;
GTimeVal now;
if (dev_urandom_exists)
{
@ -254,10 +254,10 @@ g_rand_new (void)
}
if (!dev_urandom_exists)
{
g_get_current_time (&now);
seed[0] = now.tv_sec;
seed[1] = now.tv_usec;
{
gint64 now_us = g_get_real_time ();
seed[0] = now_us / G_USEC_PER_SEC;
seed[1] = now_us % G_USEC_PER_SEC;
seed[2] = getpid ();
seed[3] = getppid ();
}

View File

@ -595,9 +595,8 @@ magazine_cache_update_stamp (void)
{
if (allocator->stamp_counter >= MAX_STAMP_COUNTER)
{
GTimeVal tv;
g_get_current_time (&tv);
allocator->last_stamp = tv.tv_sec * 1000 + tv.tv_usec / 1000; /* milli seconds */
gint64 now_us = g_get_real_time ();
allocator->last_stamp = now_us / 1000; /* milli seconds */
allocator->stamp_counter = 0;
}
else

View File

@ -286,7 +286,11 @@ g_usleep (gulong microseconds)
*
* Adds the given number of microseconds to @time_. @microseconds can
* also be negative to decrease the value of @time_.
*
* Deprecated: 2.62: #GTimeVal is not year-2038-safe. Use `guint64` for
* representing microseconds since the epoch, or use #GDateTime.
**/
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
void
g_time_val_add (GTimeVal *time_, glong microseconds)
{
@ -314,6 +318,7 @@ g_time_val_add (GTimeVal *time_, glong microseconds)
}
}
}
G_GNUC_END_IGNORE_DEPRECATIONS
/* converts a broken down date representation, relative to UTC,
* to a timestamp; it uses timegm() if it's available.
@ -364,10 +369,21 @@ mktime_utc (struct tm *tm)
*
* Any leading or trailing space in @iso_date is ignored.
*
* This function was deprecated, along with #GTimeVal itself, in GLib 2.62.
* Equivalent functionality is available using code like:
* |[
* GDateTime *dt = g_date_time_new_from_iso8601 (iso8601_string, NULL);
* gint64 time_val = g_date_time_to_unix (dt);
* g_date_time_unref (dt);
* ]|
*
* Returns: %TRUE if the conversion was successful.
*
* Since: 2.12
* Deprecated: 2.62: #GTimeVal is not year-2038-safe. Use
* g_date_time_new_from_iso8601() instead.
*/
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
gboolean
g_time_val_from_iso8601 (const gchar *iso_date,
GTimeVal *time_)
@ -528,6 +544,7 @@ g_time_val_from_iso8601 (const gchar *iso_date,
return *iso_date == '\0';
}
G_GNUC_END_IGNORE_DEPRECATIONS
/**
* g_time_val_to_iso8601:
@ -557,7 +574,13 @@ g_time_val_from_iso8601 (const gchar *iso_date,
* If @time_ represents a date which is too large to fit into a `struct tm`,
* %NULL will be returned. This is platform dependent. Note also that since
* `GTimeVal` stores the number of seconds as a `glong`, on 32-bit systems it
* is subject to the year 2038 problem.
* is subject to the year 2038 problem. Accordingly, since GLib 2.62, this
* function has been deprecated. Equivalent functionality is available using:
* |[
* GDateTime *dt = g_date_time_new_from_unix_utc (time_val);
* iso8601_string = g_date_time_format_iso8601 (dt);
* g_date_time_unref (dt);
* ]|
*
* The return value of g_time_val_to_iso8601() has been nullable since GLib
* 2.54; before then, GLib would crash under the same conditions.
@ -566,7 +589,10 @@ g_time_val_from_iso8601 (const gchar *iso_date,
* or %NULL if @time_ was too large
*
* Since: 2.12
* Deprecated: 2.62: #GTimeVal is not year-2038-safe. Use
* g_date_time_format_iso8601(dt) instead.
*/
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
gchar *
g_time_val_to_iso8601 (GTimeVal *time_)
{
@ -624,3 +650,4 @@ g_time_val_to_iso8601 (GTimeVal *time_)
return retval;
}
G_GNUC_END_IGNORE_DEPRECATIONS

View File

@ -62,14 +62,16 @@ gboolean g_timer_is_active (GTimer *timer);
GLIB_AVAILABLE_IN_ALL
void g_usleep (gulong microseconds);
GLIB_AVAILABLE_IN_ALL
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
GLIB_DEPRECATED_IN_2_62
void g_time_val_add (GTimeVal *time_,
glong microseconds);
GLIB_AVAILABLE_IN_ALL
GLIB_DEPRECATED_IN_2_62_FOR(g_date_time_new_from_iso8601)
gboolean g_time_val_from_iso8601 (const gchar *iso_date,
GTimeVal *time_);
GLIB_AVAILABLE_IN_ALL
GLIB_DEPRECATED_IN_2_62_FOR(g_date_time_format)
gchar* g_time_val_to_iso8601 (GTimeVal *time_) G_GNUC_MALLOC;
G_GNUC_END_IGNORE_DEPRECATIONS
G_END_DECLS

View File

@ -546,13 +546,13 @@ union _GDoubleIEEE754
#error unknown ENDIAN type
#endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
typedef struct _GTimeVal GTimeVal;
typedef struct _GTimeVal GTimeVal GLIB_DEPRECATED_TYPE_IN_2_62_FOR(GDateTime);
struct _GTimeVal
{
glong tv_sec;
glong tv_usec;
};
} GLIB_DEPRECATED_TYPE_IN_2_62_FOR(GDateTime);
typedef gint grefcount;
typedef volatile gint gatomicrefcount;

View File

@ -311,6 +311,7 @@ test_GDateTime_get_hour (void)
g_date_time_unref (dt);
}
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
static void
test_GDateTime_get_microsecond (void)
{
@ -322,6 +323,7 @@ test_GDateTime_get_microsecond (void)
g_assert_cmpint (tv.tv_usec, ==, g_date_time_get_microsecond (dt));
g_date_time_unref (dt);
}
G_GNUC_END_IGNORE_DEPRECATIONS
static void
test_GDateTime_get_year (void)
@ -358,6 +360,7 @@ test_GDateTime_hash (void)
g_hash_table_destroy (h);
}
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
static void
test_GDateTime_new_from_timeval (void)
{
@ -480,6 +483,7 @@ test_GDateTime_new_from_timeval_utc (void)
g_assert_cmpint (tv.tv_usec, ==, tv2.tv_usec);
g_date_time_unref (dt);
}
G_GNUC_END_IGNORE_DEPRECATIONS
static void
test_GDateTime_new_from_iso8601 (void)
@ -1238,6 +1242,7 @@ test_GDateTime_get_utc_offset (void)
#endif
}
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
static void
test_GDateTime_to_timeval (void)
{
@ -1254,6 +1259,7 @@ test_GDateTime_to_timeval (void)
g_assert_cmpint (tv1.tv_usec, ==, tv2.tv_usec);
g_date_time_unref (dt);
}
G_GNUC_END_IGNORE_DEPRECATIONS
static void
test_GDateTime_to_local (void)
@ -2066,6 +2072,30 @@ test_z (void)
g_time_zone_unref (tz);
}
static void
test_format_iso8601 (void)
{
GTimeZone *tz = NULL;
GDateTime *dt = NULL;
gchar *p = NULL;
tz = g_time_zone_new_utc ();
dt = g_date_time_new (tz, 2019, 6, 26, 15, 1, 5);
p = g_date_time_format_iso8601 (dt);
g_assert_cmpstr (p, ==, "2019-06-26T15:01:05Z");
g_free (p);
g_date_time_unref (dt);
g_time_zone_unref (tz);
tz = g_time_zone_new_offset (-60 * 60);
dt = g_date_time_new (tz, 2019, 6, 26, 15, 1, 5);
p = g_date_time_format_iso8601 (dt);
g_assert_cmpstr (p, ==, "2019-06-26T15:01:05-01");
g_free (p);
g_date_time_unref (dt);
g_time_zone_unref (tz);
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-y2k"
static void
@ -2556,6 +2586,7 @@ main (gint argc,
g_test_add_func ("/GDateTime/printf", test_GDateTime_printf);
g_test_add_func ("/GDateTime/non_utf8_printf", test_non_utf8_printf);
g_test_add_func ("/GDateTime/format_unrepresentable", test_format_unrepresentable);
g_test_add_func ("/GDateTime/format_iso8601", test_format_iso8601);
g_test_add_func ("/GDateTime/strftime", test_strftime);
g_test_add_func ("/GDateTime/strftime/error_handling", test_GDateTime_strftime_error_handling);
g_test_add_func ("/GDateTime/modifiers", test_modifiers);

View File

@ -695,7 +695,9 @@ typedef struct {
GSource *timeout1, *timeout2;
gint64 time1;
GTimeVal tv;
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
GTimeVal tv; /* needed for g_source_get_current_time() */
G_GNUC_END_IGNORE_DEPRECATIONS
} TimeTestData;
static gboolean
@ -730,7 +732,9 @@ G_GNUC_END_IGNORE_DEPRECATIONS
}
else
{
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
GTimeVal tv;
G_GNUC_END_IGNORE_DEPRECATIONS
/* Second iteration */
g_assert_true (g_source_is_destroyed (data->timeout2));

View File

@ -21,6 +21,9 @@
* Author: Matthias Clasen
*/
/* We test a few deprecated APIs here. */
#define GLIB_DISABLE_DEPRECATION_WARNINGS 1
#include "glib.h"
static void