From 9756109e670cef01b4d475c8cb6687fecdab2cd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Thu, 28 Jun 2018 15:10:11 +0200 Subject: [PATCH] [IMP] web: add tz_offset function in python eval context --- addons/web/static/src/js/core/py_utils.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/addons/web/static/src/js/core/py_utils.js b/addons/web/static/src/js/core/py_utils.js index 3ebb79d49ae..83e24b207c6 100644 --- a/addons/web/static/src/js/core/py_utils.js +++ b/addons/web/static/src/js/core/py_utils.js @@ -231,6 +231,23 @@ function context_today() { py.extras.datetime.date, [d.getFullYear(), d.getMonth() + 1, d.getDate()]); } +/** + * Returns a timedelta object which represents the timezone offset between the + * local timezone and the UTC time. + * + * This is very useful to generate datetime strings which are 'timezone' + * dependant. For example, we can now write this to generate the correct + * datetime string representing "this morning in the user timezone": + * + * "datetime.datetime.now().replace(hour=0,minute=0,second=0) + tz_offset()).strftime('%Y-%m-%d %H:%M:%S')" + * @returns {datetime.timedelta} + */ +function tz_offset() { + var offset= new Date().getTimezoneOffset(); + var kwargs = {minutes: py.float.fromJSON(offset)}; + return py.PY_call(py.extras.datetime.timedelta,[],kwargs); +} + function pycontext() { return { @@ -240,6 +257,7 @@ function pycontext() { relativedelta: py.extras.relativedelta, current_date: py.PY_call( py.extras.time.strftime, [py.str.fromJSON('%Y-%m-%d')]), + tz_offset: tz_offset, }; }