Openerp 奥多9时区

Openerp 奥多9时区,openerp,odoo-9,Openerp,Odoo 9,连接日期和时间字段后,将结果放在新的日期时间字段中可获得2小时以上的时间 例如: 日期=字段。日期字符串='date',选择=True time=fields.many21'period.time',string='Available time',select=True date\u time=fields.Datetimestring='Datetime',compute=\u compute description=字段。Charstring='Descroption',compute=\u

连接日期和时间字段后,将结果放在新的日期时间字段中可获得2小时以上的时间

例如:

日期=字段。日期字符串='date',选择=True time=fields.many21'period.time',string='Available time',select=True date\u time=fields.Datetimestring='Datetime',compute=\u compute description=字段。Charstring='Descroption',compute=\u compute @api.一旦更改“日期”,“时间”时间为08:00:00 def_computeself: self.date\u time=%s%s:00%self.date,self.time.name self.description=%s%s:00%self.date,self.time.name 日期时间日期时间2017-04-10 10:00:00->错误

描述Char 2017-04-10 08:00:00->正确


如何解决此问题?

这在odoo方面没有错,因为odoo将日期保存为UTC。当用户请求日期时,odoo将数据库中的日期转换为用户的时区

因此,如果您转到数据库,您会发现该字段的存储日期为2017-04-10 08:00:00,但在视图中,日期转换为用户的时区,在您的情况下,日期转换为2017-04-10 10:00:00

因此,在保存日期之前,请确保将值转换为UTC日期时间,我的意思是不要将2017-04-10 08:00:00保存为日期时间,而是保存2017-04-10 06:00:00

编辑: 试试这个,我希望它能工作,我没有使用它,但我认为如果有任何错误,它会工作的,只要把它放在这里:

# first import 
from datetime import datetime
import pytz

class ...
    ...
    date = fields.Date(string = 'date', select=True)
    time = fields.Many2one('period.time', string='Available time',select=True)
    date_time = fields.Datetime(string='Datetime', compute="_compute")
    description = fields.Char(string = 'Descroption', compute="_compute")
    ...
    ...

    @api.onchange('date', 'time') #time is 08:00:00
    def _compute(self):
        # by default timezone = UTC
        user_time_zone = pytz.UTC

        if self.env.user.partner_id.tz :
            # change the timezone to the timezone of the user
            user_time_zone = pytz.timezone(self.env.user.partner_id.tz)

        # create time string
        concat_time = "%s %s:00" % (self.date,self.time.name)
        fmt = '%Y-%m-%d %H:%M:%S'

        # create a time object
        user_time = datetime.strptime(concat_time, fmt)

        # define the timezone of the time object
        # here i think the first line is enougth
        user_time = user_time_zone.localize(user_time)
        user_time.replace(tzinfo=user_time_zone)

        # you can get the time in UTC like this 
        print "This is what you need to save in database %s ", user_time.astimezone(pytz.UTC).strftime(fmt)

        self.date_time = user_time.astimezone(pytz.UTC).strftime(fmt)

在odoo类datetime中,您可以找到datetime类的方法

def context_timestamp(record, timestamp):
您可以使用此方法将UTC中的Datetime转换为用户的时间戳,如下所示

您需要导入这2个包

import datetime as DT
from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT as DF
在这之后

date = fields.Date(string = 'date', select=True)
time = fields.Many2one('period.time', string='Available time',select=True)
date_time = fields.Datetime(string='Datetime', compute="_compute")
description = fields.Char(string = 'Descroption', compute="_compute")

@api.onchange('date', 'time') #time is 08:00:00
def _compute(self):
       date_utc = DT.datetime.strptime(self.date, DF)
       date_tz = fields.Datetime.context_timestamp(self, date_utc)
       self.description = date_tz.strftime(DF)

时间是odoo?@emiprotechnologiesvt.Ltd中的哪种类型的字段。时间很长,你能写出你所在领域的完整代码吗?@emiprotechnologiesvt.Ltd。添加了我的帖子…你能为我的.time类编写类定义吗?我使用了很多代码,你可以让它变得更少。请参见openerp.fields.Datetime类你可以看到转换是如何发生的