Skip to Content
Меню
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
Це запитання позначене
1 Відповісти
4680 Переглядів

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)


Аватар
Відмінити
Найкраща відповідь

Hello Ernesto Ruiz,

Find code in Comment. 

Hope it will help you. 

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

Аватар
Відмінити

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 Відповіді Переглядів Дія
Take Users Timezone Вирішено
3
лип. 25
3883
Time & Date Issue Вирішено
6
вер. 19
11175
2
бер. 16
7814
1
січ. 24
12648
0
бер. 15
8104