This question has been flagged
2 Replies
2257 Views

In odoo14, I have made an API to put the attendance but it creates the attendance as it is two hours later, for example, I create it at 9 pm it appears as it is 11 pm  but in the database, the time is as I want


creation code:

    def format_time(self,time_str):
        return fields.datetime.strptime(time_str, '%m/%d/%Y %I:%M:%S %p')
    
    @http.route('/create_attendance',type='json',auth='none',cors='*')
    def index(self,check_in,check_out,user_name,base_location=None):
        att_env = request.env['hr.attendance']
        employee_id = request.env['hr.employee'].sudo().search([('name','=',user_name)])[0].id
        check_in_time = self.format_time(check_in)
        check_out_time = self.format_time(check_out)
        new_att = att_env.sudo().create({
            'employee_id':employee_id,
            'check_in':check_in_time,
            'check_out':check_out_time
        })
        return new_att.check_out # returns what i want but in the ui it's wrong
Avatar
Discard
Best Answer

Hi,

The 2 hr difference is the timezone difference. The date time will be saving in the database as in UTC and shown in the user interface by adding/subtracting the timezone difference. So while saving the date time to database, change it to the UTC and save.

Reference:

  1. Why Datetime is Different In Database and in User Interface Odoo

  2. How To Convert Datetime To Users TimeZone In Odoo


Thanks

Avatar
Discard
Author Best Answer

Ok I knew why the problem happens but I didn't found any solution already all my user are from one timezone and the date is coming to me from this timezone so I am asking is there a way to make the UI display the time in UTC or to make it understand that the time is stored in specific timezone?

Avatar
Discard