utils.py 848 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # -*- coding: utf-8 -*-
  2. """
  3. Utility functions
  4. """
  5. from datetime import datetime
  6. import logging
  7. from pelican.utils import set_date_tzinfo
  8. DEV_LOGGER = logging.getLogger(__name__)
  9. STRING_BOOLS = {
  10. 'yes': True,
  11. 'no': False,
  12. 'true': True,
  13. 'false': False,
  14. '0': False,
  15. '1': True,
  16. 'on': True,
  17. 'off': False,
  18. }
  19. def string_to_bool(string):
  20. '''
  21. Convert a string to a bool based
  22. '''
  23. return STRING_BOOLS[string.strip().lower()]
  24. def datetime_from_timestamp(timestamp, content):
  25. """
  26. Helper function to add timezone information to datetime,
  27. so that datetime is comparable to other datetime objects in recent versions
  28. that now also have timezone information.
  29. """
  30. return set_date_tzinfo(
  31. datetime.fromtimestamp(timestamp),
  32. tz_name=content.settings.get('TIMEZONE', None))