util.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. "bounty": int,
  35. "created_at": int,
  36. "state": str,
  37. "events": list,
  38. "comments": list,
  39. }
  40. def _enforce_task_format(task):
  41. for attr, val in task.items():
  42. val_type = task_template[attr]
  43. if val is None:
  44. assert val_type == list or attr not in ["created"]
  45. continue
  46. assert isinstance(val, val_type)