File: timer.py

package info (click to toggle)
osdlyrics 0.5.15%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 2,616 kB
  • sloc: ansic: 19,458; python: 4,867; sh: 572; makefile: 366; sed: 16
file content (87 lines) | stat: -rw-r--r-- 2,406 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# -*- coding: utf-8 -*-
#
# Copyright (C) 2011  Tiger Soldier
#
# This file is part of OSD Lyrics.
#
# OSD Lyrics is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OSD Lyrics is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with OSD Lyrics.  If not, see <https://www.gnu.org/licenses/>.
#
from datetime import datetime


class Timer:
    """ A timer to account the elapsed playing.
    """

    def __init__(self, accuracy=0):
        """

        Arguments:
        - `accuracy`:The accuracy of the time provided by players.
          If the difference between the time provided by players and the time of
          the timer is greater than accuracy, the time of the timer will be adjust to
          the time of the player.
        """
        self._accuracy = accuracy
        self._started = False
        self._time = 0
        self._begintime = None

    def play(self):
        """
        Starts the timer
        """
        if not self._started:
            self._started = True
            self._begintime = datetime.now()

    def pause(self):
        """
        Stops the timer
        """
        self._time = self.time
        self._begintime = None
        self._started = False

    def stop(self):
        """
        Stops the timer and sets the elapsed time to 0
        """
        self.pause()
        self._time = 0

    @property
    def time(self):
        now = datetime.now()
        if self._started:
            return self._time + int((now - self._begintime).total_seconds() * 1000)
        else:
            return self._time

    @time.setter
    def time(self, value):
        self.set_time(value)

    def set_time(self, value):
        """
        Adjust the time.

        If the time needs to be adjusted, return True.
        """
        time = self.time
        if abs(time - value) > self._accuracy:
            self._time = value
            self._begintime = datetime.now()
            return True
        return False