This question has been flagged

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.

thanks

Avatar
Discard
Best Answer

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)
Avatar
Discard
Best Answer

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

Avatar
Discard

And where's that function?