Help

3

[Solved] Date/ Time in Odoo with python

Avatar
Siobhan

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?


アバター
Discard
2 回答
8
Avatar
Temur
Best Answer

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.
6 コメント
アバター
Discard
Avatar
Siobhan
-

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?

Avatar
Siobhan
-

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

Avatar
Siobhan
-

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.

Avatar
Temur
-

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.
Avatar
Siobhan
-

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.

Avatar
Carlos A García
-

Thanks, you are the best, exactly what I need! Cheers

1
Best Answer

Hi, I know this post is already solved but I has the same issues on Odoo 11, 12 and in my case I use fields.Datetime.context_timestamp , like this:

now= datetime.strftime(fields.Datetime.context_timestamp(self, datetime.now()), "%Y-%m-%d %H:%M:%S")

you need to declare it in on methode on models.Models, because the self take the timezone of the current user


アバター
Discard