Make datetime output timezone-aware

This commit is contained in:
RunasSudo 2017-12-13 14:51:22 +10:30
parent 98a8faf486
commit d4eb9ae5cf
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
4 changed files with 9 additions and 3 deletions

View File

@ -39,6 +39,7 @@ if is_python:
import hashlib
import importlib
import json
import pytz
import uuid
__pragma__('noskip')
else:
@ -158,14 +159,14 @@ class DateTimeField(Field):
return None
if is_python:
return datetime.strptime(value, '%Y-%m-%dT%H:%M:%SZ')
return pytz.utc.localize(datetime.strptime(value, '%Y-%m-%dT%H:%M:%SZ'))
else:
return __pragma__('js', '{}', 'new Date(value)')
@staticmethod
def now():
if is_python:
return datetime.utcnow()
return pytz.utc.localize(datetime.utcnow())
else:
return __pragma__('js', '{}', 'new Date()')

View File

@ -36,6 +36,7 @@ import functools
import importlib
import json
import os
import pytz
import subprocess
app = flask.Flask(__name__, static_folder=None)
@ -157,7 +158,8 @@ def inject_globals():
@app.template_filter('pretty_date')
def pretty_date(dt):
return flask.Markup('<time datetime="{}" title="{}">{}</time>'.format(dt.strftime('%Y-%m-%dT%H:%M:%SZ'), dt.strftime('%Y-%m-%d %H:%M:%S UTC'), timeago.format(dt, datetime.now())))
dt_local = dt.astimezone(pytz.timezone(app.config['TIMEZONE']))
return flask.Markup('<time datetime="{}" title="{}">{}</time>'.format(dt_local.strftime('%Y-%m-%dT%H:%M:%S%z'), dt_local.strftime('%Y-%m-%d %H:%M:%S %Z'), timeago.format(dt, DateTimeField.now())))
# Tickle the plumbus every request
@app.before_request

View File

@ -16,6 +16,8 @@ ADMINS = [
TASK_RUN_STRATEGY = 'eos.core.tasks.direct.DirectRunStrategy'
TIMEZONE = 'Australia/Canberra'
# MongoDB
DB_TYPE = 'mongodb'

View File

@ -15,6 +15,7 @@ psutil==5.4.1
psycopg2==2.7.3.2
PyExecJS==1.4.1
pymongo==3.5.1
pytz==2017.3
requests==2.18.4
requests-oauthlib==0.8.0
six==1.10.0