Skip to content

Commit d8feed9

Browse files
author
Rakshith Bhyravabhotla
authored
Handle 6 decimal places cloud event (Azure#19019)
* Handle 6 decimal places cloud event * changelog
1 parent acc476c commit d8feed9

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

sdk/core/azure-core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
### Bug Fixes
1010

1111
- Retry policies don't sleep after operations time out
12+
- The `from_dict` methhod in the `CloudEvent` can now convert a datetime string to datetime object when microsecond exceeds the python limitation
1213

1314

1415
## 1.14.0 (2021-05-13)

sdk/core/azure-core/azure/core/_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ def _convert_to_isoformat(date_time):
5353
sign, offset = date_time[-6], date_time[-5:]
5454
delta = int(sign + offset[:1]) * 60 + int(sign + offset[-2:])
5555

56+
check_decimal = timestamp.split('.')
57+
if len(check_decimal) > 1:
58+
decimal_str = ""
59+
for digit in check_decimal[1]:
60+
if digit.isdigit():
61+
decimal_str += digit
62+
else:
63+
break
64+
if len(decimal_str) > 6:
65+
timestamp = timestamp.replace(decimal_str, decimal_str[0:6])
66+
5667
if delta == 0:
5768
tzinfo = TZ_UTC
5869
else:

sdk/core/azure-core/tests/test_messaging_cloud_event.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def test_cloud_storage_dict():
9898
"storage_diagnostics":{"batchId":"b68529f3-68cd-4744-baa4-3c0498ec19f0"}
9999
},
100100
"type":"Microsoft.Storage.BlobCreated",
101-
"time":"2021-02-18T20:18:10.53986Z",
101+
"time":"2021-02-18T20:18:10.581147898Z",
102102
"specversion":"1.0"
103103
}
104104

@@ -120,6 +120,7 @@ def test_cloud_storage_dict():
120120
assert event.time.month == 2
121121
assert event.time.day == 18
122122
assert event.time.hour == 20
123+
assert event.time.microsecond == 581147
123124
assert event.__class__ == CloudEvent
124125
assert "id" in cloud_storage_dict
125126
assert "data" in cloud_storage_dict
@@ -131,7 +132,7 @@ def test_cloud_custom_dict_with_extensions():
131132
"source":"https://egtest.dev/cloudcustomevent",
132133
"data":{"team": "event grid squad"},
133134
"type":"Azure.Sdk.Sample",
134-
"time":"2021-02-18T20:18:10.53986+00:00",
135+
"time":"2021-02-18T20:18:10.539861122+00:00",
135136
"specversion":"1.0",
136137
"ext1": "example",
137138
"ext2": "example2"
@@ -142,6 +143,7 @@ def test_cloud_custom_dict_with_extensions():
142143
assert event.time.month == 2
143144
assert event.time.day == 18
144145
assert event.time.hour == 20
146+
assert event.time.microsecond == 539861
145147
assert event.extensions == {"ext1": "example", "ext2": "example2"}
146148

147149
def test_cloud_custom_dict_blank_data():

0 commit comments

Comments
 (0)