diff --git a/src/utils/relative_date.py b/src/utils/relative_date.py new file mode 100644 index 0000000..6ecde67 --- /dev/null +++ b/src/utils/relative_date.py @@ -0,0 +1,44 @@ +# relative_date.py +# +# Copyright 2022-2023 kramo +# +# 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 . +# +# SPDX-License-Identifier: GPL-3.0-or-later + +from datetime import datetime + +from gi.repository import GLib + + +def relative_date(timestamp): # pylint: disable=too-many-return-statements + days_no = ((today := datetime.today()) - datetime.fromtimestamp(timestamp)).days + + if days_no == 0: + return _("Today") + if days_no == 1: + return _("Yesterday") + if days_no <= (day_of_week := today.weekday()): + return GLib.DateTime.new_from_unix_utc(timestamp).format("%A") + if days_no <= day_of_week + 7: + return _("Last Week") + if days_no <= (day_of_month := today.day): + return _("This Month") + if days_no <= day_of_month + 30: + return _("Last Month") + if days_no < (day_of_year := today.timetuple().tm_yday): + return GLib.DateTime.new_from_unix_utc(timestamp).format("%B") + if days_no <= day_of_year + 365: + return _("Last Year") + return GLib.DateTime.new_from_unix_utc(timestamp).format("%Y") diff --git a/src/window.py b/src/window.py index 59205c8..d82b07a 100644 --- a/src/window.py +++ b/src/window.py @@ -17,11 +17,10 @@ # # SPDX-License-Identifier: GPL-3.0-or-later -from datetime import datetime - -from gi.repository import Adw, GLib, Gtk +from gi.repository import Adw, Gtk from src import shared # pylint: disable=no-name-in-module +from src.utils.relative_date import relative_date @Gtk.Template(resource_path=shared.PREFIX + "/gtk/window.ui") @@ -158,19 +157,6 @@ class CartridgesWindow(Adw.ApplicationWindow): def set_active_game(self, _widget, _pspec, game): self.active_game = game - def get_time(self, timestamp): - days_no = (datetime.today() - datetime.fromtimestamp(timestamp)).days - - if days_no == 0: - return _("Today") - if days_no == 1: - return _("Yesterday") - if days_no < 8: - return GLib.DateTime.new_from_unix_utc(timestamp).format("%A") - if days_no < 335: - return GLib.DateTime.new_from_unix_utc(timestamp).format("%B") - return GLib.DateTime.new_from_unix_utc(timestamp).format("%Y") - def show_details_view(self, game): self.active_game = game @@ -200,13 +186,13 @@ class CartridgesWindow(Adw.ApplicationWindow): self.details_view_title.set_label(game.name) self.details_view_header_bar_title.set_title(game.name) - date = self.get_time(game.added) + date = relative_date(game.added) self.details_view_added.set_label( # The variable is the date when the game was added _("Added: {}").format(date) ) last_played_date = ( - self.get_time(game.last_played) if game.last_played else _("Never") + relative_date(game.last_played) if game.last_played else _("Never") ) self.details_view_last_played.set_label( # The variable is the date when the game was last played