[gnome-control-center/wip/kate/improve-datetime] firmware-security: improve datetime format parsing




commit cef9878443a0adc30e100ee22662efac654b342e
Author: Kate Hsuan <hpa redhat com>
Date:   Mon Aug 29 11:46:53 2022 +0800

    firmware-security: improve datetime format parsing
    
    1. Be able to show "Today" and "Yesterday".
    2. The datetime format will be "\%d \%B \%Y \%H:\%m:\%S"
    
    Fixes: https://gitlab.gnome.org/GNOME/gnome-control-center/-/issues/2034

 panels/firmware-security/cc-firmware-security-panel.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
---
diff --git a/panels/firmware-security/cc-firmware-security-panel.c 
b/panels/firmware-security/cc-firmware-security-panel.c
index 8a5bb5e9a..3153a1ab7 100644
--- a/panels/firmware-security/cc-firmware-security-panel.c
+++ b/panels/firmware-security/cc-firmware-security-panel.c
@@ -170,8 +170,11 @@ parse_event_variant_iter (CcfirmwareSecurityPanel *self,
 {
   g_autofree gchar *date_string = NULL;
   g_autoptr (GDateTime) date = NULL;
+  g_autoptr (GDateTime) now_date = NULL;
   g_autoptr (FwupdSecurityAttr) attr = fu_security_attr_new_from_variant(iter);
   GtkWidget *row;
+  guint64 local_now_unix;
+  guint64 event_time_unix;
 
   /* unknown to us */
   if (attr->appstream_id == NULL || attr->title == NULL)
@@ -184,7 +187,16 @@ parse_event_variant_iter (CcfirmwareSecurityPanel *self,
 
   /* build new row */
   date = g_date_time_new_from_unix_local (attr->timestamp);
-  date_string = g_date_time_format (date, "\%F \%H:\%m:\%S");
+  event_time_unix = g_date_time_to_unix (date);
+  now_date = g_date_time_new_now_local ();
+  local_now_unix = g_date_time_to_unix (now_date);
+
+  if ((local_now_unix - event_time_unix) < 86400)
+    date_string = g_strdup_printf (_("Today"));
+  else if ((local_now_unix - event_time_unix) < (86400 * 2))
+     date_string = g_strdup_printf (_("Yesterday"));
+  else
+    date_string = g_date_time_format (date, "\%d \%B \%Y \%H:\%m:\%S");
 
   row = adw_expander_row_new ();
   if (attr->flags & FWUPD_SECURITY_ATTR_FLAG_SUCCESS)


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