Skip to content

Commit

Permalink
Handle 6 decimal places cloud event (#19019)
Browse files Browse the repository at this point in the history
* Handle 6 decimal places cloud event

* changelog
  • Loading branch information
rakshith91 authored Jun 1, 2021
1 parent acc476c commit d8feed9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions sdk/core/azure-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions sdk/core/azure-core/azure/core/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions sdk/core/azure-core/tests/test_messaging_cloud_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand All @@ -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
Expand All @@ -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"
Expand All @@ -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():
Expand Down

0 comments on commit d8feed9

Please sign in to comment.