1
1
# -*- coding: utf-8 -*-
2
from gi.repository import GObject
4
from gettext import gettext as _
5
from gettext import ngettext
7
def format_time (tim, round_at = None, friendly = False):
8
"""Format a time for display to the user.
10
If round_at, we round all times to some number of seconds.
12
If friendly, we don't bother showing the user more than two
13
units. i.e. 3 days 2 hours, or 2 minutes 30 seconds, but not 3
14
days, 4 hours, 2 minutes and 3 seconds...
18
units = [(int(365.25 * 24 * 60 * 60),
19
lambda years: ngettext("%(n)s year", "%(n)s years", years) % {'n': years}),
21
lambda months: ngettext("%(n)s month", "%(n)s months", months) % {'n': months}),
23
lambda weeks: ngettext("%(n)s week", "%(n)s weeks", weeks) % {'n': weeks}),
25
lambda days: ngettext("%(n)s day", "%(n)s days", days) % {'n': days}),
27
lambda hours: ngettext("%(n)s hour", "%(n)s hours", hours) % {'n': hours}),
29
lambda minutes: ngettext("%(n)s minute", "%(n)s minutes", minutes) % {'n': minutes}),
31
lambda seconds: ngettext("%(n)s second", "%(n)s seconds", seconds) % {'n': seconds})]
32
for divisor, unit_formatter in units:
33
time_covered = tim / divisor
35
if round_at and len(time_strings) + 1 >= round_at:
36
time_covered = int(round(float(tim) / divisor))
37
time_strings.append(unit_formatter(time_covered))
40
time_strings.append(unit_formatter(time_covered))
41
tim = tim - time_covered * divisor
42
if friendly and len(time_strings) > 2:
43
time_strings = time_strings[:2]
44
if len(time_strings) > 2:
45
# Translators... this is a messay way of concatenating
46
# lists. In English we do lists this way: 1, 2, 3, 4, 5
47
# and 6. This set-up allows for the English system only.
48
# You can of course make your language only use commas or
49
# ands or spaces or whatever you like by translating both
50
# ", " and " and " with the same string.
51
return _(" and ").join([_(", ").join(time_strings[0:-1]), time_strings[-1]])
53
return _(" ").join(time_strings)
55
def format_friendly_date (tim):
56
local_tim = time.localtime(tim)
57
diff = int(time.time() - tim)
58
curr_hour, curr_min = time.localtime()[3:5]
59
now_to_yesterday = curr_hour * 60 * 60 + curr_min * 60
60
if diff < now_to_yesterday:
62
if diff < 60: # within the minute
63
return ngettext("%(n)s second ago",
64
"%(n)s seconds ago", diff) % {'n': diff}
65
elif diff < (60 * 60): # within the hour...
66
minute = int(diff / 60)
67
return ngettext("%(n)s minute ago",
68
"%(n)s minutes ago", minute) % {'n': minute}
70
# Translators, see strftime manual in order to translate %? format strings
71
return time.strftime(_("at %I:%M %p"), local_tim)
72
elif diff < now_to_yesterday + (60 * 60 * 24):
73
# Translators, see strftime manual in order to translate %? format strings
74
return time.strftime(_("yesterday at %I:%M %p"), local_tim)
75
elif diff < now_to_yesterday + (60 * 60 * 24) * 6:
76
# Translators, see strftime manual in order to translate %? format strings
77
return time.strftime(_("%A %I:%M %p"), local_tim)
79
# Translators, see strftime manual in order to translate %? format strings
80
return time.strftime(_("%B %e"), local_tim)
82
class ActiveTimer (gobject.GObject):
5
class ActiveTimer (GObject.GObject):
83
6
"""A timer to keep track of how much time a window is active."""
86
'timing-started': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
87
'timing-stopped': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ())
9
'timing-started': (GObject.SignalFlags.RUN_LAST, None, ()),
10
'timing-stopped': (GObject.SignalFlags.RUN_LAST, None, ())
90
13
def __init__ (self, window):
91
gobject.GObject.__init__(self)
14
GObject.GObject.__init__(self)
92
15
self.window = window
93
16
# whether we have 'start_timing'; affects total_time
94
17
self.timer_running = False