| 1 | from datetime import datetime |
| 2 | from typing import Union |
| 3 | |
| 4 | |
| 5 | def parse_timestamp(timestamp: Union[int, str], fmt: str = "%Y-%m-%d %H:%M:%S") -> str: |
| 6 | if isinstance(timestamp, str): |
| 7 | timestamp = int(timestamp) |
| 8 | return datetime.fromtimestamp(timestamp).strftime(fmt) |
| 9 | |
| 10 | |
| 11 | def format_size(bytes_size: int) -> str: |
| 12 | for unit in ["B", "KB", "MB", "GB"]: |
| 13 | if bytes_size < 1024.0: |
| 14 | return f"{bytes_size:.2f} {unit}" |
| 15 | bytes_size /= 1024.0 |
| 16 | return f"{bytes_size:.2f} TB" |
| 17 | |
| 18 | |
| 19 | def format_duration(seconds: int) -> str: |
| 20 | hours, remainder = divmod(seconds, 3600) |
| 21 | minutes, seconds = divmod(remainder, 60) |
| 22 | if hours > 0: |
| 23 | return f"{hours:02d}:{minutes:02d}:{seconds:02d}" |
| 24 | return f"{minutes:02d}:{seconds:02d}" |
| 25 |