Python 2.6 and timezones

L

loial

Does python have an equivalent of the java Timezone object?

I need to be able to get offsets for timezones (only U.S. time zones
at the moment)
 
D

Daniel Kluev

Does python have an equivalent of the java Timezone object?

I need to be able to get offsets for timezones (only U.S. time zones
at the moment)

Depends on what exactly do you want. If you need to convert timezone
name into current offset, you should use [1] or [2].
If you just need to handle known offsets for datetime objects, there
is tzinfo class in datetime module, [3].


[1] http://pypi.python.org/pypi/PosixTimeZone/0.9.4
[2] http://pypi.python.org/pypi/pytz/2011g
[3] http://docs.python.org/library/datetime.html#tzinfo-objects
 
L

loial

Thanks...but being a python newbie I am struggling to understand how
to do this.

How can I use tzinfo to do the equivalent of what I do in Java, which
is :

TimeZone tz1 = TimeZone.getDefault();

long localOffset = tz1.getOffset(date.getTime());

TimeZone tz2 = TimeZone.getTimeZone("EST");

long remoteOffset = tz2.getOffset(date.getTime());

Any help appreciated



Does python have an equivalent of the java Timezone object?
I need to be able to get offsets for timezones (only U.S. time zones
at the moment)

Depends on what exactly do you want. If you need to convert timezone
name into current offset, you should use [1] or [2].
If you just need to handle known offsets for datetime objects, there
is tzinfo class in datetime module, [3].

[1]http://pypi.python.org/pypi/PosixTimeZone/0.9.4
[2]http://pypi.python.org/pypi/pytz/2011g
[3]http://docs.python.org/library/datetime.html#tzinfo-objects
 
D

Daniel Kluev

Thanks...but being a python newbie I am struggling to understand how
to do this.

How can I use tzinfo to do the equivalent of what I do in Java, which
is :

TimeZone tz1 = TimeZone.getDefault();

long localOffset = tz1.getOffset(date.getTime());

TimeZone tz2 = TimeZone.getTimeZone("EST");

long remoteOffset = tz2.getOffset(date.getTime());

time.timezone returns local timezone in seconds and negative sign.
FixedOffset converts it into tzinfo object.
datetime.timedelta(0, 36000)

utcoffset() returns timedelta object as offset. It requires datetime
object as first parameter due to weird API of base tzinfo class, but
it is not used in calculation, and you can pass any other object,
including None instead, like `local_tz.utcoffset(None)`
datetime.timedelta(-1, 68400)

You can add or substract these timedelta objects directly from
datetime objects or use astimezone():
datetime.datetime(2011, 5, 23, 7, 41, 48, 398685, tzinfo=<StaticTzInfo 'EST'>)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top