Start development series 2.1-post
[memo.git] / pretty_time.py
blob8221c6dcf871cea93e2fb5c9c82b9455edabd773
1 import time
3 day_name = [_('Monday'), _('Tuesday'), _('Wednesday'), _('Thursday'), _('Friday'),
4 _('Saturday'), _('Sunday')]
6 month_name = [_('January'), _('February'), _('March'), _('April'),
7 _('May'), _('June'), _('July'), _('August'),
8 _('September'), _('October'), _('November'), _('December')]
10 about_message = [_('nearly'), _('nearly'), _('about'), _('just gone'), _('just gone')]
12 section_name = ['', _('five past '), _('ten past '), _('a quarter past '),
13 _('twenty past '), _('twenty-five past '), _('half past '),
14 _('twenty-five to '), _('twenty to '), _('a quarter to '),
15 _('ten to '), _('five to ')]
17 number = [None, _('one'), _('two'), _('three'), _('four'), _('five'), _('six'),
18 _('seven'), _('eight'), _('nine'), _('ten'), _('eleven')]
20 def hour_name(hour):
21 assert hour >= 0 and hour < 24
23 if hour == 0:
24 return _("midnight")
25 elif hour == 12:
26 return _("noon")
27 return number[hour % 12]
29 def th(n):
30 "Cardinal integer to ordinal string."
31 if n > 3 and n < 20:
32 return _("%dth") % n
34 d = n % 10
35 if d == 1:
36 return _("%dst") % n
37 elif d == 2:
38 return _("%dnd") % n
39 elif d == 3:
40 return _("%drd") % n
41 else:
42 return _("%dth") % n
44 def rough_time(time_in_seconds):
45 "Convert a time (as returned by time()) to a string."
46 t = time.localtime(time_in_seconds + 150)
47 year, month, day, hour, minute, second, weekday, julian, dst = t
49 off = about_message[minute % 5]
51 if minute / 5 > 6:
52 hour = (hour + 1) % 24
54 if minute / 5 == 0 and hour != 0 and hour != 12:
55 o_clock = _(" o'clock")
56 else:
57 o_clock = ""
59 return _("It's %s %s%s%s") % (about_message[minute % 5],
60 section_name[minute / 5],
61 hour_name(hour), o_clock)
63 def str_time(hour = None, min = None):
64 if hour == None:
65 t = time.localtime(time.time())
66 year, month, day, hour, min, second, weekday, julian, dst = t
68 h = hour % 12
69 if h == 0:
70 h = 12
71 if hour < 12:
72 am = _('am')
73 else:
74 am = _('pm')
75 return _('%s:%02d %s') % (h, min, am)