Odoo Help


convert datetime to float - float_time

on 7/12/13, 1:35 PM 5,370 views

I used a float field with widget "float_time" to display a time. How I can convert datetime to float to display correctly in the widget?.

I explain a little more my scenario,

I have a field field 'start_date' which is datetime, I want that in the event "on_change" I set the time value in another field of type float that has the widget "float_time".

 'start_date': fields.datetime('Fecha Inicio' , required = True),
 'h_inicio': fields.float('Hora inicio', help="Hora inicio" , required = True),

In view :

<field name="start_date" on_change="onchange_f_inicio(start_date)"/>
<field name="h_inicio" widget="float_time"/>

Method on_change:

    def onchange_f_inicio(self, cr, uid, ids ,fecha_inicio):
    res = {'value':{}}
    fecha_ini = datetime.strptime(fecha_inicio, "%Y-%m-%d %H:%M:%S")

   user = self.pool.get('res.users').browse(cr, uid, uid)
   tz = pytz.timezone(user.tz) if user.tz else pytz.utc
   fecha_ini = pytz.utc.localize(fecha_ini).astimezone(tz)     # convert start in user's timezone           

    hora_inicio = "%s.%s" % (str(fecha_ini.hour) , str(fecha_ini.minute))       
    res['value']['h_inicio'] =  round(float(hora_inicio),1)
    return res

The only way that has worked is to convert the date 'start_date' in a string and then convert to a float to be the value returned by the method on_change.

This form does not think it's the best, if I can help create a better way to thank you.


Jairo Llopis
On 6/19/15, 6:44 AM

First, import the needed classes:

from datetime import time, timedelta

Then, on your method:

float_field_time = time.min + timedelta(seconds=self.float_field * 60 * 60)
Grahame Jordan
On 7/14/13, 7:58 PM

You can call the util function in the client code float_time_convert()

And where's that function?

Jairo Llopis
on 6/19/15, 3:56 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

0 follower(s)


Asked: 7/12/13, 1:35 PM
Seen: 5370 times
Last updated: 6/19/15, 6:44 AM