23
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)
24
'''Return the given datetime, or convert the given seconds since epoch.'''
36
25
if type(datetime_or_seconds) is datetime.datetime:
37
26
return datetime_or_seconds
38
27
return datetime.datetime.fromtimestamp(datetime_or_seconds)
43
32
Given a datetime or number of seconds elapsed since the epoch,
44
33
generates a string representing the date/time in human-readable form.
45
34
"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
36
dt = get_datetime(datetime_or_seconds)
51
return dt.strftime("%a %b %d %Y, %l:%M%P")
37
return dt.strftime("%a %b %d %Y, %I:%M %p")
53
39
def make_date_nice_short(datetime_or_seconds):
54
40
"""Generate a very compact human-readable representation of a date.
56
42
Given a datetime or number of seconds elapsed since the epoch,
57
43
generates a string representing the date in human-readable form.
58
44
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
47
dt = get_datetime(datetime_or_seconds)
104
78
Also unlike make_date_nice_short, it is suitable for use in the middle of
105
79
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
82
dt = get_datetime(datetime_or_seconds)
168
113
if dt.date() == now.date():
170
return dt.strftime('today at %l:%M%P')
115
return dt.strftime('today at %I:%M %p')
171
116
elif dt.date() == now.date() - datetime.timedelta(days=1):
173
return dt.strftime('yesterday at %l:%M%P')
118
return dt.strftime('yesterday at %I:%M %p')
175
120
if dt.date() == now.date():
177
return dt.strftime('today at %l:%M%P')
122
return dt.strftime('today at %I:%M %p')
178
123
elif dt.date() == now.date() + datetime.timedelta(days=1):
180
return dt.strftime('tomorrow at %l:%M%P')
125
return dt.strftime('tomorrow at %I:%M %p')
182
return dt.strftime('on %Y-%m-%d at %l:%M%P')
127
return dt.strftime('on %Y-%m-%d at %I:%M %p')