Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include timezone in feed_dates #769

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove the comment for the PR reference.

Also, the "Z" I was suggesting earlier was to use a literal Z instead of %Z.

I don't think the parse import is needed, is it? I don't see it being used.

The current build failure is coming from an invalid import of timezone, which doesn't exist for Python 2.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to remove the parse and timezone imports. I'd simplify to:

datetime_format = "%a, %d %b %Y %H:%M:%S"
# If time argument has no timezone information, assume UTC ("Z")
if time.tzinfo == None or time.tzinfo.utcoffset(time) == None:
    format = datetime_format + " Z"
else:
    format = datetime_format + " %Z"
return time.strftime(format)


def normalize_string(token, timestamp, nonce, request_method, host, port, path, query_array):
normalized_string = "%s\n" % (token)
normalized_string += "%s\n" % (timestamp)
Expand Down
4 changes: 2 additions & 2 deletions models/bookmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions models/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down
4 changes: 2 additions & 2 deletions models/shake.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions models/sharedfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, \
Expand Down