X Tutup
The Wayback Machine - https://web.archive.org/web/20221223123120/https://github.com/python/cpython/issues/100371
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

offset-aware datetime.datetime.time() does not keep timezone #100371

Closed
LarsKumbier opened this issue Dec 20, 2022 · 1 comment
Closed

offset-aware datetime.datetime.time() does not keep timezone #100371

LarsKumbier opened this issue Dec 20, 2022 · 1 comment
Labels
type-bug An unexpected behavior, bug, or error

Comments

@LarsKumbier
Copy link

LarsKumbier commented Dec 20, 2022

Bug report

An offset-aware datetime.datetime.time() result does not contain the timezone of the original object. This leads to problems when trying to compare them with offset-aware times:

>>> from datetime import datetime, time, timezone, timedelta
>>> x = datetime(2022, 12, 20, 0, 0, 0, tzinfo=timezone(timedelta(hours=5)))
>>> x.tzinfo  # fine: x contains a proper timezone (is offset-aware)
datetime.timezone(datetime.timedelta(seconds=18000))
>>> x.time()  # bug: x.time() is not offset-aware anymore...
datetime.time(0, 0)
>>> x.time().tzinfo # ...because it does NOT retain the timezone info...
>>> x.time() < time(tzinfo=timezone(timedelta(hours=-2))) # ...so it can NOT be compared with offset-aware times.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't compare offset-naive and offset-aware times

I would have expected that

  • offset-naive datetime.datetime.time() returns an offset-naive time object, and
  • offset-aware datetime.datetime.time() returns an offset-aware time object,

because with the current (faulty) behavior, we loose important specificity.

Your environment

  • CPython versions tested on: 3.10, 3.11
  • Operating system and architecture: Linux amd64
@LarsKumbier LarsKumbier added the type-bug An unexpected behavior, bug, or error label Dec 20, 2022
@LarsKumbier
Copy link
Author

LarsKumbier commented Dec 20, 2022

>>> help(x.time)
Help on built-in function time:

time(...) method of datetime.datetime instance
    Return time object with same time but with tzinfo=None.
>>> help(x.timetz)
Help on built-in function timetz:

timetz(...) method of datetime.datetime instance
    Return time object with same time and tzinfo.

Okay, there's an explicit method to get the tz-aware time. I would not have designed it like this (explicit is better than implicit and an implicit change of an offset-aware time into offset-naive feels wrong), but my problem is solved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
Development

No branches or pull requests

1 participant
X Tutup