microblog.pub/utils/__init__.py

33 lines
711 B
Python
Raw Permalink Normal View History

2018-05-22 22:57:34 +00:00
import logging
2019-07-12 20:03:04 +00:00
from datetime import datetime
from datetime import timezone
from dateutil import parser
from little_boxes import activitypub as ap
2018-05-22 22:57:34 +00:00
logger = logging.getLogger(__name__)
def strtobool(s: str) -> bool:
2018-06-17 17:21:59 +00:00
if s in ["y", "yes", "true", "on", "1"]:
2018-05-22 22:57:34 +00:00
return True
2018-06-17 17:21:59 +00:00
if s in ["n", "no", "false", "off", "0"]:
2018-05-22 22:57:34 +00:00
return False
2018-06-17 17:21:59 +00:00
raise ValueError(f"cannot convert {s} to bool")
2019-07-12 20:03:04 +00:00
2019-07-12 22:14:29 +00:00
def parse_datetime(s: str) -> datetime:
2019-07-12 20:03:04 +00:00
# Parses the datetime with dateutil
dt = parser.parse(s)
# If no TZ is set, assumes it's UTC
if not dt.tzinfo:
dt = dt.replace(tzinfo=timezone.utc)
return dt
def now() -> str:
return ap.format_datetime(datetime.now(timezone.utc))