Skip to content

[BUG] ZoneInfo does not support datetime.Time and utcoffset can't be retrieved #117598

Closed

Description

@07pepa

Bug report

Bug description:

this function does not support datetime.time

(dt.toordinal() - EPOCHORDINAL) * 86400

def test():
    from zoneinfo import ZoneInfo
    from datetime import time
    t=time(12, 4, 5, 123456, tzinfo=ZoneInfo("Europe/Prague"), fold=1)
    if t.tzinfo.utcoffset(None) is None:
        # if prevuous does not work this should but fails with
        # AttributeError: 'datetime.time' object has no attribute 'toordinal'
        t.tzinfo.utcoffset(t)

i propose checking if instance is time and then skip to ordinal like so

 def _get_local_timestamp(self, dt):
        return (
                (dt.toordinal() - EPOCHORDINAL) * 86400 if not isinstance(dt, time) else 0
                + dt.hour * 3600
                + dt.minute * 60
                + dt.second
        )

this issue is blocker for pydantic/speedate#62

if this is confirmed i am open to do PR or leave it to someone else Maybe there is something more i am not seeing

CPython versions tested on:

3.12

Operating systems tested on:

macOS

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Apr 7, 2024
  2. JamesParrott commented on Apr 7, 2024

    @JamesParrott
    Contributor

    I can recreate this on Windows, but below, aren't you asking zoneinfo a question that has an ambiguous answer?

    t=time(12, 4, 5, 123456, tzinfo=ZoneInfo("Europe/Prague"), fold=1)
    t.tzinfo.utcoffset(t)
    

    t.tzinfo.utcoffset(t)utcoffset expects a datetime, so that it can figure out whether or not "Europe/Prague" is on daylight savings time, at the datetime in question.

  3. 07pepa commented on Apr 7, 2024

    @07pepa
    Author

    actualy this works with pytz so maybe its pytz bug ?

       from pytz import timezone
        from datetime import time
        t = time(12, 4, 5, 123456, tzinfo=timezone("Europe/Prague"), fold=1)
        if t.tzinfo.utcoffset(None) is None:
            print(t.tzinfo.utcoffset(t))
  4. JamesParrott commented on Apr 7, 2024

    @JamesParrott
    Contributor

    I don't think it's a bug. It's Python requiring you to explicitly specify from which date you wish the daylight savings time to be used, for a time zone's UTC offset (which may vary on different days of the year).

  5. 07pepa commented on Apr 7, 2024

    @07pepa
    Author

    That seems logical gona file bug on pytz

  6. JamesParrott commented on Apr 7, 2024

    @JamesParrott
    Contributor

    This is outside of CPython's scope now.

    But, @07pepa I don't think it's a bug in pytz either. pytz has just chosen to always return self._utcoffset from timezone.utcoffset:

        def utcoffset(self, dt, is_dst=None):
            '''See datetime.tzinfo.utcoffset
    
            The is_dst parameter may be used to remove ambiguity during DST
            transitions.
    
            >>> from pytz import timezone
            >>> tz = timezone('America/St_Johns')
            >>> ambiguous = datetime(2009, 10, 31, 23, 30)
    
            >>> str(tz.utcoffset(ambiguous, is_dst=False))
            '-1 day, 20:30:00'
    
            >>> str(tz.utcoffset(ambiguous, is_dst=True))
            '-1 day, 21:30:00'
    
            >>> try:
            ...     tz.utcoffset(ambiguous)
            ... except AmbiguousTimeError:
            ...     print('Ambiguous')
            Ambiguous
    
            '''
            if dt is None:
                return None
            elif dt.tzinfo is not self:
                dt = self.localize(dt, is_dst)
                return dt.tzinfo._utcoffset
            else:
                return self._utcoffset
    

    https://github.com/stub42/pytz/blob/fb43f957c5149e750c3be3cfc72b22ad94db4886/src/pytz/tzinfo.py#L422

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions