Odoo Help

Welcome!

This community 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.

1

convert datetime to float - float_time

By
pepito
on 7/12/13, 1:35 PM 3,372 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.

thanks

0
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)
0
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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

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