Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #87914
| References | <CAGGBd_pFpemgisb-2WFQvu8MehQfHhUJ7=n9CyYFxjj56xOV_g@mail.gmail.com> |
|---|---|
| Date | 2015-03-24 16:52 -0700 |
| Subject | Re: Daylight savings time question |
| From | Dan Stromberg <drsalists@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.124.1427241135.10327.python-list@python.org> (permalink) |
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
Re: Daylight savings time question Dan Stromberg <drsalists@gmail.com> - 2015-03-24 16:52 -0700
csiph-web