[gcr/nielsdg/certificate-gdatetime: 2/6] Use GDateTime instead of GDate




commit 8927be67708f5b8fcb66e3d65b2de4b8fe77b236
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Wed Jul 6 09:57:11 2022 +0200

    Use GDateTime instead of GDate
    
    ... and remove some functions that still use `time_t`

 egg/egg-asn1x.c                   | 180 ++++++++------------------------------
 egg/egg-asn1x.h                   |  17 +---
 egg/test-asn1.c                   | 128 ++++++---------------------
 gcr-gtk3/gcr-certificate-widget.c |  31 ++++---
 gcr-gtk4/gcr-certificate-widget.c |  31 ++++---
 gcr/gcr-certificate.c             |  34 ++-----
 gcr/gcr-certificate.h             |   4 +-
 gcr/test-certificate.c            |  22 ++---
 8 files changed, 122 insertions(+), 325 deletions(-)
---
diff --git a/egg/egg-asn1x.c b/egg/egg-asn1x.c
index b7d9d112..b6522c9c 100644
--- a/egg/egg-asn1x.c
+++ b/egg/egg-asn1x.c
@@ -1968,7 +1968,7 @@ two_to_four_digit_year (int year)
 
 static gboolean
 parse_utc_time (const gchar *time, gsize n_time,
-                struct tm* when, gint *offset)
+                struct tm* when, gint32 *offset)
 {
        const char *p, *e;
        int year;
@@ -2077,7 +2077,7 @@ parse_utc_time (const gchar *time, gsize n_time,
 
 static gboolean
 parse_general_time (const gchar *time, gsize n_time,
-                    struct tm* when, gint *offset)
+                    struct tm* when, gint32 *offset)
 {
        const char *p, *e;
 
@@ -2181,48 +2181,36 @@ static gboolean
 anode_read_time (GNode *node,
                  GBytes *data,
                  struct tm *when,
-                 glong *value)
+                 gint32 *offset)
 {
-       const gchar *buf;
+       const char *buf;
        gboolean ret;
-       gint offset = 0;
-       gint flags;
-       gint type;
+       int flags;
+       int type;
        gsize len;
 
        g_assert (data != NULL);
        g_assert (when != NULL);
-       g_assert (value != NULL);
+       g_assert (offset != NULL);
 
        flags = anode_def_flags (node);
        type = anode_def_type (node);
        buf = g_bytes_get_data (data, &len);
 
        if (type == EGG_ASN1X_GENERALIZED_TIME)
-               ret = parse_general_time (buf, len, when, &offset);
+               ret = parse_general_time (buf, len, when, offset);
        else if (type == EGG_ASN1X_UTC_TIME)
-               ret = parse_utc_time (buf, len, when, &offset);
+               ret = parse_utc_time (buf, len, when, offset);
        else if (flags & FLAG_GENERALIZED)
-               ret = parse_general_time (buf, len, when, &offset);
+               ret = parse_general_time (buf, len, when, offset);
        else if (flags & FLAG_UTC)
-               ret = parse_utc_time (buf, len, when, &offset);
+               ret = parse_utc_time (buf, len, when, offset);
        else
                g_return_val_if_reached (FALSE);
 
        if (!ret)
                return anode_failure (node, "invalid time content");
 
-       /* In order to work with 32 bit time_t. */
-       if (sizeof (time_t) <= 4 && when->tm_year >= 138) {
-               *value = (time_t)2145914603;  /* 2037-12-31 23:23:23 */
-
-       /* Convert to seconds since epoch */
-       } else {
-               *value = timegm (when);
-               g_return_val_if_fail (*value >= 0, FALSE);
-               *value += offset;
-       }
-
        return TRUE;
 }
 
@@ -3619,77 +3607,51 @@ egg_asn1x_set_bits_as_ulong (GNode *node,
        anode_take_value (node, g_bytes_new_take (data, len));
 }
 
-glong
-egg_asn1x_get_time_as_long (GNode *node)
+GDateTime *
+egg_asn1x_get_time_as_date_time (GNode *node)
 {
        struct tm when;
        GBytes *data;
-       glong time;
-       gint type;
+       int type;
+       gint32 offset;
+       GTimeZone *timezone;
+       GDateTime *ret;
 
-       g_return_val_if_fail (node, -1);
-       type = anode_def_type (node);
-
-       /* Time is often represented as a choice, so work than in here */
-       if (type == EGG_ASN1X_CHOICE) {
-               node = egg_asn1x_get_choice (node);
-               if (node == NULL)
-                       return -1;
-               g_return_val_if_fail (anode_def_type (node) == EGG_ASN1X_TIME ||
-                                     anode_def_type (node) == EGG_ASN1X_UTC_TIME ||
-                                     anode_def_type (node) == EGG_ASN1X_GENERALIZED_TIME, -1);
-               return egg_asn1x_get_time_as_long (node);
-       }
-
-       g_return_val_if_fail (type == EGG_ASN1X_TIME ||
-                             type == EGG_ASN1X_UTC_TIME ||
-                             type == EGG_ASN1X_GENERALIZED_TIME, -1);
-
-       data = anode_get_value (node);
-       if (data == NULL)
-               return -1;
-
-       if (!anode_read_time (node, data, &when, &time))
-               g_return_val_if_reached (-1); /* already validated */
-       return time;
-}
-
-gboolean
-egg_asn1x_get_time_as_date (GNode *node,
-                            GDate *date)
-{
-       struct tm when;
-       GBytes *data;
-       glong time;
-       gint type;
-
-       g_return_val_if_fail (node, FALSE);
+       g_return_val_if_fail (node, NULL);
        type = anode_def_type (node);
 
        /* Time is often represented as a choice, so work than in here */
        if (type == EGG_ASN1X_CHOICE) {
                node = egg_asn1x_get_choice (node);
                if (node == NULL)
-                       return FALSE;
+                       return NULL;
                g_return_val_if_fail (anode_def_type (node) == EGG_ASN1X_TIME ||
                                      anode_def_type (node) == EGG_ASN1X_UTC_TIME ||
-                                     anode_def_type (node) == EGG_ASN1X_GENERALIZED_TIME, FALSE);
-               return egg_asn1x_get_time_as_date (node, date);
+                                     anode_def_type (node) == EGG_ASN1X_GENERALIZED_TIME, NULL);
+               return egg_asn1x_get_time_as_date_time (node);
        }
 
        g_return_val_if_fail (type == EGG_ASN1X_TIME ||
                              type == EGG_ASN1X_UTC_TIME ||
-                             type == EGG_ASN1X_GENERALIZED_TIME, FALSE);
+                             type == EGG_ASN1X_GENERALIZED_TIME, NULL);
 
        data = anode_get_value (node);
        if (data == NULL)
