has2k1 / mizani

A scales package for python
https://mizani.readthedocs.io
BSD 3-Clause "New" or "Revised" License
49 stars 14 forks source link

Comparison between tz-naive and tz-aware datetime objects due to `floor_hour` timezone erasure #45

Closed broad-well closed 1 month ago

broad-well commented 3 months ago

I haven't had time to find the exact conditions that trigger the condition described in the title, but from a little digging in my debugger, I found the following behavior in date_utils.py:

>>> floor_hour(datetime.datetime(2022, 12, 22, 8, 10, tzinfo=datetime.timezone.utc))
datetime.datetime(2022, 12, 22, 8, 0)

This effectively erases the timezone information. In my case, the return value is then compared to a timezone-aware datetime value from my dataset, which produces an error.

broad-well commented 3 months ago
def floor_day(d: datetime) -> datetime:
    """
    Round down to the start of the day
    """
    return d.min.replace(d.year, d.month, d.day, tzinfo=d.tzinfo) if has_time(d) else d

This appears to fix it on my end.

has2k1 commented 1 month ago

Thanks for solution. It indeed was an oversight.