Python Forum
discrepancy with datetime with timezone
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
discrepancy with datetime with timezone
#1
Can anyone please help explain about the following. why don't I get even numbers for the hours?

I'm running Python 3.10. The following is from ptpython session:
`
>>> import pytz
... from datetime import datetime
...
... # Define time zones for Eastern Time and Pacific Time
... eastern_timezone = pytz.timezone("US/Eastern")
... pacific_timezone = pytz.timezone("US/Pacific")
...
... # Create datetime objects with accurate timezone offsets
... dt1 = datetime(2023, 1, 1, 11, 0, 0, 0, tzinfo=eastern_timezone) # Eastern Time
... dt2 = datetime(2023, 1, 1, 8, 0, 0, 0, tzinfo=pacific_timezone) # Pacific Time
...
... print(dt1)
... print(dt2)
2023-01-01 11:00:00-04:56
2023-01-01 08:00:00-07:53
>>> dt1 - dt2
datetime.timedelta(seconds=180)
`

*********
Update: The issue is pytz. It should not be used for timezone after Python 3.9. https://pypi.org/project/pytz-deprecation-shim/

Quote:pytz has served the Python community well for many years, but it is no longer the best option for providing time zones. pytz has a non-standard interface that is very easy to misuse; this interface was necessary when pytz was created, because datetime had no way to represent ambiguous datetimes, but this was solved in in Python 3.6, which added a fold attribute to datetimes in PEP 495. With the addition of the zoneinfo module in Python 3.9 (PEP 615), there has never been a better time to migrate away from pytz.
Reply


Messages In This Thread
discrepancy with datetime with timezone - by XJia - Sep-03-2023, 06:56 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Error when Excelwriter saving a dataframe with datetime datatype with timezone klllmmm 3 19,909 Dec-08-2020, 11:37 AM
Last Post: Larz60+
  TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str' findbikash 2 11,497 Sep-18-2019, 08:32 AM
Last Post: buran
  Output discrepancy when building Translator skrivver99 17 9,107 Nov-26-2018, 01:22 AM
Last Post: ichabod801
  Package to find Timezone from Latitude and Longitude pythoneer 1 3,890 Jun-09-2018, 03:00 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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