[gnome-builder] util: add gb_date_time_format_for_display()
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] util: add gb_date_time_format_for_display()
- Date: Wed, 8 Apr 2015 07:25:50 +0000 (UTC)
commit 699beb1b1c974444bb1b43fadac8bb5744d4a96a
Author: Christian Hergert <christian hergert me>
Date: Tue Apr 7 23:54:03 2015 -0700
util: add gb_date_time_format_for_display()
Adds an imprecise date time format string that uses relative time instead
of precise dates and times.
Example: "Just now" or "Yesterday"
src/gnome-builder.mk | 1 +
src/util/gb-glib.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++
src/util/gb-glib.h | 2 +
3 files changed, 73 insertions(+), 0 deletions(-)
---
diff --git a/src/gnome-builder.mk b/src/gnome-builder.mk
index a298b28..e188338 100644
--- a/src/gnome-builder.mk
+++ b/src/gnome-builder.mk
@@ -110,6 +110,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/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]