Skip to content

[EventGrid/Core] CloudEvent.from_dict fails to convert a datetime string to datetime object when microsecond exceeds the python limitation #18602

@yunhaoling

Description

@yunhaoling
  • Package Name: azure-eventgrid
  • Package Version: 4.1.1
  • Operating System: Windows
  • Python Version: 3.7

Describe the bug
CloudEvent.from_dict fails to convert a datetime string to datetime object when the microsecond field exceeds the python limitation.

The time in the input dictionary is "2021-05-10T14:39:23.5811478Z" (This value is set by the event grid service).

Traceback (most recent call last):
  File "d:\projects\azure-sdk-for-python\sdk\core\azure-core\azure\core\_utils.py", line 65, in _convert_to_isoformat
    deserialized = datetime.datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%f")
  File "C:\Users\yuling\Anaconda3\envs\sbenv37\lib\_strptime.py", line 577, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "C:\Users\yuling\Anaconda3\envs\sbenv37\lib\_strptime.py", line 362, in _strptime
    data_string[found.end():])
ValueError: unconverted data remains: 8

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/Projects/azure-sdk-for-python/sdk/eventhub/azure-eventhub/samples/eg.py", line 37, in <module>
    c = CloudEvent.from_dict(json_obj)
  File "d:\projects\azure-sdk-for-python\sdk\core\azure-core\azure\core\messaging.py", line 166, in from_dict
    time=_convert_to_isoformat(event.get("time")),
  File "d:\projects\azure-sdk-for-python\sdk\core\azure-core\azure\core\_utils.py", line 67, in _convert_to_isoformat
    deserialized = datetime.datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S")
  File "C:\Users\yuling\Anaconda3\envs\sbenv37\lib\_strptime.py", line 577, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "C:\Users\yuling\Anaconda3\envs\sbenv37\lib\_strptime.py", line 362, in _strptime
    data_string[found.end():])
ValueError: unconverted data remains: .5811478

Process finished with exit code 1

To Reproduce
Steps to reproduce the behavior:

from azure.core.messaging import CloudEvent
raw_data = '{"id":"xxx","source":"xxx","specversion":"1.0","type":"Microsoft.Storage.BlobCreated","dataschema":"#","subject":"xxx","time":"2021-05-10T14:39:23.5811478Z","data":{"api":"PutBlob","clientRequestId":"xxx","requestId":"xxx","eTag":"xxx","contentType":"application/octet-stream","contentLength":104065,"blobType":"BlockBlob","url":"xxx","sequencer":"xxx","storageDiagnostics":{"batchId":"xxx"}}}'
json_obj = json.loads(raw_data)
c = CloudEvent.from_dict(json_obj)

Expected behavior
The sdks handles this instead of raising error or we give a more friendly error.

Additional context
python microseconds docs:

datetime.microsecond
In range(1000000). 

cc: @jongio

Metadata

Metadata

Assignees

Labels

ClientThis issue points to a problem in the data-plane of the library.Event GridMessagingMessaging crewblocking-releaseBlocks releasebugThis issue requires a change to an existing behavior in the product in order to be resolved.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions