Add exact timestamp to Reddit submission data dictionary
This will enable easy implementation of the exact timestamp display as requested in #3
This commit is contained in:
@@ -142,6 +142,27 @@ def test_content_humanize_timestamp():
|
|||||||
assert Content.humanize_timestamp(timestamp, True) == '2 years ago'
|
assert Content.humanize_timestamp(timestamp, True) == '2 years ago'
|
||||||
|
|
||||||
|
|
||||||
|
def test_content_exact_timestamp():
|
||||||
|
# Less than a year ago.
|
||||||
|
timestamp = time.time() - 60 * 60
|
||||||
|
|
||||||
|
# Calling datetime.strftime("%m-%d %H:%M") should always give a string of
|
||||||
|
# length 11
|
||||||
|
assert len(Content.exact_timestamp(timestamp)) == 11
|
||||||
|
|
||||||
|
timestamp = time.time() - 60 * 60 * 24 * 31 * 12 * 2
|
||||||
|
|
||||||
|
# Similarly, calling datetime.strftime("%Y-%m-%d") should always give a
|
||||||
|
# string of length 10
|
||||||
|
assert len(Content.exact_timestamp(timestamp)) == 10
|
||||||
|
|
||||||
|
# Does it break on a leap day?
|
||||||
|
from datetime import datetime
|
||||||
|
leapday = (datetime(2016, 2, 29) - datetime(1970, 1, 1)).total_seconds()
|
||||||
|
|
||||||
|
Content.exact_timestamp(leapday)
|
||||||
|
|
||||||
|
|
||||||
def test_content_wrap_text():
|
def test_content_wrap_text():
|
||||||
|
|
||||||
text = 'four score\nand seven\n\n'
|
text = 'four score\nand seven\n\n'
|
||||||
|
|||||||
@@ -233,6 +233,7 @@ class Content(object):
|
|||||||
data['html'] = sub.selftext_html or ''
|
data['html'] = sub.selftext_html or ''
|
||||||
data['created'] = cls.humanize_timestamp(sub.created_utc)
|
data['created'] = cls.humanize_timestamp(sub.created_utc)
|
||||||
data['created_long'] = cls.humanize_timestamp(sub.created_utc, True)
|
data['created_long'] = cls.humanize_timestamp(sub.created_utc, True)
|
||||||
|
data['created_exact'] = cls.exact_timestamp(sub.created_utc)
|
||||||
data['comments'] = sub.num_comments
|
data['comments'] = sub.num_comments
|
||||||
data['score'] = '{0}'.format('-' if sub.hide_score else sub.score)
|
data['score'] = '{0}'.format('-' if sub.hide_score else sub.score)
|
||||||
data['author'] = name
|
data['author'] = name
|
||||||
@@ -253,9 +254,12 @@ class Content(object):
|
|||||||
cls.humanize_timestamp(sub.edited))
|
cls.humanize_timestamp(sub.edited))
|
||||||
data['edited_long'] = '(edit {})'.format(
|
data['edited_long'] = '(edit {})'.format(
|
||||||
cls.humanize_timestamp(sub.edited, True))
|
cls.humanize_timestamp(sub.edited, True))
|
||||||
|
data['edited_exact'] = '(edit {})'.format(
|
||||||
|
cls.exact_timestamp(sub.edited))
|
||||||
else:
|
else:
|
||||||
data['edited'] = ''
|
data['edited'] = ''
|
||||||
data['edited_long'] = ''
|
data['edited_long'] = ''
|
||||||
|
data['edited_exact'] = ''
|
||||||
|
|
||||||
if sub.url.split('/r/')[-1] == sub.permalink.split('/r/')[-1]:
|
if sub.url.split('/r/')[-1] == sub.permalink.split('/r/')[-1]:
|
||||||
data['url'] = 'self.{0}'.format(data['subreddit'])
|
data['url'] = 'self.{0}'.format(data['subreddit'])
|
||||||
@@ -398,6 +402,18 @@ class Content(object):
|
|||||||
else:
|
else:
|
||||||
return '%dyr' % years
|
return '%dyr' % years
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def exact_timestamp(utc_timestamp):
|
||||||
|
dt = datetime.fromtimestamp(utc_timestamp)
|
||||||
|
|
||||||
|
# https://stackoverflow.com/a/765990
|
||||||
|
yearsago = ((datetime.now() - dt).days / 365.2425)
|
||||||
|
|
||||||
|
if yearsago > 0:
|
||||||
|
return dt.strftime("%Y-%m-%d")
|
||||||
|
else:
|
||||||
|
return dt.strftime("%m-%d %H:%M")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def wrap_text(text, width):
|
def wrap_text(text, width):
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user