summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGéry Debongnie <ged@odoo.com>2018-06-28 15:10:11 +0200
committerAdrien Dieudonne <adr@odoo.com>2018-07-06 14:09:09 +0200
commit9756109e670cef01b4d475c8cb6687fecdab2cd2 (patch)
treee5f0898b4d92637453615fc1f0deffa7868157f1
parent5d3f42a9ad3762b4871cbb4687276a530ffff8f7 (diff)
[IMP] web: add tz_offset function in python eval context
-rw-r--r--addons/web/static/src/js/core/py_utils.js18
1 files changed, 18 insertions, 0 deletions
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,
};
}