Format number of seconds into human-readable string. :param uptime_in_seconds: The server uptime in seconds. :returns: A human-readable string representing the uptime. >>> uptime = format_uptime('56892') >>> print(uptime) 15 hours 48 min 12 sec
(uptime_in_seconds: str)
| 28 | |
| 29 | |
| 30 | def format_uptime(uptime_in_seconds: str) -> str: |
| 31 | """Format number of seconds into human-readable string. |
| 32 | |
| 33 | :param uptime_in_seconds: The server uptime in seconds. |
| 34 | :returns: A human-readable string representing the uptime. |
| 35 | |
| 36 | >>> uptime = format_uptime('56892') |
| 37 | >>> print(uptime) |
| 38 | 15 hours 48 min 12 sec |
| 39 | """ |
| 40 | |
| 41 | m, s = divmod(int(uptime_in_seconds), 60) |
| 42 | h, m = divmod(m, 60) |
| 43 | d, h = divmod(h, 24) |
| 44 | |
| 45 | uptime_values: list[str] = [] |
| 46 | |
| 47 | for value, unit in ((d, "days"), (h, "hours"), (m, "min"), (s, "sec")): |
| 48 | if value == 0 and not uptime_values: |
| 49 | # Don't include a value/unit if the unit isn't applicable to |
| 50 | # the uptime. E.g. don't do 0 days 0 hours 1 min 30 sec. |
| 51 | continue |
| 52 | if value == 1 and unit.endswith("s"): |
| 53 | # Remove the "s" if the unit is singular. |
| 54 | unit = unit[:-1] |
| 55 | uptime_values.append(f'{value} {unit}') |
| 56 | |
| 57 | uptime = " ".join(uptime_values) |
| 58 | return uptime |
| 59 | |
| 60 | |
| 61 | def get_uptime(cur: Cursor) -> int: |