Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
4711 Lượt xem

When i am saving a expire date time on form data in db by the help of Create( ) of a form as follows at that time db stores an other expire date time..please suggest what to do.. In the form view/Tree view , it shows current date time as input but not in DB.

@api.model 
def create(self, vals):
otp_value = ''.join(random.choice(string.digits) for _ in range(4)) 
current_date_time = datetime.datetime.now() 
date_time = current_date_time + datetime.timedelta(0,600) 
new_record = super().create({'mobile_no':vals['mobile_no'],'otp':otp_value,'exp_date_time':date_time}) return new_record


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,

The thing is that Odoo stores the date-time in the database in UTC time, when it is displayed in the front end, the time will be displayed in the users set timezone. As odoo is multi user system, there will be users in the same database from the different timezones, to make the things easier odoo have used this way.


Suppose if you are here in India, your user timezone will be Asia/Kolkata , ie, +5:30 UTC. And if you save a date-time value 26/06/2019 6:30 PM in your custom field, in the database it will get stored in the UTC as 26/06/2019 1:00 PM(by subtracting the +5:30 from the time).

And when the same field has to be displayed in the front end from the database, the UTC time from the database is taken and displayed according to the users timezone. So when displaying the same field to the user with the timezone as Asia/Kolkata it will be 26/06/2019 6:30 PM, and for another user with timezone Saudi Arabia which is +3 , the value will be 26/06/2019 1:00 pm + 3 hours, ie 26/06/2019 4:00 PM.


In the database 26/06/2019 1:00 PM,

for user 1 with timezone +5:30 , time will be 26/06/2019 06:30 pm.

for user2 with timezone +3:00, time will be 26/06/2019 04:00 pm.


Thanks

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

Hii, Niyas Raphy 

The thing is that , i am trying to match the current datetime to the stored datetime in python to postgres sql for otp verification ..how should i do ??

thanks in advance..

Ảnh đại diện
Huỷ bỏ

Once you fetch the data from the db, if you are getting it in the UTC, you can convert it to the local time by adding timezone value to it. See: https://www.odoo.com/forum/help-1/question/how-to-convert-utc-datetime-to-local-118994

Tác giả

Thank you

Tác giả

Hello Niyas, this suggestion is not working..Its showing an error in terminal that if zone.upper() == 'UTC':

Bài viết liên quan Trả lời Lượt xem Hoạt động
3
thg 10 23
7427
1
thg 9 23
2668
1
thg 5 23
1606
2
thg 4 23
2025
1
thg 3 23
1556