natural.date

natural.date.compress(t, sign=False, pad='')

Convert the input to compressed format, works with a datetime.timedelta object or a number that represents the number of seconds you want to compress. If you supply a timestamp or a datetime.datetime object, it will give the delta relative to the current time.

You can enable showing a sign in front of the compressed format with the sign parameter, the default is not to show signs.

Optionally, you can chose to pad the output. If you wish your values to be separated by spaces, set pad to ' '.

Parameters:
>>> compress(1)
'1s'
>>> compress(12)
'12s'
>>> compress(123)
'2m3s'
>>> compress(1234)
'20m34s'
>>> compress(12345)
'3h25m45s'
>>> compress(123456)
'1d10h17m36s'
natural.date.day(t, now=None, format='%B %d')

Date delta compared to t. You can override now to specify what date to compare to.

You can override the date format by supplying a format parameter.

Parameters:
>>> import time
>>> day(time.time())
'today'
>>> day(time.time() - 86400)
'yesterday'
>>> day(time.time() - 604800)
'last week'
>>> day(time.time() + 86400)
'tomorrow'
>>> day(time.time() + 604800)
'next week'
natural.date.delta(t1, t2, words=True, justnow=datetime.timedelta(0, 10))

Calculates the estimated delta between two time objects in human-readable format. Used internally by the day() and duration() functions.

Parameters:
>>> delta(_to_datetime('2012-06-13T15:24:17'), _to_datetime('2013-12-11T12:34:56'))
('77 weeks', -594639)
natural.date.duration(t, now=None, precision=1, pad=', ', words=None, justnow=datetime.timedelta(0, 10))

Time delta compared to t. You can override now to specify what time to compare to.

Parameters:
  • t – timestamp, datetime.date or datetime.datetime object
  • now – default None, optionally a datetime.datetime object
  • precision – default 1, number of fragments to return
  • words – default None, allow words like “yesterday”, if set to None this will be enabled if precision is set to 1
  • justnow – default datetime.timedelta(seconds=10), datetime.timedelta object passed to delta() representing tolerance for considering argument t as meaning ‘just now’
>>> import time
>>> from datetime import datetime
>>> duration(time.time() + 1)
'just now'
>>> duration(time.time() + 11)
'11 seconds from now'
>>> duration(time.time() - 1)
'just now'
>>> duration(time.time() - 11)
'11 seconds ago'
>>> duration(time.time() - 3601)
'an hour ago'
>>> duration(time.time() - 7201)
'2 hours ago'
>>> duration(time.time() - 1234567)
'2 weeks ago'
>>> duration(time.time() + 7200, precision=1)
'2 hours from now'
>>> duration(time.time() - 1234567, precision=3)
'2 weeks, 6 hours, 56 minutes ago'
>>> duration(datetime(2014, 9, 8), now=datetime(2014, 9, 9))
'yesterday'
>>> duration(datetime(2014, 9, 7, 23), now=datetime(2014, 9, 9))
'1 day ago'
>>> duration(datetime(2014, 9, 10), now=datetime(2014, 9, 9))
'tomorrow'
>>> duration(datetime(2014, 9, 11, 1), now=datetime(2014, 9, 9, 23))
'1 day from now'