Skip to Content
Menu
This question has been flagged
1 Reply
3522 Views

In my odoo module I have a datetime field (prefered_date). What I want to do is that, no matter what time the user enters, the time is always set 10 am. I tried to do it with the following code. but is not working. The time is setting to 6 am instead of 10, Maybe it has something to do with timezone. What am I doing wrong?

@api.multi
def write(self, values):
    if 'prefered_date' in values:
        date = datetime.strptime(values.get('prefered_date'), '%Y-%m-%d %H:%M:%S')
        newdate = date.replace(hour=10, minute=0)
        new = newdate.strftime("%Y-%m-%d %H:%M:%S")
        values['prefered_date'] = new 
    return super(PostabilidadRequest, self).write(values)


Avatar
Discard
Best Answer

Hello Ernesto Ruiz,

Find code in Comment. 

Hope it will help you. 

Thanks & Regards,
Email: odoo@aktivsoftware.com
Skype: kalpeshmaheshwari

Avatar
Discard

You will need to convert the datetime to the timezone. Refer to the code:

def write(self, values):
if 'qa_deadline' in values:
date = datetime.strptime(values.get('qa_deadline'), '%Y-%m-%d %H:%M:%S')
user_date = datetime.combine(date.date(), time(10))
user_tz = pytz.timezone(self.env.context.get("tz") or self.enc.user.tz)
to_user = user_tz.localize(user_date)
tz_utc = pytz.timezone('UTC')
utc_time = to_user.astimezone(tz_utc)
utc_time = utc_time.replace(tzinfo=None)
values['qa_deadline'] = utc_time
return super(ProjectTask, self).write(values)

Related Posts Replies Views Activity
1
Jan 23
1972
6
Sep 19
9023
2
Mar 16
6517
0
Mar 15
6952
1
Jan 24
11529