You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
3.0 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # This module copyright (C) 2014 Savoir-faire Linux
  6. # (<http://www.savoirfairelinux.com>).
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. from datetime import datetime
  23. import logging
  24. import pytz
  25. from jinja2 import contextfilter
  26. from openerp.tools import (
  27. DEFAULT_SERVER_DATETIME_FORMAT as DTFMT,
  28. DEFAULT_SERVER_DATE_FORMAT as DFMT,
  29. )
  30. from openerp.addons.email_template import email_template
  31. _logger = logging.getLogger(__name__)
  32. @contextfilter
  33. def format_date(context, dtstr, new_format=None, tz=None):
  34. if not dtstr:
  35. return dtstr
  36. if not new_format:
  37. if context.get("user") and context["user"].lang:
  38. user = context["user"]
  39. lang_obj = user.pool["res.lang"]
  40. cr, uid = user._cr, user._uid
  41. lang = lang_obj.search(cr, uid, [
  42. ('code', '=', user.lang)
  43. ])
  44. if lang:
  45. new_format = lang_obj.browse(cr, uid, lang[0]).date_format
  46. new_format = new_format or DFMT
  47. try:
  48. date = datetime.strptime(dtstr, DTFMT)
  49. except ValueError:
  50. # Maybe this is a date, not datetime
  51. date = datetime.strptime(dtstr, DFMT)
  52. else:
  53. # If this was a date, calculating timezones will give unexpected
  54. # results. Any timezone with a negative offset will be the day
  55. # before, since (midnight - anything) is the previous day
  56. if tz:
  57. tz_name = tz
  58. elif context.get("user") and context["user"].tz:
  59. tz_name = context["user"].tz
  60. else:
  61. tz_name = context.get("ctx", {}).get("tz")
  62. if tz_name:
  63. try:
  64. utc = pytz.timezone('UTC')
  65. context_tz = pytz.timezone(tz_name)
  66. utc_timestamp = utc.localize(date, is_dst=False)
  67. date = utc_timestamp.astimezone(context_tz)
  68. except Exception:
  69. _logger.debug("failed to compute context/client-specific "
  70. "timestamp, using the UTC value",
  71. exc_info=True)
  72. return date.strftime(new_format)
  73. email_template.mako_template_env.filters.update(
  74. format_date=format_date,
  75. )