diff --git a/lib/utilities.py b/lib/utilities.py index 3c74a08..902b81b 100644 --- a/lib/utilities.py +++ b/lib/utilities.py @@ -5,7 +5,8 @@ import json import base64 import urllib -from datetime import datetime +from datetime import datetime, timezone +from dateutil.parser import parse import urllib from PIL import Image @@ -174,6 +175,13 @@ def pretty_date(time=False): return "%s %s ago" % (str(diff), diff_string) +# See: https://github.com/MLTSHP/mltshp/pull/769 +def rfc822_date(time): + # Make sure the datetime object is timezone aware + if time.tzinfo == None or time.tzinfo.utcoffset(time) == None: + time.replace(tzinfo=timezone.utc) + return time.strftime("%a, %d %b %Y %H:%M:%S %Z") + def normalize_string(token, timestamp, nonce, request_method, host, port, path, query_array): normalized_string = "%s\n" % (token) normalized_string += "%s\n" % (timestamp) diff --git a/models/bookmark.py b/models/bookmark.py index 74c489d..ef45182 100644 --- a/models/bookmark.py +++ b/models/bookmark.py @@ -2,7 +2,7 @@ from lib.flyingcow import Model, Property from lib.flyingcow.db import IntegrityError -from lib.utilities import pretty_date, base36encode +from lib.utilities import pretty_date, base36encode, rfc822_date from tornado.options import options @@ -47,7 +47,7 @@ def feed_date(self): Returns a date formatted to be included in feeds e.g., Tue, 12 Apr 2005 13:59:56 EST """ - return self.created_at.strftime("%a, %d %b %Y %H:%M:%S %Z") + return rfc822_date(self.created_at) def sharedfile_key(self): return base36encode(self.sharedfile_id) diff --git a/models/comment.py b/models/comment.py index ec2b150..0af4d0d 100644 --- a/models/comment.py +++ b/models/comment.py @@ -4,7 +4,7 @@ from tornado import escape from tornado.options import options from lib.flyingcow import Model, Property -from lib.utilities import pretty_date +from lib.utilities import pretty_date, rfc822_date from BeautifulSoup import BeautifulSoup import user @@ -99,7 +99,7 @@ def feed_date(self): Returns a date formatted to be included in feeds e.g., Tue, 12 Apr 2005 13:59:56 EST """ - return self.created_at.strftime("%a, %d %b %Y %H:%M:%S %Z") + return rfc822_date(self.created_at) def body_formatted(self): """ diff --git a/models/shake.py b/models/shake.py index 8f96b40..49fcf28 100644 --- a/models/shake.py +++ b/models/shake.py @@ -11,7 +11,7 @@ from lib.flyingcow import Model, Property from lib.flyingcow.cache import ModelQueryCache from lib.reservedshakenames import reserved_names -from lib.utilities import transform_to_square_thumbnail, s3_url +from lib.utilities import transform_to_square_thumbnail, s3_url, rfc822_date import user, shake, shakesharedfile, sharedfile, subscription, shakemanager @@ -361,7 +361,7 @@ def feed_date(self): Returns a date formatted to be included in feeds e.g., Tue, 12 Apr 2005 13:59:56 EST """ - return self.created_at and self.created_at.strftime("%a, %d %b %Y %H:%M:%S %Z") + return self.created_at and rfc822_date(self.created_at) @classmethod def featured_shakes(self, limit=3): diff --git a/models/sharedfile.py b/models/sharedfile.py index 56b3dec..113bfc3 100644 --- a/models/sharedfile.py +++ b/models/sharedfile.py @@ -9,7 +9,7 @@ from lib.flyingcow import Model, Property from lib.flyingcow.cache import ModelQueryCache from lib.flyingcow.db import IntegrityError -from lib.utilities import base36encode, base36decode, pretty_date, s3_authenticated_url +from lib.utilities import base36encode, base36decode, pretty_date, s3_authenticated_url, rfc822_date import user import sourcefile @@ -593,7 +593,7 @@ def feed_date(self): Returns a date formatted to be included in feeds e.g., Tue, 12 Apr 2005 13:59:56 EST """ - return self.created_at.strftime("%a, %d %b %Y %H:%M:%S %Z") + return rfc822_date(self.created_at) def thumbnail_url(self): return s3_authenticated_url(options.aws_key, options.aws_secret, options.aws_bucket, \