Reputation: 2358
After concatenate date and time field, and result put in new datetime field get 2 hours more than necessary
Example:
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):
self.date_time = "%s %s:00" % (self.date,self.time.name)
self.description = "%s %s:00" % (self.date,self.time.name)
date_time (Datetime) 2017-04-10 10:00:00 --> Wrong
description (Char) 2017-04-10 08:00:00 --> Correct
How fix this issue?
Upvotes: 0
Views: 3808
Reputation: 1155
In the odoo class datetime you can find for the class Datetime the method,
def context_timestamp(record, timestamp):
You can use this method to convert you Datetime in UTC to the timestamp of the user like this.
You need import this 2 packages
import datetime as DT
from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT as DF
and after do this.
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)
Upvotes: 0
Reputation: 14731
This is not wrong from odoo side because odoo save date in UTC.When the user request a date odoo convert the date from the database to the timezone of the users.
so if you go to the database you will find that the field is stored date = 2017-04-10 08:00:00
but in the view the date is converted to timezone of the user in your case is converted to 2017-04-10 10:00:00
so before saving the date make sure that you convert the value to UTC
datetime i mean don't save 2017-04-10 08:00:00
to date_time but save 2017-04-10 06:00:00
EDITS : Try this i hope it work id didn't use it but i think it will work if there is any error just put it here:
# 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)
Upvotes: 1