natural.date¶
-
natural.date.compress(t, sign=False, pad='')¶ Convert the input to compressed format, works with a
datetime.timedeltaobject or a number that represents the number of seconds you want to compress. If you supply a timestamp or adatetime.datetimeobject, it will give the delta relative to the current time.You can enable showing a sign in front of the compressed format with the
signparameter, 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
padto' '.Parameters: - t – seconds or
datetime.timedeltaobject - sign – default
False - pad – default
''
>>> print(compress(1)) 1s >>> print(compress(12)) 12s >>> print(compress(123)) 2m3s >>> print(compress(1234)) 20m34s >>> print(compress(12345)) 3h25m45s >>> print(compress(123456)) 1d10h17m36s
- t – seconds or
-
natural.date.day(t, now=None, format='%B %d')¶ Date delta compared to
t. You can overridenowto specify what date to compare to.You can override the date format by supplying a
formatparameter.Parameters: - t – timestamp,
datetime.dateordatetime.datetimeobject - now – default
None, optionally adatetime.datetimeobject - format – default
'%B %d'
>>> import time >>> print(day(time.time())) today >>> print(day(time.time() - 86400)) yesterday >>> print(day(time.time() - 604800)) last week >>> print(day(time.time() + 86400)) tomorrow >>> print(day(time.time() + 604800)) next week
- t – timestamp,
-
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()andduration()functions.Parameters: - t1 – timestamp,
datetime.dateordatetime.datetimeobject - t2 – timestamp,
datetime.dateordatetime.datetimeobject - words – default
True, allow words like “yesterday”, “tomorrow” and “just now” - justnow – default
datetime.timedelta(seconds=10),datetime.timedeltaobject representing tolerance for considering a delta as meaning ‘just now’
>>> (x,y) = delta(_to_datetime('2012-06-13T15:24:17'), _to_datetime('2013-12-11T12:34:56')) >>> print(x) 77 weeks >>> int(y) -594639
- t1 – timestamp,
-
natural.date.duration(t, now=None, precision=1, pad=', ', words=None, justnow=datetime.timedelta(0, 10))¶ Time delta compared to
t. You can overridenowto specify what time to compare to.Parameters: - t – timestamp,
datetime.dateordatetime.datetimeobject - now – default
None, optionally adatetime.datetimeobject - precision – default
1, number of fragments to return - words – default
None, allow words like “yesterday”, if set toNonethis will be enabled ifprecisionis set to1 - justnow – default
datetime.timedelta(seconds=10),datetime.timedeltaobject passed todelta()representing tolerance for considering argumenttas meaning ‘just now’
>>> import time >>> from datetime import datetime >>> print(duration(time.time() + 1)) just now >>> print(duration(time.time() + 11)) 11 seconds from now >>> print(duration(time.time() - 1)) just now >>> print(duration(time.time() - 11)) 11 seconds ago >>> print(duration(time.time() - 3601)) an hour ago >>> print(duration(time.time() - 7201)) 2 hours ago >>> print(duration(time.time() - 1234567)) 2 weeks ago >>> print(duration(time.time() + 7200, precision=1)) 2 hours from now >>> print(duration(time.time() - 1234567, precision=3)) 2 weeks, 6 hours, 56 minutes ago >>> print(duration(datetime(2014, 9, 8), now=datetime(2014, 9, 9))) yesterday >>> print(duration(datetime(2014, 9, 7, 23), now=datetime(2014, 9, 9))) 1 day ago >>> print(duration(datetime(2014, 9, 10), now=datetime(2014, 9, 9))) tomorrow >>> print(duration(datetime(2014, 9, 11, 1), now=datetime(2014, 9, 9, 23))) 1 day from now
- t – timestamp,