错误日期和时间 odoo v8



当我打印报告时,我总是弄错时间(-1 小时(,我不知道如何解决这个问题。

我的代码中有这个函数:

def _interpolation_dict(self):
t = time.localtime() # Actually, the server is always in UTC.
return {
'year': time.strftime('%Y', t),
'month': time.strftime('%m', t),
'day': time.strftime('%d', t),
'y': time.strftime('%y', t),
'doy': time.strftime('%j', t),
'woy': time.strftime('%W', t),
'weekday': time.strftime('%w', t),
'h24': time.strftime('%H', t),
'h12': time.strftime('%I', t),
'min': time.strftime('%M', t),
'sec': time.strftime('%S', t),
}

您需要将UTC时区转换为用户时区

您可以使用以下方法进行操作。

from datetime import datetime
import pytz
time_zone=self.env.user.tz
if time_zone:
local_now = datetime.now(pytz.timezone(time_zone))
else:
local_now=datetime.now()
return {
'year': local_now.strftime('%Y'),
'month': local_now.strftime('%m'),
'day': local_now.strftime('%d'),
'y': local_now.strftime('%y'),
'doy': local_now.strftime('%j'),
'woy': local_now.strftime('%W'),
'weekday': local_now.strftime('%w'),
'h24': local_now.strftime('%H'),
'h12': local_now.strftime('%I'),
'min': local_now.strftime('%M'),
'sec': local_now.strftime('%S'),
}    

在上面的方法中,我们使用datetime.now((获取UTC时区之后使用pytz将UTC时区转换为用户时区 函数

这可能会对您有所帮助。

最新更新