~ubuntu-branches/ubuntu/precise/gnome-games/precise-proposed

« back to all changes in this revision

Viewing changes to gnome-sudoku/src/lib/timer.py

  • Committer: Package Import Robot
  • Author(s): Rodrigo Moya
  • Date: 2011-05-30 13:32:04 UTC
  • mfrom: (1.3.4)
  • mto: (163.1.3 precise)
  • mto: This revision was merged to the branch mainline in revision 143.
  • Revision ID: package-import@ubuntu.com-20110530133204-celaq1v1dsxc48q1
Tags: upstream-3.0.2
ImportĀ upstreamĀ versionĀ 3.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# -*- coding: utf-8 -*-
2
 
import gtk, gobject
 
2
from gi.repository import GObject
3
3
import time
4
 
from gettext import gettext as _
5
 
from gettext import ngettext
6
 
 
7
 
def format_time (tim, round_at = None, friendly = False):
8
 
    """Format a time for display to the user.
9
 
 
10
 
    If round_at, we round all times to some number of seconds.
11
 
 
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...
15
 
    """
16
 
    tim = int(tim)
17
 
    time_strings = []
18
 
    units = [(int(365.25 * 24 * 60 * 60),
19
 
              lambda years: ngettext("%(n)s year", "%(n)s years", years) % {'n': years}),
20
 
             (31 * 24 * 60 * 60,
21
 
              lambda months: ngettext("%(n)s month", "%(n)s months", months) % {'n': months}),
22
 
             (7 * 24 * 60 * 60,
23
 
              lambda weeks: ngettext("%(n)s week", "%(n)s weeks", weeks) % {'n': weeks}),
24
 
             (24 * 60 * 60,
25
 
              lambda days: ngettext("%(n)s day", "%(n)s days", days) % {'n': days}),
26
 
             (60 * 60,
27
 
              lambda hours: ngettext("%(n)s hour", "%(n)s hours", hours) % {'n': hours}),
28
 
             (60,
29
 
              lambda minutes: ngettext("%(n)s minute", "%(n)s minutes", minutes) % {'n': minutes}),
30
 
             (1,
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
34
 
        if time_covered:
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))
38
 
                break
39
 
            else:
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]])
52
 
    else:
53
 
        return _(" ").join(time_strings)
54
 
 
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:
61
 
        # Then we're today
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}
69
 
        else:
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)
78
 
    else:
79
 
        # Translators, see strftime manual in order to translate %? format strings
80
 
        return time.strftime(_("%B %e"), local_tim)
81
 
 
82
 
class ActiveTimer (gobject.GObject):
 
4
 
 
5
class ActiveTimer (GObject.GObject):
83
6
    """A timer to keep track of how much time a window is active."""
84
7
 
85
8
    __gsignals__ = {
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, ())
88
11
        }
89
12
 
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
146
69
        self.mark_timing()
147
70
        self.timer_running = False
148
71
 
149
 
    def active_time_string (self):
150
 
        return format_time(self.active_time)
151
 
 
152
 
    def total_time_string (self):
153
 
        return format_time(self.total_time)
154
 
 
155
72
if __name__ == '__main__':
 
73
    from gi.repository import Gtk
 
74
 
156
75
    def report (timer):
157
 
        print 'active:', timer.active_time_string()
158
 
        print 'total:', timer.total_time_string()
 
76
        pass
159
77
 
160
78
    def test_active_timer ():
161
 
        win = gtk.Window()
 
79
        win = Gtk.Window()
162
80
        timer = ActiveTimer(win)
163
81
        timer.start_timing()
164
82
        win.connect('focus-out-event', lambda *args: report(timer))
165
 
        win.connect('delete-event', gtk.main_quit)
 
83
        win.connect('delete-event', Gtk.main_quit)
166
84
        win.show()
167
 
        gtk.main()
 
85
        Gtk.main()
168
86
 
169
87
    test_active_timer()