How to get user local timezone other than server timezone (UTC) in python?

In OpenERP, when I try to print the current date and time, it always prints UTC time. But I want to get the time in a custom time zone. Each user has a different time zone. For example 'CST6CDT' , 'US / Pacific' or 'Asia / Calcutta'. Therefore, I need to find the time in the user time zone so that I can show the correct time in the report. I tried changing the time zone using the localize () and replace () functions in the datatime module. But I did not get the correct result.

+6
source share
4 answers

Got it.

 from datetime import datetime from pytz import timezone fmt = "%Y-%m-%d %H:%M:%S" # Current time in UTC now_utc = datetime.now(timezone('UTC')) print now_utc.strftime(fmt) # Convert to US/Pacific time zone now_pacific = now_utc.astimezone(timezone('US/Pacific')) print now_pacific.strftime(fmt) # Convert to Europe/Berlin time zone now_berlin = now_pacific.astimezone(timezone('Europe/Berlin')) print now_berlin.strftime(fmt) 

Courtesy: http://www.saltycrane.com/blog/2009/05/converting-time-zones-datetime-objects-python/

+9
source

As of OpenERP 6.1, the time zone of all Python operations occurring on the server side (and in the modules) is forced to be UTC. It was a constructive solution, explained in different places [1] . The countdown of the date and time values ​​in the user time zone is intended exclusively for the client side.

There are very few cases where it makes sense to use the user's time zone instead of UTC on the server side, but, indeed, printing the date and time values ​​inside the reports is one of them, since the client side will not be able to convert the contents of the final report.

To do this, the reporting mechanism provides a utility method for this: the formatLang() method, which is provided in the context of reports (at least based on RML), will format the date in accordance with the user's time zone if you call it datetime and date_time=True (it uses the tz context variable passed in RPC calls and based on user time zone preferences) You can find an example of how this is used in official add-ons, for example, in the delivery module (l.171) .

Check out the formatLang() implementation if you want to know how the conversion actually does it.

[1]: see OpenERP release notes for 6.1, this other question , and also comment No. 4 on bug 918257 or bug 925361 .

+3
source

from: http://help.openerp.com/question/30906/how-to-get-users-timezone-for-python/

 DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S" import pytz from openerp import SUPERUSER_ID # get user timezone user_pool = self.pool.get('res.users') user = user_pool.browse(cr, SUPERUSER_ID, uid) tz = pytz.timezone(user.context_tz) or pytz.utc # get localized dates localized_datetime = pytz.utc.localize(datetime.datetime.strptime(utc_datetime,DATETIME_FORMAT)).astimezone(tz) 
0
source

DateInUTC = <~ Temporary variable for conversion

To convert to user timezone:

 LocalizedDate = fields.datetime.context_timestamp(cr, uid, DateInUTC, context=context) 

To remove an offset:

 LocalizedDate = LocalizedDate.replace(tzinfo=None) 
0
source

All Articles