From f8b769911b275a18b8725fe4fdf05d6c206be4af Mon Sep 17 00:00:00 2001 From: ivan deng Date: Wed, 27 Jan 2021 02:40:31 +0800 Subject: [PATCH] update common --- app_common/__manifest__.py | 2 +- app_common/models/base.py | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/app_common/__manifest__.py b/app_common/__manifest__.py index 69687cdc..c313e426 100644 --- a/app_common/__manifest__.py +++ b/app_common/__manifest__.py @@ -33,7 +33,7 @@ { 'name': "Sunpop Odooapp Common Func", - 'version': '13.21.01.20', + 'version': '13.21.01.27', 'author': 'Sunpop.cn', 'category': 'Base', 'website': 'https://www.sunpop.cn', diff --git a/app_common/models/base.py b/app_common/models/base.py index 1639cde7..bb8efc47 100644 --- a/app_common/models/base.py +++ b/app_common/models/base.py @@ -1,6 +1,9 @@ # -*- coding: utf-8 -*- from odoo import models, fields, api, _ +from odoo.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT +from datetime import date, datetime, time +import pytz class Base(models.AbstractModel): _inherit = 'base' @@ -14,3 +17,27 @@ class Base(models.AbstractModel): rec = self.env[self._fields[fieldname].comodel_name].search(domain, limit=1) return rec.id if rec else False return False + + def _app_dt2local(self, value, return_format=DEFAULT_SERVER_DATETIME_FORMAT): + """ + 将value中时间,按格式转为用户本地时间 + """ + if not value: + return value + # 默认中国时区 + user_tz = pytz.timezone(self.env.user.tz or 'Etc/GMT+8') + dt_local = pytz.utc.localize(value).astimezone(user_tz) + return dt_local.strftime(return_format) + + def _app_dt2utc(self, value, return_format=DEFAULT_SERVER_DATETIME_FORMAT): + """ + 将value中用户本地时间,按格式转为UTC时间,输出 str + """ + if not value: + return value + if isinstance(value, datetime): + value = value.strftime(return_format) + dt = datetime.strptime(value, return_format) + user_tz = pytz.timezone(self.env.user.tz or 'Etc/GMT+8') + dt = dt.replace(tzinfo=pytz.timezone('UTC')) + return dt.astimezone(user_tz).strftime(return_format)