diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index 0f01d4192b11..a10d98e83e6a 100644 --- a/sdk/core/azure-core/CHANGELOG.md +++ b/sdk/core/azure-core/CHANGELOG.md @@ -9,6 +9,7 @@ ### Bug Fixes - Retry policies don't sleep after operations time out +- The `from_dict` methhod in the `CloudEvent` can now convert a datetime string to datetime object when microsecond exceeds the python limitation ## 1.14.0 (2021-05-13) diff --git a/sdk/core/azure-core/azure/core/_utils.py b/sdk/core/azure-core/azure/core/_utils.py index aae272ed729b..5f3133e02955 100644 --- a/sdk/core/azure-core/azure/core/_utils.py +++ b/sdk/core/azure-core/azure/core/_utils.py @@ -53,6 +53,17 @@ def _convert_to_isoformat(date_time): sign, offset = date_time[-6], date_time[-5:] delta = int(sign + offset[:1]) * 60 + int(sign + offset[-2:]) + check_decimal = timestamp.split('.') + if len(check_decimal) > 1: + decimal_str = "" + for digit in check_decimal[1]: + if digit.isdigit(): + decimal_str += digit + else: + break + if len(decimal_str) > 6: + timestamp = timestamp.replace(decimal_str, decimal_str[0:6]) + if delta == 0: tzinfo = TZ_UTC else: diff --git a/sdk/core/azure-core/tests/test_messaging_cloud_event.py b/sdk/core/azure-core/tests/test_messaging_cloud_event.py index c0a88b845488..9dd8b80a7fbc 100644 --- a/sdk/core/azure-core/tests/test_messaging_cloud_event.py +++ b/sdk/core/azure-core/tests/test_messaging_cloud_event.py @@ -98,7 +98,7 @@ def test_cloud_storage_dict(): "storage_diagnostics":{"batchId":"b68529f3-68cd-4744-baa4-3c0498ec19f0"} }, "type":"Microsoft.Storage.BlobCreated", - "time":"2021-02-18T20:18:10.53986Z", + "time":"2021-02-18T20:18:10.581147898Z", "specversion":"1.0" } @@ -120,6 +120,7 @@ def test_cloud_storage_dict(): assert event.time.month == 2 assert event.time.day == 18 assert event.time.hour == 20 + assert event.time.microsecond == 581147 assert event.__class__ == CloudEvent assert "id" in cloud_storage_dict assert "data" in cloud_storage_dict @@ -131,7 +132,7 @@ def test_cloud_custom_dict_with_extensions(): "source":"https://egtest.dev/cloudcustomevent", "data":{"team": "event grid squad"}, "type":"Azure.Sdk.Sample", - "time":"2021-02-18T20:18:10.53986+00:00", + "time":"2021-02-18T20:18:10.539861122+00:00", "specversion":"1.0", "ext1": "example", "ext2": "example2" @@ -142,6 +143,7 @@ def test_cloud_custom_dict_with_extensions(): assert event.time.month == 2 assert event.time.day == 18 assert event.time.hour == 20 + assert event.time.microsecond == 539861 assert event.extensions == {"ext1": "example", "ext2": "example2"} def test_cloud_custom_dict_blank_data():