Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 613
OmaL Avatar asked Nov 20 '12 12:11

OmaL


People also ask

How do I get different time zones in Python?

Use the datetime. astimezone() method to convert the datetime from one timezone to another. This method uses an instance of the datetime object and returns a new datetime of a given timezone.

What does pytz UTC localize do?

pytz. localize() is useful for making a naive timezone aware. it is useful when a front-end client sends a datetime to the backend to be treated as a particular timezone (usually UTC).

How do I convert datetime from one time zone to another in Python?

Get the current time using the datetime. now() function and pass the above second−time zone i.e 'US/Eastern' time zone as an argument to it(Here it converts the current date and time to the 'US/Eastern' timezone). Use the strftime() function to format the above datetime object and print it.


2 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/

like image 137
OmaL Avatar answered Oct 21 '22 13:10

OmaL


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.

like image 22
odony Avatar answered Oct 21 '22 13:10

odony