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


Groups > comp.lang.python > #69581

Re: Calculating time differences given daylight savings time

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <kwpolska@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.069
X-Spam-Evidence '*H*': 0.86; '*S*': 0.00; 'argument': 0.05; 'linux,': 0.07; 'utf-8': 0.07; 'historic': 0.09; 'whichever': 0.09; 'windows,': 0.09; '\xe2\x80\x94': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '2.7': 0.14; 'windows': 0.15; '###': 0.16; 'happily': 0.16; 'imports': 0.16; 'i\xe2\x80\x99m': 0.16; 'objects*.': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'example': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'delta': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'skip:p 30': 0.29; 'am,': 0.29; 'database,': 0.30; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'seemingly': 0.31; 'url:python': 0.33; 'running': 0.33; 'subject:time': 0.33; 'sense': 0.34; 'something': 0.35; 'objects': 0.35; 'usual': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'object,': 0.36; 'url:listinfo': 0.36; 'url:org': 0.36; 'should': 0.36; 'thank': 0.38; 'anything': 0.39; '12,': 0.39; 'url:mail': 0.40; 'how': 0.40; 'future': 0.60; 'from:charset:utf-8': 0.61; 'you.': 0.62; 'taking': 0.65; 'account': 0.65; 'skip:\xe2 10': 0.65; 'between': 0.67; 'to:addr:aol.com': 0.68; 'url:v': 0.71; 'url:youtube': 0.71; '8bit%:43': 0.74; 'url:watch': 0.77; 'savings': 0.81; 'it\xe2\x80\x99s': 0.84; 'ships': 0.84; 'subject:savings': 0.84; 'suits': 0.84; 'tricky': 0.84; 'do:': 0.91; 'why?': 0.91; 'washington': 0.93; 'url:tk': 0.95; 'yourself,': 0.95
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=AYWGKxpm6AjBlhAqqzaOAzv24rWKohcl9BNNpO6oePw=; b=pBDyT1cjWnLWjdM5ZKNe45v6vQ09Jw1XrDbuADgjmzD8C0FwG60Cas21dxgM8WjPyi Xy9vMsAQerwS4/OUwht6fAQtsleDyTwV5WqNmdaqE9Toe+qzzOn55zckP4BpGWKuPnu1 Bn5K3sw0MI2n3SkQ8tg9PTqv0m8XpYevPTNqRHm9qNqAs6m8+XKCElwcRdRpAc8alfuD IzFq9TVlKlp+VLCUqTjAooh/JqgvOJbif+hwzCwZDrB3+TMzedJwKnPK6r9Y4WudlwIT DHpDzrJjlPqIP+2hQZzYlRjzWj/qb6uszGqjR1v9moU3PwZFbg/ohM+NDTzeyCVcylm3 ISEA==
MIME-Version 1.0
X-Received by 10.50.66.143 with SMTP id f15mr15626756igt.18.1396532810245; Thu, 03 Apr 2014 06:46:50 -0700 (PDT)
In-Reply-To <c6debf13-71a2-41ed-80a6-82008d74b55c@googlegroups.com>
References <c6debf13-71a2-41ed-80a6-82008d74b55c@googlegroups.com>
Date Thu, 3 Apr 2014 15:46:50 +0200
Subject Re: Calculating time differences given daylight savings time
From Chris “Kwpolska” Warrick <kwpolska@gmail.com>
To Washington Ratso <jobhunts02@aol.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.8835.1396532818.18130.python-list@python.org> (permalink)
Lines 56
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1396532818 news.xs4all.nl 2839 [2001:888:2000:d::a6]:38921
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:69581

Show key headers only | View raw


On Thu, Apr 3, 2014 at 3:45 AM, Washington Ratso <jobhunts02@aol.com> wrote:
> I am running Python 2.7 and would like to be able to calculate to the second the time difference between now and some future date/time in the same timezone while taking into account daylight savings time.  I do not have pytz.  Any ideas how to do it?
>
> If it requires having pytz, how would I do it with pytz?
>
> Thank you.
> --
> https://mail.python.org/mailman/listinfo/python-list

It requires having pytz, or dateutil, in order to get timezone
objects*.  You can also create those objects yourself, but that’s
tricky — and you SHOULD NOT do time zones on your own and just use
something else.  Why?  See [0].

Example with pytz:

# Necessary imports
import pytz
from datetime import datetime

# the timezone in this example is Europe/Warsaw — use your favorite one
tz = pytz.timezone('Europe/Warsaw')

now = datetime.now(tz)
# Note I’m not using the tzinfo= argument of datetime(), it’s flaky
and seemingly uses a historic WMT (+01:24) timezone that is dead since
1915.
future = tz.localize(datetime(2014, 12, 24))

# And now you can happily do:

delta = future - now

# and you will get the usual timedelta object, which you can use the usual way.


###

* pytz ships the Olson tz database, while dateutil maps uses your
system’s copy of the tz database (if you are on Linux, OS X or
anything else that is not Windows, really) or maps to the Windows
registry.  Use whichever suits you.

[0]: http://www.youtube.com/watch?v=-5wpm-gesOY

-- 
Chris “Kwpolska” Warrick <http://kwpolska.tk>
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense

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


Thread

Calculating time differences given daylight savings time Washington Ratso <jobhunts02@aol.com> - 2014-04-02 18:45 -0700
  Re: Calculating time differences given daylight savings time Chris “Kwpolska” Warrick <kwpolska@gmail.com> - 2014-04-03 15:46 +0200

csiph-web