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


Groups > comp.lang.python > #87914

Re: Daylight savings time question

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <drsalists@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.014
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; '(so': 0.07; 'dan': 0.09; 'hour.': 0.09; 'main()': 0.09; 'subject:question': 0.10; 'def': 0.12; '24,': 0.16; '__future__': 0.16; 'jumping': 0.16; 'jumps': 0.16; 'main():': 0.16; 'numerically': 0.16; 'skip:0 40': 0.16; 'to:name:python list': 0.16; 'wrote:': 0.18; 'library': 0.18; 'skip:f 30': 0.19; 'later': 0.20; 'appears': 0.22; 'import': 0.22; 'spring': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'skip:p 30': 0.29; 'message- id:@mail.gmail.com': 0.30; 'getting': 0.31; 'clock': 0.31; 'thanks!': 0.32; 'skip:# 10': 0.33; 'subject:time': 0.33; 'skip:d 20': 0.34; "i'd": 0.34; 'received:google.com': 0.35; 'there': 0.35; 'pacific': 0.36; 'ahead': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'march': 0.61; 'love': 0.65; 'forward': 0.65; 'hours': 0.66; 'mar': 0.68; 'savings': 0.81; '2015': 0.84; 'back?': 0.84; 'skip:w 60': 0.84; 'subject:savings': 0.84
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; bh=LLSGaDBGtVeC/oN2m4Y60eqt/joxpjI9uXKny/41WAk=; b=SP9TFCZVKtHvaWEgfT9wXEfWlARKrbpv1ru6xKwliot+ocbdhMeqNUWNmzOaPEq6dv ptCaVNg7nlPtRElX6tm4KqvnV5uIxOC4cWSinQwohSCNLrCf6KoBr/BZnoCvmdJu5iL+ S5bklfGl4lduBfdYrmI1CrBiPY6HBM/sLnFyZz1KmQglD4QNp/7RL0ZPEFdgbJqdBbCu zNzagBgL5tXNtgjWIDa4X93A+X07bAD8ooFZ8fR4AbqrgpHE5E2UEhGr1QMxSjkx0szf wjnRQAD4aIRkyWInNqJl4BO2y/Si5C44sS2VTp62oxBidYoOy3HeK1/1MRG8mG17LFyS MiFA==
MIME-Version 1.0
X-Received by 10.152.5.6 with SMTP id o6mr5970010lao.59.1427241134085; Tue, 24 Mar 2015 16:52:14 -0700 (PDT)
In-Reply-To <CAGGBd_pFpemgisb-2WFQvu8MehQfHhUJ7=n9CyYFxjj56xOV_g@mail.gmail.com>
References <CAGGBd_pFpemgisb-2WFQvu8MehQfHhUJ7=n9CyYFxjj56xOV_g@mail.gmail.com>
Date Tue, 24 Mar 2015 16:52:14 -0700
Subject Re: Daylight savings time question
From Dan Stromberg <drsalists@gmail.com>
To Python List <python-list@python.org>
Cc dan_stromberg@intuit.com
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.19
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.124.1427241135.10327.python-list@python.org> (permalink)
Lines 61
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1427241135 news.xs4all.nl 2938 [2001:888:2000:d::a6]:47959
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:87914

Show key headers only | View raw


This appears to do what I wanted:

#!/usr/bin/python

from __future__ import print_function

import pytz
import datetime

# Is there a good way of jumping ahead 5 hours instead of 4 on 2015-03-08?

def main():
    # On 2015-03-08, 2:00 AM to 2:59AM Pacific time does not exist -
the clock jumps forward an hour.
    us_pacific = pytz.timezone('US/Pacific')

    weird_naive_datetime = datetime.datetime(2015, 3, 8, 1, 0, 0)
    print('weird_naive_datetime:  ', weird_naive_datetime)

    weird_tz_aware_datetime = us_pacific.localize(weird_naive_datetime)
    print('weird_tz_aware_datetime', weird_tz_aware_datetime)

    four_hours=datetime.timedelta(hours=4)
    print('Four hours later is:   ',
us_pacific.normalize(weird_tz_aware_datetime + four_hours))

    print('...we want numerically 5 hours later (so 6AM), because of
Daylight Savings Time')

main()


On Tue, Mar 24, 2015 at 3:24 PM, Dan Stromberg <drsalists@gmail.com> wrote:
> Is there a way of "adding" 4 hours and getting a jump of 5 hours on
> March 8th, 2015 (due to Daylight Savings Time), without hardcoding
> when to spring forward and when to fall back?  I'd love it if there's
> some library that'll do this for me.
>
> #!/usr/bin/python
>
> import pytz
> import datetime
>
> def main():
>     # On 2015-03-08, 2:00 AM to 2:59AM Pacific time does not exist -
> the clock jumps forward an hour.
>     weird_naive_datetime = datetime.datetime(2015, 3, 8, 1, 0,
> 0).replace(tzinfo=pytz.timezone('US/Pacific'))
>     weird_tz_aware_datetime =
> weird_naive_datetime.replace(tzinfo=pytz.timezone('US/Pacific'))
>     print(weird_tz_aware_datetime)
>     four_hours=datetime.timedelta(hours=4)
>     print('Four hours later is:')
>     print(weird_tz_aware_datetime + four_hours)
>     print('...but I want numerically 5 hours later, because of
> Daylight Savings Time')
>
> main()
>
>
> Thanks!

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


Thread

Re: Daylight savings time question Dan Stromberg <drsalists@gmail.com> - 2015-03-24 16:52 -0700

csiph-web