15
15
# along with this program; if not, write to the Free Software
16
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
'''Utilities for making nice, human readable dates.'''
18
# Module: Date utilities
19
# Author: William Grant
23
def get_datetime(datetime_or_seconds):
24
'''Return the given datetime, or convert the given seconds since epoch.
26
>>> get_datetime(1000000000)
27
datetime.datetime(2001, 9, 9, 11, 46, 40)
28
>>> get_datetime(2000000000)
29
datetime.datetime(2033, 5, 18, 13, 33, 20)
31
>>> get_datetime(datetime.datetime(2009, 5, 26, 11, 38, 53))
32
datetime.datetime(2009, 5, 26, 11, 38, 53)
33
>>> get_datetime(datetime.datetime(2001, 9, 9, 11, 46, 40))
34
datetime.datetime(2001, 9, 9, 11, 46, 40)
36
if type(datetime_or_seconds) is datetime.datetime:
37
return datetime_or_seconds
38
return datetime.datetime.fromtimestamp(datetime_or_seconds)
40
def make_date_nice(datetime_or_seconds):
41
"""Generate a full human-readable representation of a date and time.
43
Given a datetime or number of seconds elapsed since the epoch,
24
seconds_per_day = 86400 # 60 * 60 * 24
26
timezone_offset = time.altzone
28
timezone_offset = time.timezone
30
def make_date_nice(seconds_since_epoch):
31
"""Given a number of seconds elapsed since the epoch,
44
32
generates a string representing the date/time in human-readable form.
45
33
"ddd mmm dd, yyyy h:m a"
47
>>> make_date_nice(datetime.datetime(2009, 5, 26, 11, 38, 53))
48
'Tue May 26 2009, 11:38am'
50
dt = get_datetime(datetime_or_seconds)
51
return dt.strftime("%a %b %d %Y, %l:%M%P")
53
def make_date_nice_short(datetime_or_seconds):
54
"""Generate a very compact human-readable representation of a date.
56
Given a datetime or number of seconds elapsed since the epoch,
35
#return time.ctime(seconds_since_epoch)
36
return time.strftime("%a %b %d %Y, %I:%M %p",
37
time.localtime(seconds_since_epoch))
39
def make_date_nice_short(seconds_since_epoch):
40
"""Given a number of seconds elapsed since the epoch,
57
41
generates a string representing the date in human-readable form.
58
42
Does not include the time.
60
>>> now = datetime.datetime.now()
61
>>> make_date_nice_short(now)
63
>>> make_date_nice_short(now - datetime.timedelta(1))
65
>>> make_date_nice_short(now - datetime.timedelta(2))
67
>>> make_date_nice_short(now - datetime.timedelta(5))
69
>>> make_date_nice_short(1242783748)
73
dt = get_datetime(datetime_or_seconds)
74
now = datetime.datetime.now()
43
This function generates a very compact representation."""
76
44
# Use a "naturalisation" algorithm.
45
days_ago = (int(time.time() - timezone_offset) / seconds_per_day
46
- int(seconds_since_epoch - timezone_offset) / seconds_per_day)
80
48
# Dates today or yesterday, return "today" or "yesterday".
86
# Dates in the last 5 days, return "n days ago".
87
return str(delta.days) + " days ago"
54
return str(days_ago) + " days ago"
55
# Dates in the last 5 days, return "n days ago".
88
56
# Other dates, return a short date format.
89
57
# If within the same year, omit the year (mmm dd)
90
if dt.year == now.year:
91
return dt.strftime("%b %d")
58
if time.localtime(seconds_since_epoch).tm_year==time.localtime().tm_year:
59
return time.strftime("%b %d", time.localtime(seconds_since_epoch))
92
60
# Else, include the year (mmm dd, yyyy)
94
return dt.strftime("%b %d, %Y")
96
def format_datetime_for_paragraph(datetime_or_seconds):
97
"""Generate a compact representation of a datetime for use in a paragraph.
99
Given a datetime or number of seconds elapsed since the epoch, generates
100
a compact string representing the date and time in human-readable form.
102
Unlike make_date_nice_short, the time will always be included.
104
Also unlike make_date_nice_short, it is suitable for use in the middle of
105
a block of prose and properly handles timestamps in the future nicely.
107
>>> now = datetime.datetime.now()
108
>>> today = now.date()
109
>>> time = datetime.time(10, 35, 40)
110
>>> earlier = datetime.datetime.combine(today, time)
111
>>> date = datetime.datetime(2009, 5, 20, 21, 19, 53)
113
>>> format_datetime_for_paragraph(now)
116
# We can go backwards and forwards a little while and be pretty.
117
>>> format_datetime_for_paragraph(now - datetime.timedelta(0, 40))
119
>>> format_datetime_for_paragraph(now + datetime.timedelta(0, 30))
122
>>> format_datetime_for_paragraph(now - datetime.timedelta(0, 245))
124
>>> format_datetime_for_paragraph(now + datetime.timedelta(0, 3500))
127
# If we go back further, it gets a bit ugly.
128
>>> format_datetime_for_paragraph(earlier - datetime.timedelta(1))
129
'yesterday at 10:35am'
130
>>> format_datetime_for_paragraph(date)
131
'on 2009-05-20 at 9:19pm'
133
>>> format_datetime_for_paragraph(earlier + datetime.timedelta(1))
134
'tomorrow at 10:35am'
137
dt = get_datetime(datetime_or_seconds)
138
now = datetime.datetime.now()
142
# If the date is earlier than now, we want to either say something like
143
# '5 days ago' or '25 seconds ago', 'yesterday at 08:54' or
144
# 'on 2009-03-26 at 20:09'.
146
# If the time is within one hour of now, we show it nicely in either
147
# minutes or seconds.
149
if abs(delta).days == 0 and abs(delta).seconds <= 1:
152
if abs(delta).days == 0 and abs(delta).seconds < 60*60:
153
if abs(delta) == delta:
154
# It's in the future.
161
# Show the number of minutes unless we are within two minutes.
162
if abs(delta).seconds >= 120:
163
return (prefix + '%d minutes' + suffix) % (abs(delta).seconds / 60)
165
return (prefix + '%d seconds' + suffix) % (abs(delta).seconds)
168
if dt.date() == now.date():
170
return dt.strftime('today at %l:%M%P')
171
elif dt.date() == now.date() - datetime.timedelta(days=1):
173
return dt.strftime('yesterday at %l:%M%P')
175
if dt.date() == now.date():
177
return dt.strftime('today at %l:%M%P')
178
elif dt.date() == now.date() + datetime.timedelta(days=1):
180
return dt.strftime('tomorrow at %l:%M%P')
182
return dt.strftime('on %Y-%m-%d at %l:%M%P')
62
return time.strftime("%b %d, %Y", time.localtime(seconds_since_epoch))