OmaL
OmaL

Reputation: 5044

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

In OpenERP, when I try to print the current date and time, it always print the 'UTC' time. But I want to get time in the user timezone . Each user have different timezone.For example 'CST6CDT', 'US/Pacific' or 'Asia/Calcutta'. So I need to get time in user timezone so that I can show the correct datetime in the report. I have tried to change the timezone using localize() and replace() function in datatime module. But I didn't get the correct output.

Upvotes: 9

Views: 15401

Answers (4)

Phuc Tran
Phuc Tran

Reputation: 160

DateInUTC = <~ Time variable to convert

To convert to user's timezone:

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

To remove the offset:

LocalizedDate = LocalizedDate.replace(tzinfo=None)

Upvotes: 0

mtoloo
mtoloo

Reputation: 1875

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's 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)

Upvotes: 0

odony
odony

Reputation: 4117

As of OpenERP 6.1 the timezone of all Python operations happening on the server-side (and in modules) is forced to be UTC. This was a design decision explained in various places [1]. The rendering of datetime values in the user's timezone is meant to be done on the client-side exclusively.

There are very few cases where it makes sense to use the user's timezone instead of UTC on the server-side, but indeed printing datetime values inside reports is one of them, because the client-side will have no chance to convert the contents of the resulting report.

That's why the report engine provides a utility method for doing so: the formatLang() method that is provided in the context of reports (RML-based ones at least) will format the date according to the timezone of the user if you call it with a datetime value and with date_time=True (it uses the tz context variable passed in RPC calls and based on the user's timezone preferences) You can find example of how this is used in the official addons, for example in the delivery module (l.171).

Have a look at the implementation of formatLang() if you want to know how it actually does the conversion.

[1]: See the OpenERP 6.1 release notes, this other question, as well as comment #4 on bug 918257 or bug 925361.

Upvotes: 5

OmaL
OmaL

Reputation: 5044

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/

Upvotes: 12

Related Questions