util.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import random, time
  2. from datetime import timezone, datetime
  3. UTC = timezone.utc
  4. def random_blob_idx():
  5. return "%030x" % random.randrange(16**30)
  6. def datetime_to_unix(dt):
  7. return int(time.mktime(dt.timetuple()))
  8. def now():
  9. return datetime_to_unix(datetime.now(tz=UTC))
  10. def month_to_unix(month=None):
  11. month_year = month if month is not None else datetime.now(UTC).strftime("%m%y")
  12. try:
  13. dt = datetime.strptime(month_year,"%m%y")
  14. dt = dt.replace(tzinfo=UTC)
  15. unix = int(dt.timestamp())
  16. except ValueError:
  17. print("Error parsing date!")
  18. exit(-1)
  19. return unix
  20. def unix_to_datetime(timestamp):
  21. timestamp = int(timestamp)
  22. ts = timestamp if timestamp < 1e10 else timestamp/1000
  23. return datetime.fromtimestamp(ts, UTC)
  24. task_template = {
  25. "workspace": str,
  26. "title": str,
  27. "tags": list,
  28. "desc": str,
  29. "owner": str,
  30. "assign": list,
  31. "project": list,
  32. "due": int,
  33. "rank": float,
  34. "created_at": int,
  35. "state": str,
  36. "events": list,
  37. "comments": list,
  38. }
  39. def _enforce_task_format(task):
  40. for attr, val in task.items():
  41. val_type = task_template[attr]
  42. if val is None:
  43. assert val_type == list or attr not in ["created"]
  44. continue
  45. assert isinstance(val, val_type)