datetime.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python3
  2. # Contest Management System - http://cms-dev.github.io/
  3. # Copyright © 2012-2017 Luca Wehrstedt <luca.wehrstedt@gmail.com>
  4. # Copyright © 2013 Stefano Maggiolo <s.maggiolo@gmail.com>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. import os
  19. import sys
  20. import time
  21. from datetime import datetime
  22. import babel.dates
  23. __all__ = [
  24. "make_datetime", "make_timestamp",
  25. "get_timezone", "get_system_timezone",
  26. "utc", "local_tz",
  27. ]
  28. def make_datetime(timestamp=None):
  29. """Return the datetime object associated with the given timestamp.
  30. timestamp (int|float|None): a POSIX timestamp, or None to use now.
  31. return (datetime): the datetime representing the UTC time of the
  32. given timestamp.
  33. """
  34. if timestamp is None:
  35. return datetime.utcnow()
  36. else:
  37. return datetime.utcfromtimestamp(timestamp)
  38. EPOCH = datetime(1970, 1, 1)
  39. def make_timestamp(_datetime=None):
  40. """Return the timestamp associated with the given datetime object.
  41. _datetime (datetime|None): a datetime object, or None to use now.
  42. return (float): the POSIX timestamp corresponding to the given
  43. datetime ("read" in UTC).
  44. """
  45. if _datetime is None:
  46. return time.time()
  47. else:
  48. return (_datetime - EPOCH).total_seconds()
  49. utc = babel.dates.UTC
  50. local_tz = babel.dates.LOCALTZ
  51. def get_timezone(user, contest):
  52. """Return the timezone for the given user and contest
  53. user (User): the user owning the timezone.
  54. contest (Contest): the contest in which the user is competing.
  55. return (tzinfo): the timezone information for the user.
  56. """
  57. if user.timezone is not None:
  58. try:
  59. return babel.dates.get_timezone(user.timezone)
  60. except LookupError:
  61. pass
  62. if contest.timezone is not None:
  63. try:
  64. return babel.dates.get_timezone(contest.timezone)
  65. except LookupError:
  66. pass
  67. return local_tz
  68. def get_system_timezone():
  69. """Return the name of the system timezone.
  70. return (unicode): the "best" description of the timezone of the
  71. local system clock that we were able to find, in a format like
  72. "Europe/Rome", "CET", etc.
  73. """
  74. if hasattr(local_tz, 'zone'):
  75. return local_tz.zone
  76. return local_tz.tzname(make_datetime())