summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeonah Moon <seonah1.moon@samsung.com>2020-02-04 21:00:32 +0900
committerSeonah Moon <seonah1.moon@samsung.com>2020-02-04 21:06:22 +0900
commitaa4ed701e31823b4a1524552ffcdd6ceca01fa78 (patch)
tree9df3875d16b276315162521205d48b68f2dc69a3
parent381bb3351e257711d22fbc81f27e551ea0f88c71 (diff)
downloadlibsoup-aa4ed701e31823b4a1524552ffcdd6ceca01fa78.tar.gz
libsoup-aa4ed701e31823b4a1524552ffcdd6ceca01fa78.tar.bz2
libsoup-aa4ed701e31823b4a1524552ffcdd6ceca01fa78.zip
SoupDate: Move from GTimeVal to glibc APIs in soup_date_to_time_t()
GTimeVal is deprecated, so let's stop using it. Also add a test for soup_date_to_time_t() which was missing. The fixes in this change use time_t-specific APIs from libc only. As such, they are not necessarily Y2038-safe depending on the architecture. time_t is not safe anyway so there's no point in trying to be. This method will be deprecated in the next release cycle anyway. https://gitlab.gnome.org/GNOME/libsoup/commit/eab0987dc7b46eb4e4c0cf8fe5afcbf6aac18830 Change-Id: I8cb9f2e6f203fe504da4f2079f79cc6b51464046
-rw-r--r--libsoup/soup-date.c30
-rw-r--r--tests/date.c2
2 files changed, 19 insertions, 13 deletions
diff --git a/libsoup/soup-date.c b/libsoup/soup-date.c
index dabae9d4..98aa95ff 100644
--- a/libsoup/soup-date.c
+++ b/libsoup/soup-date.c
@@ -680,7 +680,8 @@ soup_date_to_string (SoupDate *date, SoupDateFormat format)
* soup_date_to_time_t:
* @date: a #SoupDate
*
- * Converts @date to a <type>time_t</type>.
+ * Converts @date to a <type>time_t</type>, assumming it to be in
+ * UTC.
*
* If @date is not representable as a <type>time_t</type>, it will be
* clamped into range. (In particular, some HTTP cookies have
@@ -691,31 +692,34 @@ soup_date_to_string (SoupDate *date, SoupDateFormat format)
time_t
soup_date_to_time_t (SoupDate *date)
{
- time_t tt;
- GTimeVal val;
+ GDateTime *datetime;
+ gint64 seconds;
g_return_val_if_fail (date != NULL, 0);
- /* FIXME: offset, etc */
-
if (date->year < 1970)
return 0;
/* If the year is later than 2038, we're guaranteed to
* overflow a 32-bit time_t. (If it's exactly 2038, we'll
* *probably* overflow, but only by a little, and it's easiest
- * to test that at the end by seeing if the result has turned
- * negative.)
+ * to just clamp down the value if it's above G_MAXINT32.
*/
if (sizeof (time_t) == 4 && date->year > 2038)
- return (time_t)0x7fffffff;
+ return (time_t)G_MAXINT32;
+
+ datetime = g_date_time_new_utc (date->year,
+ date->month,
+ date->day,
+ date->hour,
+ date->minute,
+ date->second);
+
+ seconds = g_date_time_to_unix (datetime);
- soup_date_to_timeval (date, &val);
- tt = val.tv_sec;
+ g_date_time_unref (datetime);
- if (sizeof (time_t) == 4 && tt < 0)
- return (time_t)0x7fffffff;
- return tt;
+ return (time_t) (sizeof (time_t) == 4 ? MIN(seconds, G_MAXINT32) : seconds);
}
/**
diff --git a/tests/date.c b/tests/date.c
index f623061b..97ae221f 100644
--- a/tests/date.c
+++ b/tests/date.c
@@ -171,6 +171,8 @@ check_ok_time_t (void)
g_assert_cmpint (date->minute, ==, 9);
g_assert_cmpint (date->second, ==, 7);
+ g_assert_cmpuint (TIME_T, ==, soup_date_to_time_t (date));
+
soup_date_free (date);
}