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.

113 lines
4.2 KiB

10 years ago
  1. //-*- coding: utf-8 -*-
  2. //############################################################################
  3. //
  4. // OpenERP, Open Source Management Solution
  5. // This module copyright (C) 2014 Therp BV (<http://therp.nl>).
  6. //
  7. // This program is free software: you can redistribute it and/or modify
  8. // it under the terms of the GNU Affero General Public License as
  9. // published by the Free Software Foundation, either version 3 of the
  10. // License, or (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU Affero General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU Affero General Public License
  18. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. //
  20. //############################################################################
  21. openerp.web_pytz = function(instance)
  22. {
  23. var original_pyeval_context = instance.web.pyeval.context;
  24. var tzinfo = py.type('tzinfo', null, {
  25. tzname: function()
  26. {
  27. var args = py.PY_parseArgs(arguments, 'dst');
  28. return py.str.fromJSON(this.zone.name);
  29. },
  30. localize: function()
  31. {
  32. var args = py.PY_parseArgs(arguments, 'dst'),
  33. result = py.PY_call(
  34. args.dst.__class__,
  35. [
  36. py.float.fromJSON(args.dst.year),
  37. py.float.fromJSON(args.dst.month),
  38. py.float.fromJSON(args.dst.day),
  39. py.float.fromJSON(args.dst.hour),
  40. py.float.fromJSON(args.dst.minute),
  41. py.float.fromJSON(args.dst.second),
  42. py.float.fromJSON(args.dst.microsecond),
  43. ]);
  44. result.tzinfo = this;
  45. return result;
  46. },
  47. });
  48. var pytz = py.PY_call(py.object);
  49. pytz.timezone = py.PY_def.fromJSON(function()
  50. {
  51. var args = py.PY_parseArgs(arguments, 'tz_name'),
  52. tz = moment.tz.zone(args.tz_name.toJSON()),
  53. result = py.PY_call(tzinfo);
  54. result.zone = tz;
  55. return result;
  56. });
  57. pytz.utc = py.PY_call(
  58. py.PY_getAttr(pytz, 'timezone'), [py.str.fromJSON('UTC')]);
  59. function astimezone()
  60. {
  61. var args = py.PY_parseArgs(arguments, 'tzinfo');
  62. // TODO: check that we only do this with localized dts
  63. var d = moment.tz(
  64. {
  65. year: this.year,
  66. month: this.month - 1,
  67. day: this.day,
  68. hour: this.hour,
  69. minute: this.minute,
  70. second: this.second,
  71. },
  72. this.tzinfo.zone.name)
  73. .tz(args.tzinfo.zone.name);
  74. return py.PY_call(
  75. this.__class__,
  76. [d.year(), d.month() + 1, d.date(), d.hour(), d.minute(),
  77. d.second()]);
  78. };
  79. instance.web.pyeval.context = function ()
  80. {
  81. var ctx = original_pyeval_context();
  82. ctx.datetime.datetime.astimezone = astimezone;
  83. return _.extend(
  84. ctx,
  85. {
  86. pytz: pytz,
  87. utc_today: function(args, kwargs)
  88. {
  89. var timezone = py.PY_call(
  90. py.PY_getAttr(pytz, 'timezone'),
  91. [py.str.fromJSON(
  92. ctx.tz || (args.length ? args[0] : 'UTC'))]),
  93. now = py.PY_call(
  94. py.PY_getAttr(ctx.datetime.datetime, 'now')),
  95. localized = py.PY_call(
  96. py.PY_getAttr(timezone, 'localize'),
  97. [now]);
  98. localized.hour = 0;
  99. localized.minute = 0;
  100. localized.second = 0;
  101. localized.millisecond = 0;
  102. return py.PY_call(
  103. py.PY_getAttr(localized, 'astimezone'),
  104. [py.PY_getAttr(pytz, 'utc')]);
  105. }
  106. });
  107. }
  108. }