Python Forum
Confusion with datetime 'dst' function result
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Confusion with datetime 'dst' function result
#1
I have found the result of the datetime 'dst' funtion to be confusing. If I create a datetime object this way:

import datetime, pytz
myDate = datetime.datetime.now(tz=pytz.timezone("US/Eastern"))

and then type

print(myDate.timetuple().tm_isdt)

I get a '0', which correctly means that my date/time is not during daylight saving time. But then I create a new date with

newDate = myDate - datetime.timedelta(days=180)

which gives me a date/time on May 19, 2019. But if I type

print(newDate.timetuple().tm_isdt)

I still get a '0' even though my new date is during daylight saving time. Or, more practically, the statement

print(myDate.dst() == newDate.dst())

results in 'True' even though one date was during daylight saving time and the other wasn't. Is there a way to avoid this confusion (other than limiting any usage of the 'dst' function to the line immediately after a datetime object is created)?
Reply
#2
I'm not sure that's what pytz is for. I think it is meant to be used for converting one time between two time zones, not for dealing with two different times in the same time zone.

Fun fact: pytz uses what is sometimes called the 'Olson database', after the founding contributor, Arthur David Olson. I know Arthur David. He lives near me, and we used to run into each other a lot.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
I'm not able to answer the question but from what I read elsewhere, using the arrow module could be the solution to these issues.
>>> import arrow
>>> a = arrow.now('US/Eastern')
>>> a
<Arrow [2019-11-15T15:34:42.463220-05:00]>
>>> b = a.shift(days=-180)
>>> b
<Arrow [2019-05-19T15:34:42.463220-04:00]>
>>> a.dst()
datetime.timedelta(0)
>>> b.dst()
datetime.timedelta(0, 3600)
Reply
#4
It's the same for my when test this.
Rather than try to figure what's going on,i would use Pendulum .
It provides drop-in replacements for datetime,pytz...ect (or inherit from them if needed).
Timezone is a really strong part that's done right.
>>> import pendulum
>>> 
>>> # Now not daylight savings time
>>> dt = pendulum.datetime(2019, 11, 15, tz='Europe/Berlin')
>>> dt.is_dst()
False

>>> # Go back to when it was daylight savings time(Norway)
>>> dt = pendulum.datetime(2019, 7, 15, tz='Europe/Berlin')
>>> dt.is_dst()
True 
Reply
#5
Working with pytz requires knowledge how to use it right.
If you make a mistake, you get strange results.

I like the pendulum module more, because you have lesser to write.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to use result of solver in another function ross1993hall 0 1,379 Aug-10-2020, 10:29 AM
Last Post: ross1993hall
  Pass integers to datetime.date function florian 3 2,677 Jul-18-2020, 04:43 AM
Last Post: scidam
  How to use function result in another function Ayckinn 4 2,756 Jun-16-2020, 04:50 PM
Last Post: Ayckinn
  Pandas's regular expression function result is so strange cools0607 6 3,095 Jun-15-2020, 07:34 AM
Last Post: cools0607
  TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str' findbikash 2 9,508 Sep-18-2019, 08:32 AM
Last Post: buran
  readshapefile function in basemap - confusion regarding arguments sendiptangshu 1 2,495 Mar-09-2019, 09:29 AM
Last Post: Larz60+
  Erratic Datetime result timsch 0 1,986 Dec-27-2018, 01:49 AM
Last Post: timsch
  SQLAlchemy DateTime result 0's AMarotta97 0 2,249 Oct-01-2018, 04:35 AM
Last Post: AMarotta97
  Why the IDE cannot show the result of zip function? Tony 2 2,861 May-19-2018, 02:58 AM
Last Post: Tony
  confusion on print function volcano 3 3,588 Mar-11-2018, 10:41 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020