[gnome-software/1111-version-history-box: 1/2] src: Move time-to-string function to gs-common




commit 7520b4a5c265ac76cd963f870e75950f8cc469b4
Author: Phaedrus Leeds <mwleeds endlessos org>
Date:   Wed Feb 3 18:41:44 2021 -0800

    src: Move time-to-string function to gs-common
    
    This will allow it to be reused in later commits.

 src/gs-common.c       | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/gs-common.h       |   2 +
 src/gs-updates-page.c |  95 ++-----------------------------------------
 3 files changed, 114 insertions(+), 92 deletions(-)
---
diff --git a/src/gs-common.c b/src/gs-common.c
index 1e5358bfe..0088d4320 100644
--- a/src/gs-common.c
+++ b/src/gs-common.c
@@ -14,6 +14,12 @@
 
 #include "gs-common.h"
 
+#ifdef HAVE_GSETTINGS_DESKTOP_SCHEMAS
+#include <gdesktop-enums.h>
+#endif
+
+#include <langinfo.h>
+
 #define SPINNER_DELAY 500
 
 static gboolean
@@ -653,3 +659,106 @@ gs_utils_reboot_notify (GsAppList *list)
        g_notification_set_priority (n, G_NOTIFICATION_PRIORITY_URGENT);
        g_application_send_notification (g_application_get_default (), "restart-required", n);
 }
