protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

Parsing a timestamp-string without a timezone-offset using `FromJsonString` throws a confusing message

Open caje731 opened this issue 4 years ago • 1 comments

What version of protobuf and what language are you using? Version: v3.17.3. Language:Python 3.8

What operating system (Linux, Windows, ...) and version? macOS Big Sur Version 11.2

What runtime / compiler are you using (e.g., python version or gcc version) Python 3.8

What did you do? Steps to reproduce the behavior:

>>> from google.protobuf.timestamp_pb2 import Timestamp 
>>> date_string = "2021-06-15T09:45:49"   # timezone-naive
>>> pb_timestamp = Timestamp().FromJsonString(date_string)

What did you expect to see

ValueError: time data '2021-06-15T09:45:49' does not specify a timezone-offset

What did you see instead?

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/x/.local/share/virtualenvs/y-i29MmNV0/lib/python3.8/site-packages/google/protobuf/internal/well_known_types.py", line 167, in FromJsonString
    date_object = datetime.strptime(second_value, _TIMESTAMPFOMAT)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/_strptime.py", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/_strptime.py", line 349, in _strptime
    raise ValueError("time data %r does not match format %r" %
ValueError: time data '2021-06' does not match format '%Y-%m-%dT%H:%M:%S'

The time-data passed in was 2021-06-15T09:45:49, and not just 2021-06 as erroneously reported in the stack-trace.

Anything else we should know about your project / environment

caje731 avatar Jun 23 '21 13:06 caje731

This does not work:

https://github.com/protocolbuffers/protobuf/blob/fb8862f781430f2ebf66dae8ab6ac455ee778f40/python/google/protobuf/internal/well_known_types.py#L148

maybe changing it to this would do the trick?

 timezone_offset = value[10:].rfind('-')

took me a while to track down this bug to end up on this issue

K-Yo avatar Dec 09 '22 15:12 K-Yo

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago.

github-actions[bot] avatar Feb 21 '24 10:02 github-actions[bot]

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please reopen it.

This issue was closed and archived because there has been no new activity in the 14 days since the inactive label was added.

github-actions[bot] avatar Mar 07 '24 10:03 github-actions[bot]