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] Always returning UTC for datetime.now() regardless of my timezone- Odoo8

By
Siobhan
on 6/8/15, 5:15 AM 1,042 views

Have a piece of code in python: now = datetime.datetime.now().strftime("%c")

when I run it in python I get this :


>>> import datetime, time; now = datetime.datetime.now().strftime("%c"); print now

Mon Jun 8 10:52:59 2015

This is correct

However when I run in Odoo (regardless of my timezone) it always shows UTC which is 2 hours behind.

Any advise?

0
Siobhan
On 6/9/15, 3:05 AM

Had to look at a lot of code to get this to work and lots of testing but we have a solution:


user_pool = self.pool.get('res.users')

user = user_pool.browse(cr, SUPERUSER_ID, uid)

from_zone = pytz.timezone(user.partner_id.tz) or pytz.utc

to_zone = tz.gettz(str(from_zone))

utc = datetime.datetime.now()

utc = utc.replace(tzinfo=tz.tzutc())

now = datetime.datetime.now(tz.gettz('Europe/Berlin')).strftime('%c')

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: 6/8/15, 5:15 AM
Seen: 1042 times
Last updated: 6/9/15, 3:05 AM