[gnome-notes/wip/sadiq/modernize: 2/3] Add gn-utils
- From: Mohammed Sadiq <pksadiq src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-notes/wip/sadiq/modernize: 2/3] Add gn-utils
- Date: Tue, 8 Jan 2019 04:22:25 +0000 (UTC)
commit b9a8842af6693a199d04a054e280af257e3f2510
Author: Mohammed Sadiq <sadiq sadiqpk org>
Date: Wed Jan 2 15:06:12 2019 +0530
Add gn-utils
src/gn-utils.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++
src/gn-utils.h | 29 +++++++++++++++++
src/libbiji/meson.build | 3 +-
3 files changed, 113 insertions(+), 1 deletion(-)
---
diff --git a/src/gn-utils.c b/src/gn-utils.c
new file mode 100644
index 0000000..c6da641
--- /dev/null
+++ b/src/gn-utils.c
@@ -0,0 +1,82 @@
+/* gn-utils.c
+ *
+ * Copyright 2019 Mohammed Sadiq <sadiq sadiqpk org>
+ *
+ * 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/>.
+ *
+ * Authors:
+ * Mohammed Sadiq <sadiq sadiqpk org>
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#include <glib/gi18n.h>
+
+/**
+ * gn_utils_get_human_time:
+ * @unix_time: seconds since Epoch
+ *
+ * Get a human readable representation of the time
+ * @unix_time.
+ *
+ * The time returned isn’t always in the same format.
+ * Say if @unix_time represents the current day,
+ * a string with time like “07:30” is returned.
+ * Else if @unix_time represents a preceding day from
+ * the same week the weekday name is returned, and so on.
+ *
+ * Returns: A new string. Free with g_free().
+ */
+gchar *
+gn_utils_get_human_time (gint64 unix_time)
+{
+ g_autoptr(GDateTime) now = NULL;
+ g_autoptr(GDateTime) utc_time = NULL;
+ g_autoptr(GDateTime) local_time = NULL;
+ gint year_now, month_now, day_now;
+ gint year, month, day;
+
+ g_return_val_if_fail (unix_time >= 0, g_strdup (_("Unknown")));
+
+ now = g_date_time_new_now_local ();
+ utc_time = g_date_time_new_from_unix_utc (unix_time);
+ local_time = g_date_time_to_local (utc_time);
+
+ g_date_time_get_ymd (now, &year_now, &month_now, &day_now);
+ g_date_time_get_ymd (local_time, &year, &month, &day);
+
+ if (year == year_now &&
+ month == month_now)
+ {
+ /* Time in the format HH:MM */
+ if (day == day_now)
+ return g_date_time_format (local_time, "%R");
+
+ if (day_now - day == 1)
+ return g_strdup (_("Yesterday"));
+
+ /* Localized day name */
+ if (day_now - day <= 7)
+ return g_date_time_format (local_time, "%A");
+
+ return g_strdup (_("This month"));
+ }
+
+ /* Localized month name */
+ if (year == year_now)
+ return g_date_time_format (local_time, "%B");
+
+ /* Year */
+ return g_date_time_format (local_time, "%Y");
+}
diff --git a/src/gn-utils.h b/src/gn-utils.h
new file mode 100644
index 0000000..3e17a81
--- /dev/null
+++ b/src/gn-utils.h
@@ -0,0 +1,29 @@
+/* gn-utils.h
+ *
+ * Copyright 2018 Mohammed Sadiq <sadiq sadiqpk org>
+ *
+ * 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+gchar *gn_utils_get_human_time (gint64 unix_time);
+
+G_END_DECLS
diff --git a/src/libbiji/meson.build b/src/libbiji/meson.build
index 12f2fdf..72aeda1 100644
--- a/src/libbiji/meson.build
+++ b/src/libbiji/meson.build
@@ -23,7 +23,8 @@ sources = files(
'biji-string.c',
'biji-timeout.c',
'biji-tracker.c',
- 'biji-zeitgeist.c'
+ 'biji-zeitgeist.c',
+ '../gn-utils.c',
)
marshalers = 'biji-marshalers'
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]