+
+static GDateTime *
+time_next_midnight (void)
+{
+       GDateTime *next_midnight;
+       GTimeSpan since_midnight;
+       g_autoptr(GDateTime) now = NULL;
+
+       now = g_date_time_new_now_local ();
+       since_midnight = g_date_time_get_hour (now) * G_TIME_SPAN_HOUR +
+                        g_date_time_get_minute (now) * G_TIME_SPAN_MINUTE +
+                        g_date_time_get_second (now) * G_TIME_SPAN_SECOND +
+                        g_date_time_get_microsecond (now);
+       next_midnight = g_date_time_add (now, G_TIME_SPAN_DAY - since_midnight);
+
+       return next_midnight;
+}
+
+/**
+ * gs_utils_time_to_string:
+ * @desktop_settings: A #GSettings object for org.gnome.desktop.interface
+ * @unix_time_seconds: Time since the epoch in seconds
+ *
+ * Converts a time to a string such as "3:45 PM" or "2 weeks ago"
+ *
+ * Returns: (transfer full): the time string, or %NULL if @unix_time_seconds is
+ *   not valid
+ */
+gchar *
+gs_utils_time_to_string (GSettings *desktop_settings, gint64 unix_time_seconds)
+{
+#ifdef HAVE_GSETTINGS_DESKTOP_SCHEMAS
+       GDesktopClockFormat clock_format;
+#endif
+       const gchar *format_string;
+       gchar *time_string;
+       gboolean use_24h_time = FALSE;
+       gboolean ampm_available = FALSE;
+       gint days_ago;
+       g_autoptr(GDateTime) date_time = NULL;
+       g_autoptr(GDateTime) midnight = NULL;
+       const char *ampm;
+
+       if (unix_time_seconds <= 0)
+               return NULL;
+       date_time = g_date_time_new_from_unix_local (unix_time_seconds);
+
+       midnight = time_next_midnight ();
+       days_ago = (gint) (g_date_time_difference (midnight, date_time) / G_TIME_SPAN_DAY);
+
+       ampm = nl_langinfo (AM_STR);
+       if (ampm != NULL && *ampm != '\0')
+               ampm_available = TRUE;
+       else
+               use_24h_time = TRUE;
+
+#ifdef HAVE_GSETTINGS_DESKTOP_SCHEMAS
+       clock_format = g_settings_get_enum (desktop_settings, "clock-format");
+       use_24h_time = (clock_format == G_DESKTOP_CLOCK_FORMAT_24H || ampm_available == FALSE);
+#endif
+
+       if (days_ago < 1) { // today
+               if (use_24h_time) {
+                       /* TRANSLATORS: Time in 24h format */
+                       format_string = _("%R");
+               } else {
+                       /* TRANSLATORS: Time in 12h format */
+                       format_string = _("%l:%M %p");
+               }
+       } else if (days_ago < 2) { // yesterday
+               if (use_24h_time) {
+                       /* TRANSLATORS: This is the word "Yesterday" followed by a
+                          time string in 24h format. i.e. "Yesterday, 14:30" */
+                       format_string = _("Yesterday, %R");
+               } else {
+                       /* TRANSLATORS: This is the word "Yesterday" followed by a
+                          time string in 12h format. i.e. "Yesterday, 2:30 PM" */
+                       format_string = _("Yesterday, %l:%M %p");
+               }
+       } else if (days_ago < 3) {
+               format_string = _("Two days ago");
+       } else if (days_ago < 4) {
+               format_string = _("Three days ago");
+       } else if (days_ago < 5) {
+               format_string = _("Four days ago");
+       } else if (days_ago < 6) {
+               format_string = _("Five days ago");
+       } else if (days_ago < 7) {
+               format_string = _("Six days ago");
+       } else if (days_ago < 8) {
+               format_string = _("One week ago");
+       } else if (days_ago < 15) {
+               format_string = _("Two weeks ago");
+       } else {
+               /* TRANSLATORS: This is the date string with: day number, month name, year.
+                  i.e. "25 May 2012" */
+               format_string = _("%e %B %Y");
+       }
+
+       time_string = g_date_time_format (date_time, format_string);
+
+       return time_string;
+}
diff --git a/src/gs-common.h b/src/gs-common.h
index 7540aef74..7df1183b3 100644
--- a/src/gs-common.h
+++ b/src/gs-common.h
@@ -47,5 +47,7 @@ gchar         *gs_utils_build_unique_id_kind  (AsAppKind       kind,
 gboolean        gs_utils_list_has_app_fuzzy    (GsAppList      *list,
                                                 GsApp          *app);
 void            gs_utils_reboot_notify         (GsAppList      *list);
+gchar          *gs_utils_time_to_string        (GSettings      *desktop_settings,
+                                                gint64          unix_time_seconds);
 
 G_END_DECLS
diff --git a/src/gs-updates-page.c b/src/gs-updates-page.c
index 9c04975d8..8c66adfbe 100644
--- a/src/gs-updates-page.c
+++ b/src/gs-updates-page.c
@@ -23,12 +23,6 @@
 #include "gs-upgrade-banner.h"
 #include "gs-application.h"
 
-#ifdef HAVE_GSETTINGS_DESKTOP_SCHEMAS
-#include <gdesktop-enums.h>
-#endif
-
-#include <langinfo.h>
-
 typedef enum {
        GS_UPDATES_PAGE_FLAG_NONE               = 0,
        GS_UPDATES_PAGE_FLAG_HAS_UPDATES        = 1 << 0,
@@ -176,91 +170,13 @@ _get_num_updates (GsUpdatesPage *self)
        return count;
 }
 
-static GDateTime *
-time_next_midnight (void)
-{
-       GDateTime *next_midnight;
-       GTimeSpan since_midnight;
-       g_autoptr(GDateTime) now = NULL;
-
-       now = g_date_time_new_now_local ();
-       since_midnight = g_date_time_get_hour (now) * G_TIME_SPAN_HOUR +
-                        g_date_time_get_minute (now) * G_TIME_SPAN_MINUTE +
-                        g_date_time_get_second (now) * G_TIME_SPAN_SECOND +
-                        g_date_time_get_microsecond (now);
-       next_midnight = g_date_time_add (now, G_TIME_SPAN_DAY - since_midnight);
-
-       return next_midnight;
-}
-
 static gchar *
 gs_updates_page_last_checked_time_string (GsUpdatesPage *self)
 {
-#ifdef HAVE_GSETTINGS_DESKTOP_SCHEMAS
-       GDesktopClockFormat clock_format;
-#endif
-       const gchar *format_string;
-       gchar *time_string;
-       gboolean use_24h_time = FALSE;
-       gint64 tmp;
-       gint days_ago;
-       g_autoptr(GDateTime) last_checked = NULL;
-       g_autoptr(GDateTime) midnight = NULL;
-
-       g_settings_get (self->settings, "check-timestamp", "x", &tmp);
-       if (tmp == 0)
-               return NULL;
-       last_checked = g_date_time_new_from_unix_local (tmp);
-
-       midnight = time_next_midnight ();
-       days_ago = (gint) (g_date_time_difference (midnight, last_checked) / G_TIME_SPAN_DAY);
-
-#ifdef HAVE_GSETTINGS_DESKTOP_SCHEMAS
-       clock_format = g_settings_get_enum (self->desktop_settings, "clock-format");
-       use_24h_time = (clock_format == G_DESKTOP_CLOCK_FORMAT_24H || self->ampm_available == FALSE);
-#endif
-
-       if (days_ago < 1) { // today
-               if (use_24h_time) {
-                       /* TRANSLATORS: Time in 24h format */
-                       format_string = _("%R");
-               } else {
-                       /* TRANSLATORS: Time in 12h format */
-                       format_string = _("%l:%M %p");
-               }
-       } else if (days_ago < 2) { // yesterday
-               if (use_24h_time) {
-                       /* TRANSLATORS: This is the word "Yesterday" followed by a
-                          time string in 24h format. i.e. "Yesterday, 14:30" */
-                       format_string = _("Yesterday, %R");
-               } else {
-                       /* TRANSLATORS: This is the word "Yesterday" followed by a
-                          time string in 12h format. i.e. "Yesterday, 2:30 PM" */
-                       format_string = _("Yesterday, %l:%M %p");
-               }
-       } else if (days_ago < 3) {
-               format_string = _("Two days ago");
-       } else if (days_ago < 4) {
-               format_string = _("Three days ago");
-       } else if (days_ago < 5) {
-               format_string = _("Four days ago");
-       } else if (days_ago < 6) {
-               format_string = _("Five days ago");
-       } else if (days_ago < 7) {
-               format_string = _("Six days ago");
-       } else if (days_ago < 8) {
-               format_string = _("One week ago");
-       } else if (days_ago < 15) {
-               format_string = _("Two weeks ago");
-       } else {
-               /* TRANSLATORS: This is the date string with: day number, month name, year.
-                  i.e. "25 May 2012" */
-               format_string = _("%e %B %Y");
-       }
+       gint64 last_checked;
 
-       time_string = g_date_time_format (last_checked, format_string);
-
-       return time_string;
+       g_settings_get (self->settings, "check-timestamp", "x", &last_checked);
+       return gs_utils_time_to_string (self->settings, last_checked);
 }
 
 static const gchar *
@@ -1440,8 +1356,6 @@ gs_updates_page_class_init (GsUpdatesPageClass *klass)
 static void
 gs_updates_page_init (GsUpdatesPage *self)
 {
-       const char *ampm;
-
        gtk_widget_init_template (GTK_WIDGET (self));
 
        self->state = GS_UPDATES_PAGE_STATE_STARTUP;
@@ -1454,9 +1368,6 @@ gs_updates_page_init (GsUpdatesPage *self)
        self->sizegroup_button = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
        self->sizegroup_header = gtk_size_group_new (GTK_SIZE_GROUP_VERTICAL);
 
-       ampm = nl_langinfo (AM_STR);
-       if (ampm != NULL && *ampm != '\0')
-               self->ampm_available = TRUE;
 }
 
 GsUpdatesPage *


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