hustcc / timeago

:hourglass: Simple library used to format datetime with `*** time ago` statement. eg: "3 hours ago".
http://timeago.org
MIT License
224 stars 37 forks source link

Example code does not work! #2

Closed Gunni closed 8 years ago

Gunni commented 8 years ago
○ python time.py
Traceback (most recent call last):
  File "time.py", line 6, in <module>
    print (timeago.format(date, now, locale='zh_CN')) # will print <removed because encoding>
NameError: name 'date' is not defined

.

○ python time.py
Traceback (most recent call last):
  File "time.py", line 9, in <module>
    print (timeago.format(date, now)) # will print 3 minutes ago
NameError: name 'date' is not defined

.

○ python time.py
Traceback (most recent call last):
  File "time.py", line 12, in <module>
    print (timeago.format(datetime.timedelta(seconds = 60 * 3.4))) # will print 3 minutes ago
  File "/usr/lib/python2.6/site-packages/timeago/__init__.py", line 25, in format
    diff_seconds = date.total_seconds()
AttributeError: 'datetime.timedelta' object has no attribute 'total_seconds'

.

○ python time.py
Traceback (most recent call last):
  File "time.py", line 15, in <module>
    print (timeago.format(datetime.date(2016, 5, 27), now))
  File "/usr/lib/python2.6/site-packages/timeago/__init__.py", line 33, in format
    raise ParameterUnvalid('the parameter `date` should be datetime / timedelta, or datetime formated string.')
timeago.excepts.ParameterUnvalid: 'the parameter `date` should be datetime / timedelta, or datetime formated string.'

.

○ python time.py
Traceback (most recent call last):
  File "time.py", line 18, in <module>
    print (timeago.format(datetime.date(2016, 5, 27), now))
  File "/usr/lib/python2.6/site-packages/timeago/__init__.py", line 33, in format
    raise ParameterUnvalid('the parameter `date` should be datetime / timedelta, or datetime formated string.')
timeago.excepts.ParameterUnvalid: 'the parameter `date` should be datetime / timedelta, or datetime formated string.'

.

○ python time.py
Traceback (most recent call last):
  File "time.py", line 21, in <module>
    print (timeago.format('2016-05-27 12:12:12', '2016-05-27 12:12:03')) # will print just now
  File "/usr/lib/python2.6/site-packages/timeago/__init__.py", line 37, in format
    diff_seconds = (now - date).total_seconds()
AttributeError: 'datetime.timedelta' object has no attribute 'total_seconds'

.

○ python --version
Python 2.6.6
hustcc commented 8 years ago

I update the demo code in README, add the var date, and set the file encoding with utf-8. and:

  1. fixed timedelta.total_seconds in py2.6
  2. fixed bug of string parse with format 2016/7/11

very thanks for you issue, for javascript version timeago, can see here.