summaryrefslogtreecommitdiff
path: root/alot/settings
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2021-01-16 09:39:30 +0100
committerAnton Khirnov <anton@khirnov.net>2021-01-16 09:39:44 +0100
commit7ba6454a8c029462c7f86bb1090fe74ff28d093f (patch)
treef5338aaa3e3df7a4dc9cb048e1c93800e0c5ae14 /alot/settings
parent55171ecf0bd62c3b83aea85ac3c7e1d4967ebda4 (diff)
helper: move pretty_datetime to the only place where it is used
Also, drop now-unnecessary call to decode_string
Diffstat (limited to 'alot/settings')
-rw-r--r--alot/settings/manager.py63
1 files changed, 60 insertions, 3 deletions
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 <patricktotzke@gmail.com>
# 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