Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #6071

Re: Python 2.6 and timezones

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <dan.kluev@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'newbie': 0.03; 'parameter': 0.05; 'subject:Python': 0.06; 'python': 0.08; 'object.': 0.09; 'pm,': 0.10; 'api': 0.11; '>>>': 0.12; 'received:209.85.214.174': 0.14; 'received:mail- iw0-f174.google.com': 0.14; 'skip:f 30': 0.14; 'wrote:': 0.14; '48,': 0.16; 'sign.': 0.16; 'class,': 0.16; 'seconds': 0.16; 'mon,': 0.17; 'object,': 0.19; 'java,': 0.19; 'header:In-Reply- To:1': 0.21; 'converts': 0.23; 'skip:` 20': 0.23; 'objects': 0.23; 'object': 0.26; 'pass': 0.27; 'message-id:@mail.gmail.com': 0.28; 'received:209.85.214': 0.28; 'import': 0.29; 'equivalent': 0.31; 'this.': 0.31; 'to:addr:python-list': 0.33; 'daniel': 0.34; 'weird': 0.35; 'none': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.37; 'instead,': 0.37; 'but': 0.38; 'subject:: ': 0.38; 'received:209': 0.39; 'add': 0.39; 'to:addr:python.org': 0.39; 'best': 0.60; 'due': 0.67; '10:56': 0.84; 'datetime': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=1jeuPrhLZbE15ghYVvZcx1yrDi1L2glagcb5GZKEiSc=; b=HknYVTSHEVQW0qCEZf+h0FpiN+7GV246uN16lqdIdCKeIe8ujrhUfrL7gyFIN52z5b UhZKb7AwNjxHAkBkHamlx3jOSKM4zj0ASR9ZjeI4AMqtx8RMitzRYYNr41weRub1KbUY vDFV6sKrQY3TEbAmW6JU3MOcdwCZmFtEcBCNE=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=xp3hsWQvoykBn3mQQ1KrPpp4M708iamXsOuGXPhSOAwHHc7rsmD+6mb3hMtQ8V0E4m CjPl3NG/zFNrxjelt1Y3b24SQfrfDNzSBM0DMK4Xr1VSUewwTIotdsD9oJI667s+djQZ NozQELqyB8X0+dSxDLa4LPX1ABvdK4Er9S2mg=
MIME-Version 1.0
In-Reply-To <932c351d-0dbd-4d5b-8151-e695b4b83fe1@z37g2000vbl.googlegroups.com>
References <46c24750-ac9b-4d75-835c-5403ff2ea959@d28g2000yqf.googlegroups.com> <mailman.1971.1306147689.9059.python-list@python.org> <932c351d-0dbd-4d5b-8151-e695b4b83fe1@z37g2000vbl.googlegroups.com>
Date Mon, 23 May 2011 23:33:40 +1100
Subject Re: Python 2.6 and timezones
From Daniel Kluev <dan.kluev@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1975.1306154023.9059.python-list@python.org> (permalink)
Lines 50
NNTP-Posting-Host 82.94.164.166
X-Trace 1306154023 news.xs4all.nl 49039 [::ffff:82.94.164.166]:38682
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:6071

Show key headers only | View raw


On Mon, May 23, 2011 at 10:56 PM, loial <jldunn2000@gmail.com> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Python 2.6 and timezones loial <jldunn2000@gmail.com> - 2011-05-23 03:32 -0700
  Re: Python 2.6 and timezones Daniel Kluev <dan.kluev@gmail.com> - 2011-05-23 21:48 +1100
    Re: Python 2.6 and timezones loial <jldunn2000@gmail.com> - 2011-05-23 04:56 -0700
      Re: Python 2.6 and timezones Daniel Kluev <dan.kluev@gmail.com> - 2011-05-23 23:33 +1100

csiph-web