25 lines
751 B
Python
25 lines
751 B
Python
|
|
from datetime import datetime
|
||
|
|
from typing import Union
|
||
|
|
|
||
|
|
|
||
|
|
def parse_timestamp(timestamp: Union[int, str], fmt: str = '%Y-%m-%d %H:%M:%S') -> str:
|
||
|
|
if isinstance(timestamp, str):
|
||
|
|
timestamp = int(timestamp)
|
||
|
|
return datetime.fromtimestamp(timestamp).strftime(fmt)
|
||
|
|
|
||
|
|
|
||
|
|
def format_size(bytes_size: int) -> str:
|
||
|
|
for unit in ['B', 'KB', 'MB', 'GB']:
|
||
|
|
if bytes_size < 1024.0:
|
||
|
|
return f"{bytes_size:.2f} {unit}"
|
||
|
|
bytes_size /= 1024.0
|
||
|
|
return f"{bytes_size:.2f} TB"
|
||
|
|
|
||
|
|
|
||
|
|
def format_duration(seconds: int) -> str:
|
||
|
|
hours, remainder = divmod(seconds, 3600)
|
||
|
|
minutes, seconds = divmod(remainder, 60)
|
||
|
|
if hours > 0:
|
||
|
|
return f"{hours:02d}:{minutes:02d}:{seconds:02d}"
|
||
|
|
return f"{minutes:02d}:{seconds:02d}"
|