From d166a55c6498919641edcfa238b6d4b19e34dcc7 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Fri, 28 Jun 2019 19:02:09 +0100 Subject: [PATCH] gfileinfo: Add g_file_info_{get,set}_modification_date_time() APIs These are alternatives to g_file_info_{get,set}_modification_time(), which will soon be deprecated due to using the deprecated GTimeVal type, which is not year 2038 safe. The new APIs take a GDateTime instead. Signed-off-by: Philip Withnall Helps: #1438 --- docs/reference/gio/gio-sections-common.txt | 2 + gio/gfileinfo.c | 74 ++++++++++++++++++++++ gio/gfileinfo.h | 5 ++ 3 files changed, 81 insertions(+) diff --git a/docs/reference/gio/gio-sections-common.txt b/docs/reference/gio/gio-sections-common.txt index 95433ce43..bb483b0ce 100644 --- a/docs/reference/gio/gio-sections-common.txt +++ b/docs/reference/gio/gio-sections-common.txt @@ -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 diff --git a/gio/gfileinfo.c b/gio/gfileinfo.c index 68bacde52..db5ba5dd7 100644 --- a/gio/gfileinfo.c +++ b/gio/gfileinfo.c @@ -1776,6 +1776,46 @@ g_file_info_get_modification_time (GFileInfo *info, result->tv_usec = _g_file_attribute_value_get_uint32 (value); } +/** + * 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: * @info: a #GFileInfo. @@ -2138,6 +2178,40 @@ g_file_info_set_modification_time (GFileInfo *info, _g_file_attribute_value_set_uint32 (value, mtime->tv_usec); } +/** + * 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: * @info: a #GFileInfo. diff --git a/gio/gfileinfo.h b/gio/gfileinfo.h index 622c2b611..01dcc4f14 100644 --- a/gio/gfileinfo.h +++ b/gio/gfileinfo.h @@ -1049,6 +1049,8 @@ goffset g_file_info_get_size (GFileInfo *info); GLIB_AVAILABLE_IN_ALL void g_file_info_get_modification_time (GFileInfo *info, GTimeVal *result); +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 @@ -1096,6 +1098,9 @@ void g_file_info_set_size (GFileInfo *info, GLIB_AVAILABLE_IN_ALL void g_file_info_set_modification_time (GFileInfo *info, GTimeVal *mtime); +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);