[libsoup/gtimeval-out: 2/4] SoupDate: Move from GTimeVal to glibc APIs in soup_date_to_time_t()



commit 9cd0065593564d4b09a367a270ef051516cc7d7f
Author: Claudio Saavedra <csaavedra igalia com>
Date:   Tue Aug 13 14:42:52 2019 +0300

    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.

 libsoup/soup-date.c | 44 +++++++++++++++++++-------------------------
 tests/date-test.c   |  2 ++
 2 files changed, 21 insertions(+), 25 deletions(-)
---
diff --git a/libsoup/soup-date.c b/libsoup/soup-date.c
index dabae9d4..8f1143d9 100644
--- a/libsoup/soup-date.c
+++ b/libsoup/soup-date.c
@@ -682,9 +682,10 @@ soup_date_to_string (SoupDate *date, SoupDateFormat format)
  *
  * Converts @date to a <type>time_t</type>.
  *
- * If @date is not representable as a <type>time_t</type>, it will be
- * clamped into range. (In particular, some HTTP cookies have
- * expiration dates after "Y2.038k" (2038-01-19T03:14:07Z).)
+ * During the conversion, this method assumes @date to be in UTC.
+ * Please note that <type>time_t</type> is not Y2038-safe and this
+ * method doesn't aim to be either. Expect it to be deprecated soon
+ * and do not use it in new code.
  *
  * Return value: @date as a <type>time_t</type>
  **/
@@ -692,29 +693,22 @@ time_t
 soup_date_to_time_t (SoupDate *date)
 {
        time_t tt;
-       GTimeVal val;
+       struct tm time;
+
+       time.tm_sec = date->second;
+       time.tm_min = date->minute;
+       time.tm_hour = date->hour;
+       time.tm_mday = date->day;
+       /* tm_mon is 0 to 11 whereas SoupDate.month is 1 to 12. */
+       time.tm_mon = date->month - 1;
+       /* tm_year is years since 1900. */
+       time.tm_year = date->year - 1900;
+       time.tm_isdst = 0;
+
+       /* mktime sets the timezone to the local one, use timegm()
+          instead. */
+       tt = timegm (&time);
 
-       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.)
-        */
-       if (sizeof (time_t) == 4 && date->year > 2038)
-               return (time_t)0x7fffffff;
-
-       soup_date_to_timeval (date, &val);
-       tt = val.tv_sec;
-
-       if (sizeof (time_t) == 4 && tt < 0)
-               return (time_t)0x7fffffff;
        return tt;
 }
 
diff --git a/tests/date-test.c b/tests/date-test.c
index f623061b..97ae221f 100644
--- a/tests/date-test.c
+++ b/tests/date-test.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);
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]