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 adatetime.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: - t – seconds or
datetime.timedelta
object - sign – default
False
- pad – default
''
>>> compress(1) '1s' >>> compress(12) '12s' >>> compress(123) '2m3s' >>> compress(1234) '20m34s' >>> compress(12345) '3h25m45s' >>> compress(123456) '1d10h17m36s'
- t – seconds or
-
natural.date.
day
(t, now=None, format='%B %d')¶ Date delta compared to
t
. You can overridenow
to specify what date to compare to.You can override the date format by supplying a
format
parameter.Parameters: - t – timestamp,
datetime.date
ordatetime.datetime
object - now – default
None
, optionally adatetime.datetime
object - format – default
'%B %d'
>>> 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'
- 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.date
ordatetime.datetime
object - t2 – timestamp,
datetime.date
ordatetime.datetime
object - words – default
True
, allow words like “yesterday”, “tomorrow” and “just now” - justnow – default
datetime.timedelta(seconds=10)
,datetime.timedelta
object representing tolerance for considering a delta as meaning ‘just now’
>>> delta(_to_datetime('2012-06-13T15:24:17'), _to_datetime('2013-12-11T12:34:56')) ('77 weeks', -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 overridenow
to specify what time to compare to.Parameters: - t – timestamp,
datetime.date
ordatetime.datetime
object - now – default
None
, optionally adatetime.datetime
object - precision – default
1
, number of fragments to return - words – default
None
, allow words like “yesterday”, if set toNone
this will be enabled ifprecision
is set to1
- justnow – default
datetime.timedelta(seconds=10)
,datetime.timedelta
object passed todelta()
representing tolerance for considering argumentt
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'
- t – timestamp,