[gnome-builder/wip/project-selector: 33/67] datetime: add gb_date_time_format_for_display()
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder/wip/project-selector: 33/67] datetime: add gb_date_time_format_for_display()
- Date: Mon, 6 Apr 2015 21:31:56 +0000 (UTC)
commit 3d96d5d029ecbd12d4374398297bf612a0692bb2
Author: Christian Hergert <christian hergert me>
Date: Thu Apr 2 16:17:18 2015 -0700
datetime: add gb_date_time_format_for_display()
An imprecise date time format string such as "Yesterday".
src/gnome-builder.mk | 1 +
src/initial-setup/gb-initial-setup-dialog.c | 35 +-------------
src/util/gb-glib.c | 70 +++++++++++++++++++++++++++
src/util/gb-glib.h | 2 +
4 files changed, 75 insertions(+), 33 deletions(-)
---
diff --git a/src/gnome-builder.mk b/src/gnome-builder.mk
index bfb725b..aed13c4 100644
--- a/src/gnome-builder.mk
+++ b/src/gnome-builder.mk
@@ -112,6 +112,7 @@ libgnome_builder_la_SOURCES = \
src/util/gb-cairo.h \
src/util/gb-dnd.c \
src/util/gb-dnd.h \
+ src/util/gb-glib.c \
src/util/gb-glib.h \
src/util/gb-gtk.c \
src/util/gb-gtk.h \
diff --git a/src/initial-setup/gb-initial-setup-dialog.c b/src/initial-setup/gb-initial-setup-dialog.c
index 2db33bd..e1f61db 100644
--- a/src/initial-setup/gb-initial-setup-dialog.c
+++ b/src/initial-setup/gb-initial-setup-dialog.c
@@ -19,6 +19,7 @@
#include <glib/gi18n.h>
#include <ide.h>
+#include "gb-glib.h"
#include "gb-initial-setup-dialog.h"
#include "gb-scrolled-window.h"
#include "gb-widget.h"
@@ -52,38 +53,6 @@ enum {
static GParamSpec *gParamSpecs [LAST_PROP];
-static gchar *
-_g_date_time_format_for_display (GDateTime *datetime)
-{
- GDateTime *now;
- GTimeSpan diff;
- gint years;
-
- g_return_val_if_fail (datetime != NULL, NULL);
-
- now = g_date_time_new_now_utc ();
- diff = g_date_time_difference (now, datetime) / G_USEC_PER_SEC;
-
- if (diff < 0)
- return g_strdup ("");
- else if (diff < (60 * 45))
- return g_strdup (_("Just now"));
- else if (diff < (60 * 90))
- return g_strdup (_("An hour ago"));
- else if (diff < (60 * 60 * 24 * 2))
- return g_strdup (_("Yesterday"));
- else if (diff < (60 * 60 * 24 * 7))
- return g_date_time_format (datetime, "%A");
- else if (diff < (60 * 60 * 24 * 365))
- return g_date_time_format (datetime, "%B");
- else if (diff < (60 * 60 * 24 * 365 * 1.5))
- return g_strdup (_("About a year ago"));
-
- years = MAX (2, diff / (60 * 60 * 24 * 365));
-
- return g_strdup_printf (_("About %u years ago"), years);
-}
-
GbInitialSetupPage
gb_initial_setup_dialog_get_page (GbInitialSetupDialog *self)
{
@@ -163,7 +132,7 @@ gb_initial_setup_dialog__miner_discovered_cb (GbInitialSetupDialog *self,
last_modified_at = ide_project_info_get_last_modified_at (project_info);
if (last_modified_at)
- display_date = _g_date_time_format_for_display (last_modified_at);
+ display_date = gb_date_time_format_for_display (last_modified_at);
row = g_object_new (GTK_TYPE_LIST_BOX_ROW,
"visible", TRUE,
diff --git a/src/util/gb-glib.c b/src/util/gb-glib.c
new file mode 100644
index 0000000..02f247d
--- /dev/null
+++ b/src/util/gb-glib.c
@@ -0,0 +1,70 @@
+/* gb-glib.c
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <glib/gi18n.h>
+
+#include "gb-glib.h"
+
+/**
+ * gb_date_time_format_for_display:
+ * @self: A #GDateTime
+ *
+ * Helper function to "humanize" a #GDateTime into a relative time
+ * relationship string.
+ *
+ * Returns: (transfer full): A newly allocated string describing the
+ * date and time imprecisely such as "Yesterday".
+ */
+gchar *
+gb_date_time_format_for_display (GDateTime *self)
+{
+ GDateTime *now;
+ GTimeSpan diff;
+ gint years;
+
+ /*
+ * TODO:
+ *
+ * There is probably a lot more we can do here to be friendly for
+ * various locales, but this will get us started.
+ */
+
+ g_return_val_if_fail (self != NULL, NULL);
+
+ now = g_date_time_new_now_utc ();
+ diff = g_date_time_difference (now, self) / G_USEC_PER_SEC;
+
+ if (diff < 0)
+ return g_strdup ("");
+ else if (diff < (60 * 45))
+ return g_strdup (_("Just now"));
+ else if (diff < (60 * 90))
+ return g_strdup (_("An hour ago"));
+ else if (diff < (60 * 60 * 24 * 2))
+ return g_strdup (_("Yesterday"));
+ else if (diff < (60 * 60 * 24 * 7))
+ return g_date_time_format (self, "%A");
+ else if (diff < (60 * 60 * 24 * 365))
+ return g_date_time_format (self, "%B");
+ else if (diff < (60 * 60 * 24 * 365 * 1.5))
+ return g_strdup (_("About a year ago"));
+
+ years = MAX (2, diff / (60 * 60 * 24 * 365));
+
+ return g_strdup_printf (_("About %u years ago"), years);
+}
diff --git a/src/util/gb-glib.h b/src/util/gb-glib.h
index c7f9f36..1942a68 100644
--- a/src/util/gb-glib.h
+++ b/src/util/gb-glib.h
@@ -42,6 +42,8 @@ G_BEGIN_DECLS
} \
} G_STMT_END
+gchar *gb_date_time_format_for_display (GDateTime *self);
+
G_END_DECLS
#endif /* GB_GLIB_H */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]