5
updated: March 26, 2011
7
Functions for working with timestamps.
12
from datetime import datetime
14
from Translations import _, ngettext
17
"""Returns a datetime objects with the current timestamp."""
20
def locale_date(timestamp):
21
"""Returns a string with the date in the current locales format."""
22
return timestamp.strftime(locale.nl_langinfo(locale.D_FMT))
24
def locale_datetime(timestamp):
25
"""Returns a string with the date and time in the current locales format."""
26
return timestamp.strftime(locale.nl_langinfo(locale.D_T_FMT))
28
def locale_time(timestamp):
29
"""Returns a string with the time in the current locales format."""
30
return timestamp.strftime(locale.nl_langinfo(locale.T_FMT))
32
def time_ago_in_words(timestamp):
33
"""Returns a string in a format such as "less than a minute ago" or "2 hours ago"
34
the distance in time from timestamp to now. If the difference is more than
35
a day, returns locale_datetime instead."""
36
delta = now() - timestamp
45
return _("less than a minute ago")
48
return ngettext("1 minute ago", "{0} minutes ago", m).format(m)
51
return ngettext("1 hour ago", "{0} hours ago", h).format(h)
55
return locale_datetime(timestamp)