mirror of
https://github.com/guohuadeng/app-odoo.git
synced 2025-02-23 04:11:36 +02:00
update common
This commit is contained in:
@@ -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',
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user