mirror of
https://github.com/guohuadeng/app-odoo.git
synced 2025-02-23 04:11:36 +02:00
opt m2o
This commit is contained in:
@@ -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,29 @@ 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
|
||||
if isinstance(value, datetime):
|
||||
value = value.strftime(return_format)
|
||||
dt = datetime.strptime(value, return_format)
|
||||
pytz_timezone = pytz.timezone(self.env.user.tz or 'Etc/GMT-8')
|
||||
dt = dt.replace(tzinfo=pytz.timezone('UTC'))
|
||||
return dt.astimezone(pytz_timezone).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