2
# Copyright (C) 2008 The University of Melbourne
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
'''Utilities for making nice, human readable dates.'''
23
def get_datetime(datetime_or_seconds):
24
'''Return the given datetime, or convert the given seconds since epoch.'''
25
if type(datetime_or_seconds) is datetime.datetime:
26
return datetime_or_seconds
27
return datetime.datetime.fromtimestamp(datetime_or_seconds)
29
def make_date_nice(datetime_or_seconds):
30
"""Generate a full human-readable representation of a date and time.
32
Given a datetime or number of seconds elapsed since the epoch,
33
generates a string representing the date/time in human-readable form.
34
"ddd mmm dd, yyyy h:m a"
36
dt = get_datetime(datetime_or_seconds)
37
return dt.strftime("%a %b %d %Y, %I:%M %p")
39
def make_date_nice_short(datetime_or_seconds):
40
"""Generate a very compact human-readable representation of a date.
42
Given a datetime or number of seconds elapsed since the epoch,
43
generates a string representing the date in human-readable form.
44
Does not include the time.
47
dt = get_datetime(datetime_or_seconds)
48
now = datetime.datetime.now()
50
# Use a "naturalisation" algorithm.
54
# Dates today or yesterday, return "today" or "yesterday".
60
# Dates in the last 5 days, return "n days ago".
61
return str(delta.days) + " days ago"
62
# Other dates, return a short date format.
63
# If within the same year, omit the year (mmm dd)
64
if dt.year == now.year:
65
return dt.strftime("%b %d")
66
# Else, include the year (mmm dd, yyyy)
68
return dt.strftime("%b %d, %Y")
70
def format_datetime_for_paragraph(datetime_or_seconds):
71
"""Generate a compact representation of a datetime for use in a paragraph.
73
Given a datetime or number of seconds elapsed since the epoch, generates
74
a compact string representing the date and time in human-readable form.
76
Unlike make_date_nice_short, the time will always be included.
78
Also unlike make_date_nice_short, it is suitable for use in the middle of
79
a block of prose and properly handles timestamps in the future nicely.
82
dt = get_datetime(datetime_or_seconds)
83
now = datetime.datetime.now()
87
# If the date is earlier than now, we want to either say something like
88
# '5 days ago' or '25 seconds ago', 'yesterday at 08:54' or
89
# 'on 2009-03-26 at 20:09'.
91
# If the time is within one hour of now, we show it nicely in either
94
if abs(delta).days == 0 and abs(delta).seconds <= 1:
97
if abs(delta).days == 0 and abs(delta).seconds < 60*60:
98
if abs(delta) == delta:
106
# Show the number of minutes unless we are within two minutes.
107
if abs(delta).seconds >= 120:
108
return (prefix + '%d minutes' + suffix) % (abs(delta).seconds / 60)
110
return (prefix + '%d seconds' + suffix) % (abs(delta).seconds)
113
if dt.date() == now.date():
115
return dt.strftime('today at %I:%M %p')
116
elif dt.date() == now.date() - datetime.timedelta(days=1):
118
return dt.strftime('yesterday at %I:%M %p')
120
if dt.date() == now.date():
122
return dt.strftime('today at %I:%M %p')
123
elif dt.date() == now.date() + datetime.timedelta(days=1):
125
return dt.strftime('tomorrow at %I:%M %p')
127
return dt.strftime('on %Y-%m-%d at %I:%M %p')