From 7ba6454a8c029462c7f86bb1090fe74ff28d093f Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 16 Jan 2021 09:39:30 +0100 Subject: helper: move pretty_datetime to the only place where it is used Also, drop now-unnecessary call to decode_string --- alot/settings/manager.py | 63 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 3 deletions(-) (limited to 'alot/settings') diff --git a/alot/settings/manager.py b/alot/settings/manager.py index f6da5332..ab5e70fb 100644 --- a/alot/settings/manager.py +++ b/alot/settings/manager.py @@ -1,6 +1,9 @@ # Copyright (C) 2011-2012 Patrick Totzke # This file is released under the GNU GPL, version 3 or a later revision. # For further details see the COPYING file + +from datetime import datetime, timedelta + import importlib.util import itertools import logging @@ -13,7 +16,7 @@ from configobj import ConfigObj, Section from ..account import SendmailAccount from ..addressbook.abook import AbookAddressBook from ..addressbook.external import ExternalAddressbook -from ..helper import pretty_datetime, string_decode, get_xdg_env +from ..helper import string_decode, get_xdg_env from ..utils import configobj as checks from .errors import ConfigError, NoMatchingAccount @@ -26,6 +29,60 @@ DEFAULTSPATH = os.path.join(os.path.dirname(__file__), '..', 'defaults') DATA_DIRS = get_xdg_env('XDG_DATA_DIRS', '/usr/local/share:/usr/share').split(':') +def _pretty_datetime(d): + """ + translates :class:`datetime` `d` to a "sup-style" human readable string. + + >>> now = datetime.now() + >>> now.strftime('%c') + 'Sat 31 Mar 2012 14:47:26 ' + >>> _pretty_datetime(now) + 'just now' + >>> _pretty_datetime(now - timedelta(minutes=1)) + '1min ago' + >>> _pretty_datetime(now - timedelta(hours=5)) + '5h ago' + >>> _pretty_datetime(now - timedelta(hours=12)) + '02:54am' + >>> _pretty_datetime(now - timedelta(days=1)) + 'yest 02pm' + >>> _pretty_datetime(now - timedelta(days=2)) + 'Thu 02pm' + >>> _pretty_datetime(now - timedelta(days=7)) + 'Mar 24' + >>> _pretty_datetime(now - timedelta(days=356)) + 'Apr 2011' + """ + ampm = d.strftime('%p').lower() + if len(ampm): + hourfmt = '%I' + ampm + hourminfmt = '%I:%M' + ampm + else: + hourfmt = '%Hh' + hourminfmt = '%H:%M' + + now = datetime.now() + today = now.date() + if d.date() == today or d > now - timedelta(hours=6): + delta = datetime.now() - d + if delta.seconds < 60: + string = 'just now' + elif delta.seconds < 3600: + string = '%dmin ago' % (delta.seconds // 60) + elif delta.seconds < 6 * 3600: + string = '%dh ago' % (delta.seconds // 3600) + else: + string = d.strftime(hourminfmt) + elif d.date() == today - timedelta(1): + string = d.strftime('yest ' + hourfmt) + elif d.date() > today - timedelta(7): + string = d.strftime('%a ' + hourfmt) + elif d.year != today.year: + string = d.strftime('%b %Y') + else: + string = d.strftime('%b %d') + + return string class SettingsManager: """Organizes user settings""" @@ -523,7 +580,7 @@ class SettingsManager: 1) look if a fixed 'timestamp_format' is given in the config 2) check if a 'timestamp_format' hook is defined - 3) use :func:`~alot.helper.pretty_datetime` as fallback + 3) use :func:`_pretty_datetime` as fallback """ fixed_format = self.get('timestamp_format') @@ -534,5 +591,5 @@ class SettingsManager: if format_hook: rep = string_decode(format_hook(d), 'UTF-8') else: - rep = pretty_datetime(d) + rep = _pretty_datetime(d) return rep -- cgit v1.2.3