[gnome-software/1111-version-history-box: 68/71] src: Move time-to-string function to gs-common
- From: Phaedrus Leeds <mwleeds src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-software/1111-version-history-box: 68/71] src: Move time-to-string function to gs-common
- Date: Thu, 11 Feb 2021 05:48:12 +0000 (UTC)
commit 130832d69c19357ee4c0a1785c331e1c47dc80a3
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. Also change it to be
more in line with how time spans are displayed in the mock-ups for the
version history widget, e.g. "5 weeks ago" instead of "25 May 2012".
This code is based on code in gnome-shell's js/misc/util.js.
src/gs-common.c | 66 +++++++++++++++++++++++++++++++++++
src/gs-common.h | 1 +
src/gs-updates-page.c | 95 ++-------------------------------------------------
src/meson.build | 1 +
4 files changed, 71 insertions(+), 92 deletions(-)
---
diff --git a/src/gs-common.c b/src/gs-common.c
index 404f9ae93..79ff95c4a 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
@@ -654,3 +660,63 @@ 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);
}
+
+/**
+ * gs_utils_time_to_string:
+ * @unix_time_seconds: Time since the epoch in seconds
+ *
+ * Converts a time to a string such as "5 minutes ago" 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 (gint64 unix_time_seconds)
+{
+ gint minutes_ago, hours_ago, days_ago;
+ gint weeks_ago, months_ago, years_ago;
+ g_autoptr(GDateTime) date_time = NULL;
+ g_autoptr(GDateTime) now = NULL;
+ GTimeSpan timespan;
+
+ if (unix_time_seconds <= 0)
+ return NULL;
+ date_time = g_date_time_new_from_unix_local (unix_time_seconds);
+ now = g_date_time_new_now_local ();
+ timespan = g_date_time_difference (now, date_time);
+
+ minutes_ago = (gint) (timespan / G_TIME_SPAN_MINUTE);
+ hours_ago = (gint) (timespan / G_TIME_SPAN_HOUR);
+ days_ago = (gint) (timespan / G_TIME_SPAN_DAY);
+ weeks_ago = days_ago / 7;
+ months_ago = days_ago / 30;
+ years_ago = weeks_ago / 52;
+
+ if (minutes_ago < 5) {
+ /* TRANSLATORS: something happened less than 5 minutes ago */
+ return g_strdup (_("Just now"));
+ } else if (hours_ago < 1)
+ return g_strdup_printf (ngettext ("%d minute ago",
+ "%d minutes ago", minutes_ago),
+ minutes_ago);
+ else if (days_ago < 1)
+ return g_strdup_printf (ngettext ("%d hour ago",
+ "%d hours ago", hours_ago),
+ hours_ago);
+ else if (days_ago < 15)
+ return g_strdup_printf (ngettext ("%d day ago",
+ "%d days ago", days_ago),
+ days_ago);
+ else if (weeks_ago < 8)
+ return g_strdup_printf (ngettext ("%d week ago",
+ "%d weeks ago", weeks_ago),
+ weeks_ago);
+ else if (years_ago < 1)
+ return g_strdup_printf (ngettext ("%d month ago",
+ "%d months ago", months_ago),
+ months_ago);
+ else
+ return g_strdup_printf (ngettext ("%d year ago",
+ "%d years ago", years_ago),
+ years_ago);
+}
diff --git a/src/gs-common.h b/src/gs-common.h
index 63c8e86bf..cbd6dc61c 100644
--- a/src/gs-common.h
+++ b/src/gs-common.h
@@ -47,5 +47,6 @@ gchar *gs_utils_build_unique_id_kind (AsComponentKind kind,
gboolean gs_utils_list_has_component_fuzzy (GsAppList *list,
GsApp *app);
void gs_utils_reboot_notify (GsAppList *list);
+gchar *gs_utils_time_to_string (gint64 unix_time_seconds);
G_END_DECLS
diff --git a/src/gs-updates-page.c b/src/gs-updates-page.c
index 37919fc53..0b51283ee 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 (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 *
diff --git a/src/meson.build b/src/meson.build
index 364b86387..7e4cf4f66 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -241,6 +241,7 @@ if get_option('tests')
glib,
gmodule,
goa,
+ gsettings_desktop_schemas,
gtk,
json_glib,
libgnomesoftware_dep,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]