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.

0

[Solved] Date/ Time in Odoo with python

By
Siobhan
on 5/29/15, 6:10 AM 3,741 views

I have an onchange class written :


@api.onchange('res_users_id')

def do_stuff(self):

if self.res_users_id:

self.beschreibung = str(now) + '\n' + str(self.beschreibung)

else:

self.beschreibung = ' '


and a global variable: now = datetime.datetime.now()

I have 2 problems:

1: it is showing about 2 hours behind my current time, how do I fix that? and

2: how do I get rid of the milliseconds at the end - 10:07:15.471708?


1

Temur

--Temur--
2894
| 6 5 7
Tbilisi, Georgia
--Temur--
Programmer
Temur
On 5/29/15, 8:49 AM

1. I guess you're in UTC+2 time zone? Take in account that server time is in UTC, but that's corrected according timezone of user (timezone of users is configured in settings), so in user interface  every user see time correctly in it's timezone (that's nice solution for a case if several user are logged into the same server from different timezone) consider to use datetime and date fields (also try widget="datetime" widget="date"), finally users will see correct time.

2. consider to use strftime / strptime play.


UPDATE for 1.

if you want add or subtract that 2 hours directly at server side (in python), then you can use (but take in account that it may show wrong time to users in browser, because of what I posted above...):

from datetime import timedelta

...

now = datetime.datetime.now() + timedelta(hours=2) 

...


UPDATE: one more option to get time in user timezone at serverside:

in your case, you may find useful to use server_to_local_timestamp function from openerp.tools.misc

from openerp import tools
...

src_tstamp_str = tools.datetime.now().strftime(tools.misc.DEFAULT_SERVER_DATETIME_FORMAT)

src_format = tools.misc.DEFAULT_SERVER_DATETIME_FORMAT

dst_format = "%c" #format you want to get time in.

dst_tz_name = self.env.user.tz # timezone you want get time in (here used v8 api, otherwise you'll have to browse for user and access tz field using "user_browserecord.tz")

_now = tools.misc.server_to_local_timestamp(src_tstamp_str, src_format, dst_format, dst_tz_name)

note: user should have configured a timezone in settings. if that's not a case, then odoo displays warning (!) at upper right corner in UI in browser.

Thank you for the information... I am trying to use %c Locale’s appropriate date and time representation. but it doesn't like the syntax... what is the correct way to use this?

Siobhan
on 6/1/15, 2:41 AM

Is there a way to use timedelta in this piece of code: now = strftime("%a, %d %b %Y %H:%M:%S ", localtime())

Siobhan
on 6/1/15, 3:13 AM

Ok.. got it down to one line of code: now = datetime.datetime.now().strftime("%c") Problem is that I am again 2 hours out even when my time preference in Odoo is set to Central European time.

Siobhan
on 6/1/15, 4:22 AM

you can turn your code into the:

now = ( datetime.datetime.now() + timedelta(hours=2) ).strftime("%c")
this way you can add back that 2 hours difference to the current time. also consider to use another option that I've added to my answer, choose one which better fits your requirements.
Temur
on 6/1/15, 5:39 AM

Thank you Temur... timedelta is perfect for now as everyone is in Central Europe so I don't need timezone. Will look at the code above and try get it to work during my quiet time.

Siobhan
on 6/2/15, 1:59 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

1 follower(s)

Stats

Asked: 5/29/15, 6:10 AM
Seen: 3741 times
Last updated: 6/2/15, 1:59 AM