all_tasks=set()# each registered task will show up here
all_tasks=set()# each registered task will show up here
DATETIME_FORMAT="%Y-%m-%dT%H:%M:%S.%f%Z%z"# used for serialization, I leave here both %Z%z for compatibility in case we ever want to improve timezone-related stuff
DATETIME_FORMAT="%Y-%m-%dT%H:%M:%S.%fZ"# need it to de/serialize datetime stored in JSON
defserialize(stuff):
defserialize(stuff):
returnjson.dumps(stuff).encode("utf-8")
returnjson.dumps(stuff).encode("utf-8")
defdeserialize(binary):
defdeserialize(bytes):
returnjson.loads(binary.decode("utf-8"))
returnjson.loads(bytes.decode("utf-8"))
defserialize_datetime(dt):
defserialize_datetime(dt):
returndatetime.strftime(dt,DATETIME_FORMAT)
"""
:param dt: datetime (timezone-aware)
:return: str that can be deserialized by deserialize_datetime()
defdeserialize_datetime(text):
"""
""" Throws ValueError when fails to parse the text """
ifnotdt.tzinfo:
returndatetime.strptime(text,DATETIME_FORMAT)
logger.warning("Naive datetime received by serialize_datetime() and will be treated as local: {dt}. Avoid using naive datetime objects.".format(dt=dt))
utc_dt=dt.astimezone(timezone.utc)
returndatetime.strftime(utc_dt,DATETIME_FORMAT)
defdeserialize_datetime(text,utc=False):
"""
:param text: str created by serialize_datetime()
:param utc: set to True if you want this function to return UTC datetime
:return: datetime (timezone-aware)
"""
dt=datetime.strptime(text,DATETIME_FORMAT)
assertdt.tzinfo,"ok, now that's weird, no tzinfo, but there must have been %z in the DATETIME_FORMAT"
ifutc:
returndt.astimezone(timezone.utc)
else:
returndt.astimezone()# not pytz, just old c++ stuff, but still a timezone with correct utc offset