Skip to content
This repository has been archived by the owner on Feb 13, 2021. It is now read-only.

Commit

Permalink
#11 corrigido erro
Browse files Browse the repository at this point in the history
  • Loading branch information
staticdev committed Jan 11, 2019
1 parent de87889 commit f50fb05
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions humanizer_portugues/time.py
Expand Up @@ -7,9 +7,11 @@
import time
from datetime import date, datetime, time, timedelta

__all__ = ['naturalclock', 'naturaldelta', 'naturaltime', 'naturalday', 'naturaldate', 'naturalyear']
__all__ = ['naturalclock', 'naturaldelta', 'naturaltime',
'naturalday', 'naturaldate', 'naturalyear']

MONTHS = {1: "janeiro",
MONTHS = {
1: "janeiro",
2: "fevereiro",
3: "março",
4: "abril",
Expand All @@ -23,7 +25,8 @@
12: "dezembro"
}

HOURS = {0: "zero",
HOURS = {
0: "zero",
1: "uma",
2: "duas",
3: "três",
Expand Down Expand Up @@ -111,9 +114,11 @@
59: "cinquenta e nove"
}


def _now():
return datetime.now()


def naturalclock(value, formal=True):
try:
value = time(value.hour, value.minute, value.second)
Expand All @@ -131,7 +136,7 @@ def naturalclock(value, formal=True):
elif usedHour > 18 and usedHour <= 23:
periodo = 'noite'
# Formal time
if formal == True:
if formal:
clock = HOURS[usedHour]
if usedHour in [0, 1]:
clock += ' hora'
Expand All @@ -149,7 +154,7 @@ def naturalclock(value, formal=True):
usedHour -= 12
clock = HOURS[usedHour]

if usedHour == 0:
if usedHour == 0:
clock = 'meia noite'
elif usedHour == 12:
clock = 'meio dia'
Expand All @@ -162,9 +167,11 @@ def naturalclock(value, formal=True):
try:
periodo
clock += ' da ' + periodo
except: pass
except:
pass
return str(clock)


def abs_timedelta(delta):
"""Returns an "absolute" value for a timedelta, always representing a
time distance."""
Expand All @@ -173,6 +180,7 @@ def abs_timedelta(delta):
return now - (now + delta)
return delta


def date_and_delta(value):
"""Turn a value into a date and a timedelta which represents how long ago
it was. If that's not possible, return (None, value)."""
Expand All @@ -192,6 +200,7 @@ def date_and_delta(value):
return (None, value)
return date, abs_timedelta(delta)


def naturaldelta(value, months=True):
"""Given a timedelta or a number of seconds, return a natural
representation of the amount of time elapsed. This is similar to
Expand Down Expand Up @@ -243,7 +252,10 @@ def naturaldelta(value, months=True):
if not months and not days:
return "um ano"
elif not months:
return "1 ano e %d dias" % days
if days == 1:
return "1 ano e 1 dia"
else:
return "1 ano e %d dias" % days
elif use_months:
if months == 1:
return "1 ano e 1 mês"
Expand Down Expand Up @@ -278,6 +290,7 @@ def naturaltime(value, future=False, months=True):

return ago % delta


def naturalday(value, hasYear=False):
"""For date values that are tomorrow, today or yesterday compared to
present day returns representing string. Otherwise, returns a string
Expand All @@ -300,6 +313,7 @@ def naturalday(value, hasYear=False):
natday += ' de {0}'.format(value.year)
return natday


def naturalyear(value):
"""For date values that are last year, this year or next year compared to
present year returns representing string. Otherwise, returns a string
Expand All @@ -318,6 +332,7 @@ def naturalyear(value):
return 'ano passado'
return str(value.year)


def naturaldate(value):
"""Like naturalday, but will append a year for dates that are a year
ago or more."""
Expand Down

0 comments on commit f50fb05

Please sign in to comment.