Merge pull request #184 from YunoHost/enh-human-readable-date

[enh] Display date as system timezone
This commit is contained in:
Alexandre Aubin 2018-12-12 13:12:58 +01:00 committed by GitHub
commit 6411e5a1bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 2 deletions

3
debian/control vendored
View file

@ -16,7 +16,8 @@ Depends: ${misc:Depends}, ${python:Depends},
python-gnupg, python-gnupg,
python-gevent-websocket, python-gevent-websocket,
python-argcomplete, python-argcomplete,
python-psutil python-psutil,
python-tz
Replaces: yunohost-cli Replaces: yunohost-cli
Breaks: yunohost-cli Breaks: yunohost-cli
Description: prototype interfaces with ease in Python Description: prototype interfaces with ease in Python

View file

@ -8,6 +8,9 @@ import locale
import logging import logging
from argparse import SUPPRESS from argparse import SUPPRESS
from collections import OrderedDict from collections import OrderedDict
import time
import pytz
from datetime import date, datetime
import argcomplete import argcomplete
@ -94,6 +97,32 @@ def plain_print_dict(d, depth=0):
print(d) print(d)
def pretty_date(_date):
"""Display a date in the current time zone without ms and tzinfo
Argument:
- date -- The date or datetime to display
"""
# Deduce system timezone
nowutc = datetime.now(tz=pytz.utc)
nowtz = datetime.now()
nowtz = nowtz.replace(tzinfo=pytz.utc)
offsetHour = nowutc - nowtz
offsetHour = int(round(offsetHour.total_seconds() / 3600))
localtz = 'Etc/GMT%+d' % offsetHour
# Transform naive date into UTC date
if _date.tzinfo is None:
_date = _date.replace(tzinfo=pytz.utc)
# Convert UTC date into system locale date
_date = _date.astimezone(pytz.timezone(localtz))
if isinstance(_date, datetime):
return _date.strftime("%Y-%m-%d %H:%M:%S")
else:
return _date.strftime("%Y-%m-%d")
def pretty_print_dict(d, depth=0): def pretty_print_dict(d, depth=0):
"""Print in a pretty way a dictionary recursively """Print in a pretty way a dictionary recursively
@ -127,10 +156,14 @@ def pretty_print_dict(d, depth=0):
else: else:
if isinstance(value, unicode): if isinstance(value, unicode):
value = value.encode('utf-8') value = value.encode('utf-8')
elif isinstance(v, date):
v = pretty_date(v)
print("{:s}- {}".format(" " * (depth + 1), value)) print("{:s}- {}".format(" " * (depth + 1), value))
else: else:
if isinstance(v, unicode): if isinstance(v, unicode):
v = v.encode('utf-8') v = v.encode('utf-8')
elif isinstance(v, date):
v = pretty_date(v)
print("{:s}{}: {}".format(" " * depth, k, v)) print("{:s}{}: {}".format(" " * depth, k, v))

View file

@ -1,6 +1,7 @@
import logging import logging
from json.encoder import JSONEncoder from json.encoder import JSONEncoder
import datetime import datetime
import pytz
logger = logging.getLogger('moulinette.utils.serialize') logger = logging.getLogger('moulinette.utils.serialize')
@ -26,7 +27,9 @@ class JSONExtendedEncoder(JSONEncoder):
return list(o) return list(o)
# Display the date in its iso format ISO-8601 Internet Profile (RFC 3339) # Display the date in its iso format ISO-8601 Internet Profile (RFC 3339)
if isinstance(o, datetime.datetime) or isinstance(o, datetime.date): if isinstance(o, datetime.date):
if o.tzinfo is None:
o = o.replace(tzinfo=pytz.utc)
return o.isoformat() return o.isoformat()
# Return the repr for object that json can't encode # Return the repr for object that json can't encode