Azure / msrest-for-python

The runtime library "msrest" for AutoRest generated Python clients.
MIT License
41 stars 64 forks source link

Parsing RFC date assumes current local is English #192

Closed lmazuel closed 4 years ago

lmazuel commented 4 years ago

Example, trying to parse Fri, 28 Feb 2020 19:04:06 GMT with a spanish locale will fail, since "Fri" or "Feb" is not spanish. This is because this parser uses strptime which is local dependent. Python doesn't support configuration for locale.

The only reliable way would be to stop using "strptime", since other solution like this relies on a thread lock and I don't like that.

lmazuel commented 4 years ago

Suggestion based on email stdlib

class _FixedOffset(datetime.tzinfo):
    """Fixed offset in minutes east from UTC.

    Copy/pasted from Python doc

    :param int offset: offset in minutes
    """

    def __init__(self, offset):
        self.__offset = datetime.timedelta(minutes=offset)

    def utcoffset(self, dt):
        return self.__offset

    def tzname(self, dt):
        return str(self.__offset.total_seconds()/3600)

    def __repr__(self):
        return "<FixedOffset {}>".format(self.tzname(None))

    def dst(self, dt):
        return datetime.timedelta(0)

def _parse_http_date(text):
    parsed_date = email.utils.parsedate_tz(text)
    return datetime.datetime(
        *parsed_date[:6],
        tzinfo=_FixedOffset(parsed_date[9]/60)
    )
lmazuel commented 4 years ago

See azure-core polling for details

Guts commented 4 years ago

I encountered this problem and had to add specific workarounds to prevent it from crashing our program that uses azure-store-blob.

Are there any plans to fix this soon?

lmazuel commented 4 years ago

Hey @Guts I wasn't sure if there was any impacted people yet, no I see your message I'm adding that in my TODO, thanks

lmazuel commented 4 years ago

@Guts would you have a stacktrace of the error available? Just for the sake of having this issue with all details.

Guts commented 4 years ago

@lmazuel do you still need the full trace? I've seen that you managed to reproduce in https://github.com/Azure/msrest-for-python/pull/201#issuecomment-624959102 ?

lmazuel commented 4 years ago

I just wanted to be sure I was addressing your problem, comparing the stackstrace would have made sure of it. But I think I got it, thanks! At least, if that sounds off to you let me know.