Project 'mj/mo-submit' was moved to 'mo-p/osmo'. Please update any links and bookmarks that may still have the old path.
Select Git revision
kgrams_test.cpp
utils.py 1.12 KiB
import sys, os
import traceback
import datetime
from typing import Optional
def eprint(*args, flush=True):
print(*args, file=sys.stderr, flush=flush)
async def catch_all(corutine, exitcode=None):
try:
await corutine
except Exception as e:
eprint(traceback.format_exc())
if exitcode is not None:
exit(exitcode)
local_timezone = datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo
dt_pinf = datetime.datetime(2100, 1, 1, tzinfo=local_timezone)
dt_minf = datetime.datetime(2000, 1, 1, tzinfo=local_timezone)
def format_dt_to_user(dt: datetime.datetime) -> str:
return dt.strftime("%Y-%m-%d %H:%M:%S")
def parse_dt_from_user(val: str) -> Optional[datetime.datetime]:
return datetime.datetime.strptime(val, "%Y-%m-%d %H:%M:%S").replace(tzinfo=local_timezone)
def format_delay_s(s):
if s is None:
return "–"
if s < 0: return "- " + format_delay_s(-s)
if s == 0: return "on time"
if s < 60: return f"{s} s"
if s < 10*60: return f"{s//60} min {s%60} s"
if s < 60*60: return f"{s//60} min"
if s < 60*60: return f"{s//60//60} h {s//60} min"