Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_naturalday(test_args, expected):
assert humanize.naturalday(*test_args) == expected
def human_date(date):
def style_day(n):
n = int(n)
return str(n)+("th" if 4<=n%100<=20 else {1:"st",2:"nd",3:"rd"}.get(n%10, "th"))
# apply humanization fx
h = humanize.naturalday(datetime.datetime.strptime(date, "%Y-%m-%d")).title()
# remove zeros
m0 = re.search(r"([A-Za-z]+) 0([0-9])", h)
if m0: h = "%s %s" % ( m0.group(1), m0.group(2) )
# style day
m_day = re.search(r"([A-Za-z]+) (\d+)", h)
if m_day: h = "%s %s" % ( m_day.group(1), style_day(m_day.group(2)) )
# lowercase yesterday and today
if h in ['Yesterday', 'Today']: h = h.lower()
return h
def humanize_date(time):
return humanize.naturalday(time)
def print_articles(articles):
for i, article in zip(range(len(articles)), articles):
print "### %i) %s\n\n%s\n%s - [Read more](%s)\n----\n" % (
i+1,
article.title,
html2text(article.content,
article.link),
humanize.naturalday(
datetime.datetime.now() - \
datetime.timedelta(
seconds=time.time() - article.date)),
article.link
)
# -*- coding: utf-8 -*-
__author__ = 'ipetrash'
# SOURCE: https://github.com/jmoiron/humanize
# pip install humanize
import humanize
# Date & time humanization:
import datetime as DT
print(humanize.naturalday(DT.datetime.now())) # 'today'
print(humanize.naturalday(DT.datetime.now() - DT.timedelta(days=1))) # 'yesterday'
print(humanize.naturalday(DT.date(2007, 6, 5))) # 'Jun 05'
print()
print(DT.timedelta(seconds=1001)) # '0:16:41'
print(humanize.naturaldelta(DT.timedelta(seconds=1001))) # '16 minutes'
print(humanize.naturaldelta(DT.timedelta(seconds=5))) # '5 seconds'
print(humanize.naturaldelta(DT.timedelta(hours=30))) # 'a day'
print(humanize.naturaldelta(DT.timedelta(hours=60))) # '2 days'
print()
print(humanize.naturaldate(DT.date(2007, 6, 5))) # 'Jun 05 2007'
print(humanize.naturaldate(DT.date(2007, 6, 5))) # 'Jun 05 2007'
print()
print(humanize.naturaltime(DT.datetime.now() - DT.timedelta(seconds=1))) # 'a second ago'
print(humanize.naturaltime(DT.datetime.now() - DT.timedelta(seconds=3600))) # 'an hour ago'
def convert_date(date_to_convert):
"""Convert date to human readable format: Month Day Year
If date is less than a day return: today or yesterday
Format of date to convert: 2019-01-12T16:48:41.821037+00:00
Output: Jan 12 2019
:param date_to_convert: Date to convert
:returns: Readable date
"""
date_parsed = parser.parse(date_to_convert).replace(tzinfo=None)
delta = datetime.datetime.now() - datetime.timedelta(days=1)
if delta < date_parsed:
return humanize.naturalday(date_parsed).title()
else:
return date_parsed.strftime("%-d %B %Y")
__author__ = 'ipetrash'
# SOURCE: https://github.com/jmoiron/humanize
# pip install humanize
import humanize
# Date & time humanization:
import datetime as DT
print(humanize.naturalday(DT.datetime.now())) # 'today'
print(humanize.naturalday(DT.datetime.now() - DT.timedelta(days=1))) # 'yesterday'
print(humanize.naturalday(DT.date(2007, 6, 5))) # 'Jun 05'
print()
print(DT.timedelta(seconds=1001)) # '0:16:41'
print(humanize.naturaldelta(DT.timedelta(seconds=1001))) # '16 minutes'
print(humanize.naturaldelta(DT.timedelta(seconds=5))) # '5 seconds'
print(humanize.naturaldelta(DT.timedelta(hours=30))) # 'a day'
print(humanize.naturaldelta(DT.timedelta(hours=60))) # '2 days'
print()
print(humanize.naturaldate(DT.date(2007, 6, 5))) # 'Jun 05 2007'
print(humanize.naturaldate(DT.date(2007, 6, 5))) # 'Jun 05 2007'
print()
print(humanize.naturaltime(DT.datetime.now() - DT.timedelta(seconds=1))) # 'a second ago'
print(humanize.naturaltime(DT.datetime.now() - DT.timedelta(seconds=3600))) # 'an hour ago'
print(humanize.naturaltime(DT.datetime.now() - DT.timedelta(hours=30))) # 'a day ago'