-               return FALSE;
-
-       if (!anode_read_time (node, data, &when, &time))
-               g_return_val_if_reached (FALSE); /* already validated */
+               return NULL;
 
-       g_date_set_dmy (date, when.tm_mday, when.tm_mon + 1, when.tm_year + 1900);
-       return TRUE;
+       if (!anode_read_time (node, data, &when, &offset))
+               g_return_val_if_reached (NULL); /* already validated */
+
+       timezone = g_time_zone_new_offset (offset);
+       ret = g_date_time_new (timezone,
+                              when.tm_year + 1900,
+                              when.tm_mon + 1,
+                              when.tm_mday,
+                              when.tm_hour,
+                              when.tm_min,
+                              when.tm_sec);
+       g_time_zone_unref (timezone);
+       return ret;
 }
 
 gchar*
@@ -3985,9 +3947,9 @@ static gboolean
 anode_validate_time (GNode *node,
                      GBytes *value)
 {
-       glong time;
        struct tm when;
-       return anode_read_time (node, value, &when, &time);
+       gint32 offset;
+       return anode_read_time (node, value, &when, &offset);
 }
 
 static gboolean
@@ -4718,72 +4680,6 @@ egg_asn1x_destroy (gpointer data)
        }
 }
 
-/* --------------------------------------------------------------------------------
- * TIME PARSING
- */
-
-glong
-egg_asn1x_parse_time_general (const gchar *time, gssize n_time)
-{
-       gboolean ret;
-       glong value;
-       struct tm when;
-       gint offset = 0;
-
-       g_return_val_if_fail (time, -1);
-
-       if (n_time < 0)
-               n_time = strlen (time);
-
-       ret = parse_general_time (time, n_time, &when, &offset);
-       if (!ret)
-               return -1;
-
-       /* In order to work with 32 bit time_t. */
-       if (sizeof (time_t) <= 4 && when.tm_year >= 138) {
-               value = (time_t)2145914603;  /* 2037-12-31 23:23:23 */
-
-       /* Convert to seconds since epoch */
-       } else {
-               value = timegm (&when);
-               g_return_val_if_fail (*time >= 0, FALSE);
-               value += offset;
-       }
-
-       return value;
-}
-
-glong
-egg_asn1x_parse_time_utc (const gchar *time, gssize n_time)
-{
-       gboolean ret;
-       glong value;
-       struct tm when;
-       gint offset = 0;
-
-       g_return_val_if_fail (time, -1);
-
-       if (n_time < 0)
-               n_time = strlen (time);
-
-       ret = parse_utc_time (time, n_time, &when, &offset);
-       if (!ret)
-               return -1;
-
-       /* In order to work with 32 bit time_t. */
-       if (sizeof (time_t) <= 4 && when.tm_year >= 138) {
-               value = (time_t)2145914603;  /* 2037-12-31 23:23:23 */
-
-       /* Convert to seconds since epoch */
-       } else {
-               value = timegm (&when);
-               g_return_val_if_fail (*time >= 0, FALSE);
-               value += offset;
-       }
-
-       return value;
-}
-
 /* --------------------------------------------------------------------------------
  * BASIC RAW ELEMENT INFO
  */
