'commit'
This commit is contained in:
24
DouYin/dy-downloader/utils/helpers.py
Normal file
24
DouYin/dy-downloader/utils/helpers.py
Normal file
@@ -0,0 +1,24 @@
|
||||
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}"
|
||||
Reference in New Issue
Block a user