Python 2.6 and timezones

Discussion in 'Python' started by loial, May 23, 2011.

  1. loial

    loial Guest

    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)
    loial, May 23, 2011
    #1
    1. Advertising

  2. loial

    Daniel Kluev Guest

    On Mon, May 23, 2011 at 9:32 PM, loial <> wrote:
    > 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

    --
    With best regards,
    Daniel Kluev
    Daniel Kluev, May 23, 2011
    #2
    1. Advertising

  3. loial

    loial Guest

    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



    On May 23, 11:48 am, Daniel Kluev <> wrote:
    > On Mon, May 23, 2011 at 9:32 PM, loial <> wrote:
    > > 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
    >
    > --
    > With best regards,
    > Daniel Kluev
    loial, May 23, 2011
    #3
  4. loial

    Daniel Kluev Guest

    On Mon, May 23, 2011 at 10:56 PM, loial <> wrote:
    > 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());
    >


    >>> from pytz import timezone, FixedOffset
    >>> import time
    >>> from datetime import datetime
    >>> local_tz = FixedOffset(-time.timezone/60)


    time.timezone returns local timezone in seconds and negative sign.
    FixedOffset converts it into tzinfo object.

    >>> now = datetime.now()
    >>> local_tz.utcoffset(now)

    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)`

    >>> remote_tz = timezone("EST")
    >>> remote_tz.utcoffset(now)

    datetime.timedelta(-1, 68400)

    You can add or substract these timedelta objects directly from
    datetime objects or use astimezone():

    >>> now = datetime.now(local_tz)
    >>> now

    datetime.datetime(2011, 5, 23, 22, 41, 48, 398685, tzinfo=pytz.FixedOffset(600))
    >>> now.astimezone(remote_tz)

    datetime.datetime(2011, 5, 23, 7, 41, 48, 398685, tzinfo=<StaticTzInfo 'EST'>)


    --
    With best regards,
    Daniel Kluev
    Daniel Kluev, May 23, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ryan Ternier
    Replies:
    1
    Views:
    1,467
    Ryan Ternier
    Oct 14, 2005
  2. Replies:
    2
    Views:
    4,433
  3. Timezones in python

    , Dec 5, 2007, in forum: Python
    Replies:
    5
    Views:
    1,249
  4. Python and timezones

    , Jul 2, 2008, in forum: Python
    Replies:
    3
    Views:
    608
  5. Chris Angelico
    Replies:
    0
    Views:
    70
    Chris Angelico
    Jan 31, 2014
Loading...

Share This Page