From 92257ffde25e6a03f1fbf866db6996df1179e894 Mon Sep 17 00:00:00 2001 From: Mohammed Sadiq Date: Wed, 2 Jan 2019 15:06:12 +0530 Subject: [PATCH 1/2] Add bjb-utils --- src/bjb-utils.c | 82 +++++++++++++++++++++++++++++++++++++++++ src/bjb-utils.h | 32 ++++++++++++++++ src/libbiji/meson.build | 3 +- 3 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 src/bjb-utils.c create mode 100644 src/bjb-utils.h diff --git a/src/bjb-utils.c b/src/bjb-utils.c new file mode 100644 index 00000000..944d4cfd --- /dev/null +++ b/src/bjb-utils.c @@ -0,0 +1,82 @@ +/* gn-utils.c + * + * Copyright 2019 Mohammed Sadiq + * + * 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 . + * + * Author(s): + * Mohammed Sadiq + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include + +/** + * bjb_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 * +bjb_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, "%OB"); + + /* Year */ + return g_date_time_format (local_time, "%Y"); +} diff --git a/src/bjb-utils.h b/src/bjb-utils.h new file mode 100644 index 00000000..9c738148 --- /dev/null +++ b/src/bjb-utils.h @@ -0,0 +1,32 @@ +/* gn-utils.h + * + * Copyright 2019 Mohammed Sadiq + * + * 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 . + * + * Author(s): + * Mohammed Sadiq + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#pragma once + +#include + +G_BEGIN_DECLS + +gchar *bjb_utils_get_human_time (gint64 unix_time); + +G_END_DECLS diff --git a/src/libbiji/meson.build b/src/libbiji/meson.build index 12f2fdf9..84c6406f 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', + '../bjb-utils.c', ) marshalers = 'biji-marshalers' -- GitLab From f7cc2aac8a50088492f7a72c817f87ef7c374595 Mon Sep 17 00:00:00 2001 From: Mohammed Sadiq Date: Wed, 2 Jan 2019 15:08:10 +0530 Subject: [PATCH 2/2] libbiji: Use the new API to get human time ... which has better time resolution like showing time, which is included in the latest design. --- src/bjb-main-toolbar.c | 5 +++-- src/bjb-main-view.c | 5 +++-- src/libbiji/biji-note-obj.c | 8 ++++---- src/libbiji/biji-note-obj.h | 2 +- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/bjb-main-toolbar.c b/src/bjb-main-toolbar.c index 7836a812..dd39c975 100644 --- a/src/bjb-main-toolbar.c +++ b/src/bjb-main-toolbar.c @@ -420,14 +420,15 @@ on_last_updated_cb (BijiItem *note, BjbMainToolbar *self) { g_autofree gchar *label; + g_autofree gchar *time_str = NULL; + time_str = biji_note_obj_get_last_change_date_string (self->note); /* Translators: %s is the note last recency description. * Last updated is placed as in left to right language * right to left languages might move %s * '%s Last Updated' */ - label = g_strdup_printf (_("Last updated %s"), - biji_note_obj_get_last_change_date_string (self->note)); + label = g_strdup_printf (_("Last updated: %s"), time_str); gtk_label_set_text (GTK_LABEL (self->last_update_item), label); } diff --git a/src/bjb-main-view.c b/src/bjb-main-view.c index 5513393c..02a2e95a 100644 --- a/src/bjb-main-view.c +++ b/src/bjb-main-view.c @@ -24,6 +24,7 @@ #include "bjb-controller.h" #include "bjb-load-more-button.h" #include "bjb-main-toolbar.h" +#include "bjb-utils.h" #include "bjb-main-view.h" #include "bjb-note-view.h" #include "bjb-organize-dialog.h" @@ -502,7 +503,7 @@ render_date (GtkTreeViewColumn *tree_column, gpointer data) { BijiItem *item; - const gchar *str; + g_autofree gchar *str = NULL; BjbMainView *self; self = data; @@ -510,7 +511,7 @@ render_date (GtkTreeViewColumn *tree_column, if (item != NULL) { - str = biji_get_time_diff_with_time (biji_item_get_mtime (item)); + str = bjb_utils_get_human_time (biji_item_get_mtime (item)); g_object_set (cell, "text", str, NULL); } } diff --git a/src/libbiji/biji-note-obj.c b/src/libbiji/biji-note-obj.c index b84e368a..90990771 100644 --- a/src/libbiji/biji-note-obj.c +++ b/src/libbiji/biji-note-obj.c @@ -18,6 +18,7 @@ #include "biji-date-time.h" #include "biji-note-id.h" #include "biji-manager.h" +#include "../bjb-utils.h" #include "biji-note-obj.h" #include "biji-timeout.h" #include "biji-tracker.h" @@ -357,17 +358,16 @@ biji_note_obj_get_mtime (BijiItem *note) return biji_note_id_get_mtime (priv->id); } -const gchar * +gchar * biji_note_obj_get_last_change_date_string (BijiNoteObj *self) { BijiNoteObjPrivate *priv; - g_return_val_if_fail (BIJI_IS_NOTE_OBJ (self), ""); + g_return_val_if_fail (BIJI_IS_NOTE_OBJ (self), g_strdup ("")); priv = biji_note_obj_get_instance_private (self); - return biji_get_time_diff_with_time ( - biji_note_id_get_mtime (priv->id)); + return bjb_utils_get_human_time (biji_note_id_get_mtime (priv->id)); } diff --git a/src/libbiji/biji-note-obj.h b/src/libbiji/biji-note-obj.h index 77c995a8..e1b9a01b 100644 --- a/src/libbiji/biji-note-obj.h +++ b/src/libbiji/biji-note-obj.h @@ -97,7 +97,7 @@ gboolean biji_note_obj_set_mtime (BijiNoteObj* n, gint64 time); -const gchar *biji_note_obj_get_last_change_date_string (BijiNoteObj *self); +gchar *biji_note_obj_get_last_change_date_string (BijiNoteObj *self); gint64 biji_note_obj_get_last_metadata_change_date (BijiNoteObj *note); -- GitLab