|
14 | 14 | # A named tuple for holding the elapsed and total playing times from Foobar2000's status bar |
15 | 15 | statusBarTimes = collections.namedtuple('StatusBarTimes', ['elapsed', 'total']) |
16 | 16 |
|
17 | | -def getParsingFormat(interval): |
18 | | - """Attempts to find a suitable parsing format string for a HH:MM:SS, MM:SS or SS -style time interval.""" |
19 | | - timeParts = len(interval.split(":")) |
| 17 | + |
| 18 | +def parseIntervalToTimestamp(interval): |
| 19 | + """Parses a D HH:MM:SS, HH:MM:SS, MM:SS or SS -style interval to a timestamp.""" |
| 20 | + timeParts = len(interval.strip().replace(" ", ":").split(":")) |
| 21 | + # day is automatically set to 1 |
| 22 | + # if no day is provided, remove 86400 seconds from parsed time |
20 | 23 | if timeParts == 1: |
21 | | - return "%S" |
| 24 | + return calendar.timegm(time.strptime(interval.strip(), "%S")) - 86400 |
22 | 25 | elif timeParts == 2: |
23 | | - return "%M:%S" |
| 26 | + return calendar.timegm(time.strptime(interval.strip(), "%M:%S")) - 86400 |
24 | 27 | elif timeParts == 3: |
25 | | - return "%H:%M:%S" |
| 28 | + return calendar.timegm(time.strptime(interval.strip(), "%H:%M:%S")) - 86400 |
| 29 | + elif timeParts == 4: |
| 30 | + return calendar.timegm(time.strptime(interval.strip(), "%dd %H:%M:%S")) |
26 | 31 | else: |
27 | 32 | return None |
28 | 33 |
|
29 | | -def getOutputFormat(seconds): |
30 | | - """Returns a format string for the given number of seconds with the least leading zeros.""" |
31 | | - if seconds < 60: |
32 | | - return "%S" |
33 | | - elif seconds < 3600: |
34 | | - return "%M:%S" |
| 34 | + |
| 35 | +def getRemainingTime(parsedElapsedTime, parsedTotalTime): |
| 36 | + """Returns the difference between two times.""" |
| 37 | + remainingTime = parsedTotalTime - parsedElapsedTime |
| 38 | + if remainingTime < 60: |
| 39 | + return time.strftime("%S", time.gmtime(remainingTime)) |
| 40 | + elif remainingTime < 3600: |
| 41 | + return time.strftime("%M:%S", time.gmtime(remainingTime)) |
| 42 | + elif remainingTime < 86400: |
| 43 | + return time.strftime("%H:%M:%S", time.gmtime(remainingTime)) |
| 44 | + elif remainingTime < 172800: |
| 45 | + return time.strftime("%#d day %H:%M:%S", time.gmtime(remainingTime - 86400)) |
35 | 46 | else: |
36 | | - return "%H:%M:%S" |
| 47 | + return time.strftime("%#d days %H:%M:%S", time.gmtime(remainingTime - 86400)) |
37 | 48 |
|
38 | | -def parseIntervalToTimestamp(interval): |
39 | | - """Parses a HH:MM:SS, MM:SS or SS -style interval to a timestamp.""" |
40 | | - format = getParsingFormat(interval) |
41 | | - return calendar.timegm(time.strptime(interval.strip(), format)) |
42 | 49 |
|
43 | 50 | class AppModule(appModuleHandler.AppModule): |
44 | 51 | _statusBar = None |
@@ -77,25 +84,27 @@ def script_reportRemainingTime(self,gesture): |
77 | 84 | # Translators: Reported if the remaining time can not be calculated in Foobar2000 |
78 | 85 | msg = _("Unable to determine remaining time") |
79 | 86 | else: |
80 | | - parsedElapsedTime = parseIntervalToTimestamp(elapsedTime) |
81 | 87 | parsedTotalTime = parseIntervalToTimestamp(totalTime) |
82 | | - remainingTime = parsedTotalTime - parsedElapsedTime |
83 | | - msg = time.strftime(getOutputFormat(remainingTime), time.gmtime(remainingTime)) |
| 88 | + parsedElapsedTime = parseIntervalToTimestamp(elapsedTime) |
| 89 | + msg = getRemainingTime(parsedElapsedTime, parsedTotalTime) + " remaining" |
84 | 90 | ui.message(msg) |
85 | 91 | # Translators: The description of an NVDA command for reading the remaining time of the currently playing track in Foobar 2000. |
86 | 92 | script_reportRemainingTime.__doc__ = _("Reports the remaining time of the currently playing track, if any") |
87 | 93 |
|
88 | 94 | def script_reportElapsedTime(self,gesture): |
89 | 95 | elapsedTime = self.getElapsedAndTotalIfPlaying()[0] |
90 | 96 | if elapsedTime is not None: |
91 | | - ui.message(elapsedTime) |
| 97 | + ui.message(elapsedTime + " elapsed") |
| 98 | + else: |
| 99 | + # Translators: Reported if the elapsed time is not available in Foobar2000 |
| 100 | + ui.message(_("Elapsed time not available")) |
92 | 101 | # Translators: The description of an NVDA command for reading the elapsed time of the currently playing track in Foobar 2000. |
93 | 102 | script_reportElapsedTime.__doc__ = _("Reports the elapsed time of the currently playing track, if any") |
94 | 103 |
|
95 | 104 | def script_reportTotalTime(self,gesture): |
96 | 105 | totalTime = self.getElapsedAndTotalIfPlaying()[1] |
97 | 106 | if totalTime is not None: |
98 | | - ui.message(totalTime) |
| 107 | + ui.message(totalTime + " total") |
99 | 108 | else: |
100 | 109 | # Translators: Reported if the total time is not available in Foobar2000 |
101 | 110 | ui.message(_("Total time not available")) |
|
0 commit comments