Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
1 ตอบกลับ
4682 มุมมอง

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
3890
Time & Date Issue แก้ไขแล้ว
6
ก.ย. 19
11177
2
มี.ค. 16
7814
Datetime and Time Zones - OpenERP 7 แก้ไขแล้ว
1
ม.ค. 24
12649
0
มี.ค. 15
8104