diff --git a/egg/egg-asn1x.h b/egg/egg-asn1x.h
index aab3f4ef..f0d66226 100644
--- a/egg/egg-asn1x.h
+++ b/egg/egg-asn1x.h
@@ -239,16 +239,7 @@ gboolean            egg_asn1x_set_string_as_utf8     (GNode *node,
 
 gchar *             egg_asn1x_get_bmpstring_as_utf8  (GNode *node);
 
-glong               egg_asn1x_get_time_as_long       (GNode *node);
-
-gboolean            egg_asn1x_set_time_as_long       (GNode *node,
-                                                      glong time);
-
-gboolean            egg_asn1x_get_time_as_date       (GNode *node,
-                                                      GDate *date);
-
-gboolean            egg_asn1x_set_time_as_date       (GNode *node,
-                                                      GDate *date);
+GDateTime *         egg_asn1x_get_time_as_date_time  (GNode *node);
 
 GQuark              egg_asn1x_get_oid_as_quark       (GNode *node);
 
@@ -262,12 +253,6 @@ gboolean            egg_asn1x_set_oid_as_string      (GNode *node,
 
 void                egg_asn1x_destroy                (gpointer asn);
 
-glong               egg_asn1x_parse_time_general     (const gchar *time,
-                                                      gssize n_time);
-
-glong               egg_asn1x_parse_time_utc         (const gchar *time,
-                                                      gssize n_time);
-
 gssize              egg_asn1x_element_length         (const guchar *data,
                                                       gsize n_data);
 
diff --git a/egg/test-asn1.c b/egg/test-asn1.c
index c4750b15..9f64a3dd 100644
--- a/egg/test-asn1.c
+++ b/egg/test-asn1.c
@@ -569,7 +569,7 @@ test_generalized_time (void)
 {
        GBytes *bytes;
        GNode *asn;
-       glong value;
+       GDateTime *value;
 
        asn = egg_asn1x_create (test_asn1_tab, "TestGeneralized");
        g_assert (asn);
@@ -578,22 +578,26 @@ test_generalized_time (void)
                g_assert_cmpint (EGG_ASN1X_GENERALIZED_TIME, ==, egg_asn1x_type (asn));
 
        /* Shouldn't succeed */
-       value = egg_asn1x_get_time_as_long (asn);
-       g_assert (value == -1);
+       value = egg_asn1x_get_time_as_date_time (asn);
+       g_assert_null (value);
 
        /* Should work */
        bytes = g_bytes_new_static (TGENERALIZED, XL (TGENERALIZED));
        if (!egg_asn1x_decode (asn, bytes))
                g_assert_not_reached ();
        g_bytes_unref (bytes);
-       value = egg_asn1x_get_time_as_long (asn);
-       g_assert (value == 1185368728);
+       value = egg_asn1x_get_time_as_date_time (asn);
+       g_assert_nonnull (value);
+       g_assert_cmpint (g_date_time_get_day_of_month (value), ==, 25);
+       g_assert_cmpint (g_date_time_get_month (value), ==, 7);
+       g_assert_cmpint (g_date_time_get_year (value), ==, 2007);
+       g_clear_pointer (&value, g_date_time_unref);
 
        egg_asn1x_clear (asn);
 
        /* Shouldn't succeed */
-       value = egg_asn1x_get_time_as_long (asn);
-       g_assert (value == -1);
+       value = egg_asn1x_get_time_as_date_time (asn);
+       g_assert_null (value);
 
        egg_asn1x_destroy (asn);
 }
@@ -601,13 +605,10 @@ test_generalized_time (void)
 static void
 test_time_get_missing (void)
 {
-       GDate date;
        GNode *asn;
 
        asn = egg_asn1x_create (test_asn1_tab, "TestGeneralized");
-       if (egg_asn1x_get_time_as_date (asn, &date))
-               g_assert_not_reached ();
-       g_assert (egg_asn1x_get_time_as_long (asn) == -1);
+       g_assert_null (egg_asn1x_get_time_as_date_time (asn));
        egg_asn1x_destroy (asn);
 }
 
@@ -2058,103 +2059,30 @@ test_oid_get_no_value (void)
        egg_asn1x_destroy (asn);
 }
 
-typedef struct _TimeTestData {
-       gchar *value;
-       time_t ref;
-} TimeTestData;
-
-static const TimeTestData generalized_time_test_data[] = {
-       { "20070725130528Z", 1185368728 },
-       { "20070725130528.2134Z", 1185368728 },
-       { "20070725140528-0100", 1185368728 },
-       { "20070725040528+0900", 1185368728 },
-       { "20070725013528+1130", 1185368728 },
-       { "20070725Z", 1185321600 },
-       { "20070725+0000", 1185321600 },
-
-       /* Bad ones */
-       { "200707", -1 },
-
-       { NULL, 0 }
-};
-
-static const TimeTestData utc_time_test_data[] = {
-       /* Test the Y2K style wrap arounds */
-       { "070725130528Z", 1185368728 },  /* The year 2007 */
-       { "020725130528Z", 1027602328 },  /* The year 2002 */
-       { "970725130528Z", 869835928 },   /* The year 1997 */
-       { "370725130528Z", 2132139928 },  /* The year 2037 */
-
-       /* Test the time zones and other formats */
-       { "070725130528.2134Z", 1185368728 },
-       { "070725140528-0100", 1185368728 },
-       { "070725040528+0900", 1185368728 },
-       { "070725013528+1130", 1185368728 },
-       { "070725Z", 1185321600 },
-       { "070725+0000", 1185321600 },
-
-       /* Bad ones */
-       { "0707", -1 },
-
-       { NULL, 0 }
-};
-
-static void
-test_general_time (Test* test, gconstpointer unused)
-{
-       time_t when;
-       const TimeTestData *data;
-
-       for (data = generalized_time_test_data; data->value; ++data) {
-               when = egg_asn1x_parse_time_general (data->value, -1);
-               if (data->ref != when) {
-                       printf ("%s", data->value);
-                       printf ("%s != ", ctime (&when));
-                       printf ("%s\n", ctime (&data->ref));
-                       fflush (stdout);
-               }
-
-               g_assert ("decoded time doesn't match reference" && data->ref == when);
-       }
-}
-
-static void
-test_utc_time (Test* test, gconstpointer unused)
-{
-       time_t when;
-       const TimeTestData *data;
-
-       for (data = utc_time_test_data; data->value; ++data) {
-               when = egg_asn1x_parse_time_utc (data->value, -1);
-               if (data->ref != when) {
-                       printf ("%s", data->value);
-                       printf ("%s != ", ctime (&when));
-                       printf ("%s\n", ctime (&data->ref));
-                       fflush (stdout);
-               }
-
-               g_assert ("decoded time doesn't match reference" && data->ref == when);
-       }
-}
-
 static void
 test_read_time (Test* test, gconstpointer unused)
 {
-       glong time;
+       GDateTime *val;
 
-       time = egg_asn1x_get_time_as_long (egg_asn1x_node (test->asn1, "tbsCertificate", "validity", 
"notBefore", NULL));
-       g_assert_cmpint (time, ==, 820454400);
+       val = egg_asn1x_get_time_as_date_time (egg_asn1x_node (test->asn1, "tbsCertificate", "validity", 
"notBefore", NULL));
+       g_assert_nonnull (val);
+       g_assert_cmpint (g_date_time_to_unix (val), ==, 820454400);
+       g_clear_pointer (&val, g_date_time_unref);
 }
 
 static void
 test_read_date (Test* test, gconstpointer unused)
 {
-       GDate date;
-       if (!egg_asn1x_get_time_as_date (egg_asn1x_node (test->asn1, "tbsCertificate", "validity", 
"notAfter", NULL), &date))
-               g_assert_not_reached ();
-       g_assert_cmpint (date.day, ==, 31);
-       g_assert_cmpint (date.month, ==, 12);
-       g_assert_cmpint (date.year, ==, 2020);
+       GDateTime *datetime;
+
+       datetime = egg_asn1x_get_time_as_date_time (egg_asn1x_node (test->asn1, "tbsCertificate", "validity", 
"notAfter", NULL));
+       g_assert_nonnull (datetime);
+       g_assert_cmpint (g_date_time_get_day_of_month (datetime), ==, 31);
+       g_assert_cmpint (g_date_time_get_month (datetime), ==, 12);
+       g_assert_cmpint (g_date_time_get_year (datetime), ==, 2020);
+       g_assert_cmpint (g_date_time_get_utc_offset (datetime), ==, 0);
+
+       g_clear_pointer (&datetime, g_date_time_unref);
 }
 
 static void
@@ -2545,8 +2473,6 @@ main (int argc, char **argv)
        g_test_add ("/asn1/write_value", Test, NULL, setup, test_write_value, teardown);
        g_test_add ("/asn1/element_length_content", Test, NULL, setup, test_element_length_content, teardown);
        g_test_add ("/asn1/read_element", Test, NULL, setup, test_read_element, teardown);
-       g_test_add ("/asn1/general_time", Test, NULL, setup, test_general_time, teardown);
-       g_test_add ("/asn1/utc_time", Test, NULL, setup, test_utc_time, teardown);
        g_test_add ("/asn1/read_time", Test, NULL, setup, test_read_time, teardown);
        g_test_add ("/asn1/read_date", Test, NULL, setup, test_read_date, teardown);
        g_test_add ("/asn1/create_by_oid", Test, NULL, setup, test_create_by_oid, teardown);
diff --git a/gcr-gtk3/gcr-certificate-widget.c b/gcr-gtk3/gcr-certificate-widget.c
index 32d5b721..cf995d58 100644
--- a/gcr-gtk3/gcr-certificate-widget.c
+++ b/gcr-gtk3/gcr-certificate-widget.c
@@ -540,7 +540,7 @@ gcr_certificate_widget_set_certificate (GcrCertificateWidget *self, GcrCertifica
        GQuark oid;
        gconstpointer data;
        gsize n_data;
-       GDate date;
+       GDateTime *datetime;
        gulong version;
        guint bits, index;
 
@@ -569,12 +569,13 @@ gcr_certificate_widget_set_certificate (GcrCertificateWidget *self, GcrCertifica
        gcr_section_add_child (GCR_SECTION (section), _("Verified by"), create_value_label (display));
        g_clear_pointer (&display, g_free);
 
-       if (egg_asn1x_get_time_as_date (egg_asn1x_node (asn, "tbsCertificate", "validity", "notAfter", NULL), 
&date)) {
-               display = g_malloc0 (128);
-               if (!g_date_strftime (display, 128, "%x", &date))
-                       g_return_if_reached ();
+       datetime = egg_asn1x_get_time_as_date_time (egg_asn1x_node (asn, "tbsCertificate", "validity", 
"notAfter", NULL));
+       if (datetime) {
+               display = g_date_time_format (datetime, "%x");
+               g_return_if_fail (display != NULL);
                gcr_section_add_child (GCR_SECTION (section), _("Expires"), create_value_label (display));
                g_clear_pointer (&display, g_free);
+               g_clear_pointer (&datetime, g_date_time_unref);
        }
 
        /* The subject */
@@ -606,18 +607,22 @@ gcr_certificate_widget_set_certificate (GcrCertificateWidget *self, GcrCertifica
                g_bytes_unref (number);
        }
 
-       display = g_malloc0 (128);
-       if (egg_asn1x_get_time_as_date (egg_asn1x_node (asn, "tbsCertificate", "validity", "notBefore", 
NULL), &date)) {
-               if (!g_date_strftime (display, 128, "%x", &date))
-                       g_return_if_reached ();
+       datetime = egg_asn1x_get_time_as_date_time (egg_asn1x_node (asn, "tbsCertificate", "validity", 
"notBefore", NULL));
+       if (datetime) {
+               display = g_date_time_format (datetime, "%x");
+               g_return_if_fail (display != NULL);
                gcr_section_add_child (GCR_SECTION (section), _("Not Valid Before"), create_value_label 
(display));
+               g_clear_pointer (&display, g_free);
+               g_clear_pointer (&datetime, g_date_time_unref);
        }
-       if (egg_asn1x_get_time_as_date (egg_asn1x_node (asn, "tbsCertificate", "validity", "notAfter", NULL), 
&date)) {
-               if (!g_date_strftime (display, 128, "%x", &date))
-                       g_return_if_reached ();
+       datetime = egg_asn1x_get_time_as_date_time (egg_asn1x_node (asn, "tbsCertificate", "validity", 
"notAfter", NULL));
+       if (datetime) {
+               display = g_date_time_format (datetime, "%x");
+               g_return_if_fail (display != NULL);
                gcr_section_add_child (GCR_SECTION (section), _("Not Valid After"), create_value_label 
(display));
+               g_clear_pointer (&display, g_free);
+               g_clear_pointer (&datetime, g_date_time_unref);
        }
-       g_clear_pointer (&display, g_free);
 
        /* Fingerprints */
        section = _gcr_certificate_widget_add_section (self, _("Certificate Fingerprints"), FALSE);
diff --git a/gcr-gtk4/gcr-certificate-widget.c b/gcr-gtk4/gcr-certificate-widget.c
index c0242fca..0acdc7b2 100644
--- a/gcr-gtk4/gcr-certificate-widget.c
+++ b/gcr-gtk4/gcr-certificate-widget.c
@@ -547,7 +547,7 @@ gcr_certificate_widget_set_certificate (GcrCertificateWidget *self, GcrCertifica
        GQuark oid;
        gconstpointer data;
        gsize n_data;
-       GDate date;
+       GDateTime *datetime;
        gulong version;
        guint bits, index;
 
@@ -576,12 +576,13 @@ gcr_certificate_widget_set_certificate (GcrCertificateWidget *self, GcrCertifica
        gcr_section_add_child (GCR_SECTION (section), _("Verified by"), create_value_label (display));
        g_clear_pointer (&display, g_free);
 
-       if (egg_asn1x_get_time_as_date (egg_asn1x_node (asn, "tbsCertificate", "validity", "notAfter", NULL), 
&date)) {
-               display = g_malloc0 (128);
-               if (!g_date_strftime (display, 128, "%x", &date))
-                       g_return_if_reached ();
+       datetime = egg_asn1x_get_time_as_date_time (egg_asn1x_node (asn, "tbsCertificate", "validity", 
"notAfter", NULL));
+       if (datetime) {
+               display = g_date_time_format (datetime, "%x");
+               g_return_if_fail (display != NULL);
                gcr_section_add_child (GCR_SECTION (section), _("Expires"), create_value_label (display));
                g_clear_pointer (&display, g_free);
+               g_clear_pointer (&datetime, g_date_time_unref);
        }
 
        /* The subject */
@@ -613,18 +614,22 @@ gcr_certificate_widget_set_certificate (GcrCertificateWidget *self, GcrCertifica
                g_bytes_unref (number);
        }
 
-       display = g_malloc0 (128);
-       if (egg_asn1x_get_time_as_date (egg_asn1x_node (asn, "tbsCertificate", "validity", "notBefore", 
NULL), &date)) {
-               if (!g_date_strftime (display, 128, "%x", &date))
-                       g_return_if_reached ();
+       datetime = egg_asn1x_get_time_as_date_time (egg_asn1x_node (asn, "tbsCertificate", "validity", 
"notBefore", NULL));
+       if (datetime) {
+               display = g_date_time_format (datetime, "%x");
+               g_return_if_fail (display != NULL);
                gcr_section_add_child (GCR_SECTION (section), _("Not Valid Before"), create_value_label 
(display));
+               g_clear_pointer (&display, g_free);
+               g_clear_pointer (&datetime, g_date_time_unref);
        }
-       if (egg_asn1x_get_time_as_date (egg_asn1x_node (asn, "tbsCertificate", "validity", "notAfter", NULL), 
&date)) {
-               if (!g_date_strftime (display, 128, "%x", &date))
-                       g_return_if_reached ();
+       datetime = egg_asn1x_get_time_as_date_time (egg_asn1x_node (asn, "tbsCertificate", "validity", 
"notAfter", NULL));
+       if (datetime) {
+               display = g_date_time_format (datetime, "%x");
+               g_return_if_fail (display != NULL);
                gcr_section_add_child (GCR_SECTION (section), _("Not Valid After"), create_value_label 
(display));
+               g_clear_pointer (&display, g_free);
+               g_clear_pointer (&datetime, g_date_time_unref);
        }
-       g_clear_pointer (&display, g_free);
 
        /* Fingerprints */
        section = _gcr_certificate_widget_add_section (self, _("Certificate Fingerprints"), FALSE);
diff --git a/gcr/gcr-certificate.c b/gcr/gcr-certificate.c
index 3e839272..13b2379e 100644
--- a/gcr/gcr-certificate.c
+++ b/gcr/gcr-certificate.c
@@ -273,7 +273,7 @@ gcr_certificate_default_init (GcrCertificateIface *iface)
                 */
                g_object_interface_install_property (iface,
                           g_param_spec_boxed ("expiry-date", "Expiry date", "Certificate expiry date",
-                                              G_TYPE_DATE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+                                              G_TYPE_DATE_TIME, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
 
                g_once_init_leave (&initialized, 1);
        }
@@ -687,16 +687,12 @@ gcr_certificate_get_subject_raw (GcrCertificate *self, gsize *n_data)
  *
  * Get the issued date of this certificate.
  *
- * The #GDate returned should be freed by the caller using
- * g_date_free() when no longer required.
- *
- * Returns: (nullable): An allocated issued date of this certificate.
+ * Returns: (nullable): A issued date of this certificate.
  */
-GDate*
+GDateTime *
 gcr_certificate_get_issued_date (GcrCertificate *self)
 {
        GcrCertificateInfo *info;
-       GDate *date;
 
        g_return_val_if_fail (GCR_IS_CERTIFICATE (self), NULL);
 
@@ -704,13 +700,7 @@ gcr_certificate_get_issued_date (GcrCertificate *self)
        if (info == NULL)
                return NULL;
 
-       date = g_date_new ();
-       if (!egg_asn1x_get_time_as_date (egg_asn1x_node (info->asn1, "tbsCertificate", "validity", 
"notBefore", NULL), date)) {
-               g_date_free (date);
-               return NULL;
-       }
-
-       return date;
+       return egg_asn1x_get_time_as_date_time (egg_asn1x_node (info->asn1, "tbsCertificate", "validity", 
"notBefore", NULL));
 }
 
 /**
@@ -719,16 +709,12 @@ gcr_certificate_get_issued_date (GcrCertificate *self)
  *
  * Get the expiry date of this certificate.
  *
- * The #GDate returned should be freed by the caller using
- * g_date_free() when no longer required.
- *
- * Returns: (nullable): An allocated expiry date of this certificate.
+ * Returns: (nullable): An expiry date of this certificate.
  */
-GDate*
+GDateTime *
 gcr_certificate_get_expiry_date (GcrCertificate *self)
 {
        GcrCertificateInfo *info;
-       GDate *date;
 
        g_return_val_if_fail (GCR_IS_CERTIFICATE (self), NULL);
 
@@ -736,13 +722,7 @@ gcr_certificate_get_expiry_date (GcrCertificate *self)
        if (info == NULL)
                return NULL;
 
-       date = g_date_new ();
-       if (!egg_asn1x_get_time_as_date (egg_asn1x_node (info->asn1, "tbsCertificate", "validity", 
"notAfter", NULL), date)) {
-               g_date_free (date);
-               return NULL;
-       }
-
-       return date;
+       return egg_asn1x_get_time_as_date_time (egg_asn1x_node (info->asn1, "tbsCertificate", "validity", 
"notAfter", NULL));
 }
 
 /**
diff --git a/gcr/gcr-certificate.h b/gcr/gcr-certificate.h
index 444b2f58..ab65026e 100644
--- a/gcr/gcr-certificate.h
+++ b/gcr/gcr-certificate.h
@@ -102,9 +102,9 @@ gchar*              gcr_certificate_get_subject_part       (GcrCertificate *self
 guchar *            gcr_certificate_get_subject_raw        (GcrCertificate *self,
                                                             gsize *n_data);
 
-GDate*              gcr_certificate_get_issued_date        (GcrCertificate *self);
+GDateTime *         gcr_certificate_get_issued_date        (GcrCertificate *self);
 
-GDate*              gcr_certificate_get_expiry_date        (GcrCertificate *self);
+GDateTime *         gcr_certificate_get_expiry_date        (GcrCertificate *self);
 
 guchar*             gcr_certificate_get_serial_number      (GcrCertificate *self,
                                                             gsize *n_length);
diff --git a/gcr/test-certificate.c b/gcr/test-certificate.c
index 2fed913a..8bca0343 100644
--- a/gcr/test-certificate.c
+++ b/gcr/test-certificate.c
@@ -174,23 +174,23 @@ test_subject_raw (Test *test, gconstpointer unused)
 static void
 test_issued_date (Test *test, gconstpointer unused)
 {
-       GDate *date = gcr_certificate_get_issued_date (test->certificate);
-       g_assert (date);
-       g_assert_cmpuint (g_date_get_year (date), ==, 1999);
-       g_assert_cmpuint (g_date_get_month (date), ==, 6);
-       g_assert_cmpuint (g_date_get_day (date), ==, 26);
-       g_date_free (date);
+       GDateTime *date = gcr_certificate_get_issued_date (test->certificate);
+       g_assert_nonnull (date);
+       g_assert_cmpuint (g_date_time_get_year (date), ==, 1999);
+       g_assert_cmpuint (g_date_time_get_month (date), ==, 6);
+       g_assert_cmpuint (g_date_time_get_day_of_month (date), ==, 26);
+       g_date_time_unref (date);
 }
 
 static void
 test_expiry_date (Test *test, gconstpointer unused)
 {
-       GDate *date = gcr_certificate_get_expiry_date (test->certificate);
+       GDateTime *date = gcr_certificate_get_expiry_date (test->certificate);
        g_assert (date);
-       g_assert_cmpuint (g_date_get_year (date), ==, 2019);
-       g_assert_cmpuint (g_date_get_month (date), ==, 6);
-       g_assert_cmpuint (g_date_get_day (date), ==, 26);
-       g_date_free (date);
+       g_assert_cmpuint (g_date_time_get_year (date), ==, 2019);
+       g_assert_cmpuint (g_date_time_get_month (date), ==, 6);
+       g_assert_cmpuint (g_date_time_get_day_of_month (date), ==, 26);
+       g_date_time_unref (date);
 }
 
 static void


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