@@ -215,10 +215,10 @@ def _get_year_stats(self, year: int) -> Optional[YearStats]:
215215 return YearStats (
216216 year = year ,
217217 prints_completed = stats .get ("prints_finished" , 0 ),
218- total_print_duration = self . _to_duration_days (
218+ total_print_duration = _format_duration (
219219 int (stats .get ("print_duration_total" , 0 ))
220220 ),
221- longest_print = self . _to_duration_hours (
221+ longest_print = _format_duration (
222222 int (stats .get ("longest_print_duration" , 0 ))
223223 ),
224224 busiest_weekday = weekday ,
@@ -231,31 +231,6 @@ def _get_year_stats(self, year: int) -> Optional[YearStats]:
231231 )
232232 return None
233233
234- def _to_duration_days (self , seconds : int ) -> str :
235- days = int (seconds / SECONDS_DAY )
236- seconds -= days * SECONDS_DAY
237-
238- hours = int (seconds / SECONDS_HOUR )
239- seconds -= hours * SECONDS_HOUR
240-
241- minutes = int (seconds / SECONDS_MINUTE )
242- seconds -= minutes * SECONDS_MINUTE
243-
244- if days >= 100 :
245- # strip the minutes to keep things fitting...
246- return f"{ days } d { hours } h"
247- else :
248- return f"{ days } d { hours } h { minutes } m"
249-
250- def _to_duration_hours (self , seconds : int ) -> str :
251- hours = int (seconds / SECONDS_HOUR )
252- seconds -= hours * SECONDS_HOUR
253-
254- minutes = int (seconds / SECONDS_MINUTE )
255- seconds -= minutes * SECONDS_MINUTE
256-
257- return f"{ hours } h { minutes } m"
258-
259234 def _load_font (self ) -> None :
260235 from base64 import b64encode
261236
@@ -273,6 +248,54 @@ def _load_font(self) -> None:
273248 self ._logger .exception ("Error creating data URI for embedded font" )
274249
275250
251+ def _format_duration (seconds : int ) -> str :
252+ """
253+ Formats a duration in seconds for the wrapped picture.
254+
255+ Examples:
256+
257+ >>> _format_duration(SECONDS_MINUTE)
258+ '0h 1m'
259+ >>> _format_duration(SECONDS_HOUR)
260+ '1h 0m'
261+ >>> _format_duration(SECONDS_DAY)
262+ '1d 0h 0m'
263+ >>> _format_duration(SECONDS_DAY + SECONDS_HOUR + SECONDS_MINUTE)
264+ '1d 1h 1m'
265+ >>> _format_duration(99 * SECONDS_DAY + SECONDS_HOUR + SECONDS_MINUTE)
266+ '99d 1h 1m'
267+ >>> _format_duration(100 * SECONDS_DAY + SECONDS_HOUR + SECONDS_MINUTE)
268+ '100d 1h'
269+ >>> _format_duration(100 * SECONDS_DAY + SECONDS_HOUR + 30 * SECONDS_MINUTE)
270+ '100d 2h'
271+ >>> _format_duration(100 * SECONDS_DAY + 23 * SECONDS_HOUR + 30 * SECONDS_MINUTE)
272+ '101d 0h'
273+ """
274+ days = int (seconds / SECONDS_DAY )
275+ seconds %= SECONDS_DAY
276+
277+ hours = int (seconds / SECONDS_HOUR )
278+ seconds %= SECONDS_HOUR
279+
280+ minutes = int (seconds / SECONDS_MINUTE )
281+ seconds %= SECONDS_MINUTE
282+
283+ if days >= 100 :
284+ # strip the minutes to keep things fitting (but round up if needed)...
285+ if minutes >= 30 :
286+ # past 30m -> add an hour
287+ hours += 1
288+ if hours >= 24 :
289+ # past 24h -> add a day
290+ hours %= 24
291+ days += 1
292+ return f"{ days } d { hours } h"
293+ elif days == 0 :
294+ return f"{ hours } h { minutes } m"
295+ else :
296+ return f"{ days } d { hours } h { minutes } m"
297+
298+
276299__plugin_name__ = "OctoPrint Wrapped!"
277300__plugin_pythoncompat__ = ">=3.9,<4" # Only Python 3
278301
0 commit comments