Skip to content

Commit bde74f3

Browse files
[Storage] Improve datetime utility (#34041)
1 parent bb8166e commit bde74f3

File tree

5 files changed

+75
-7
lines changed

5 files changed

+75
-7
lines changed

sdk/storage/azure-storage-file-share/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ pointing to the root of the file share would raise an `InvalidResourceName` on a
1414
Python 3.12.
1515
- Fixed an issue where authentication errors could raise `AttributeError` instead of `ClientAuthenticationError` when
1616
using async OAuth credentials.
17+
- Fixed an issue where specifying datetime objects with less than 7 digits of precision as input could incorrectly raise
18+
`InvalidHeaderValue` due to improper precision parsing.
1719

1820
## 12.15.0 (2023-11-07)
1921

sdk/storage/azure-storage-file-share/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "python",
44
"TagPrefix": "python/storage/azure-storage-file-share",
5-
"Tag": "python/storage/azure-storage-file-share_b47b0652ef"
5+
"Tag": "python/storage/azure-storage-file-share_48979bc3d9"
66
}

sdk/storage/azure-storage-file-share/azure/storage/fileshare/_parser.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# --------------------------------------------------------------------------
66

77
from datetime import datetime, timedelta
8+
from ._generated._serialization import Serializer
89

910
_ERROR_TOO_MANY_FILE_PERMISSIONS = 'file_permission and file_permission_key should not be set at the same time'
1011
_FILE_PERMISSION_TOO_LONG = 'Size of file_permission is too large. file_permission should be <=8KB, else' \
@@ -41,4 +42,6 @@ def _parse_datetime_from_str(string_datetime):
4142
def _datetime_to_str(datetime_obj):
4243
if not datetime_obj:
4344
return None
44-
return datetime_obj if isinstance(datetime_obj, str) else datetime_obj.isoformat() + '0Z'
45+
if isinstance(datetime_obj, str):
46+
return datetime_obj
47+
return Serializer.serialize_iso(datetime_obj)[:-1].ljust(27, "0") + "Z"

sdk/storage/azure-storage-file-share/tests/test_file.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
# license information.
55
# --------------------------------------------------------------------------
66
import base64
7-
import os
87
import tempfile
9-
import uuid
10-
from datetime import datetime, timedelta
8+
from datetime import datetime, timedelta, timezone
119

1210
import pytest
1311
import requests
@@ -831,6 +829,39 @@ def test_set_file_properties(self, **kwargs):
831829
assert properties.creation_time is not None
832830
assert properties.permission_key is not None
833831

832+
@FileSharePreparer()
833+
@recorded_by_proxy
834+
def test_set_datetime_all_ms_precision(self, **kwargs):
835+
storage_account_name = kwargs.pop("storage_account_name")
836+
storage_account_key = kwargs.pop("storage_account_key")
837+
838+
self._setup(storage_account_name, storage_account_key)
839+
file_client = self._create_file()
840+
841+
date_times = [
842+
datetime(3005, 5, 11, 12, 24, 7),
843+
datetime(3005, 5, 11, 12, 24, 7, 0),
844+
datetime(3005, 5, 11, 12, 24, 7, 1),
845+
datetime(3005, 5, 11, 12, 24, 7, 12),
846+
datetime(3005, 5, 11, 12, 24, 7, 123),
847+
datetime(3005, 5, 11, 12, 24, 7, 1234),
848+
datetime(3005, 5, 11, 12, 24, 7, 12345),
849+
datetime(3005, 5, 11, 12, 24, 7, 123456),
850+
datetime(2023, 12, 8, tzinfo=timezone(-timedelta(hours=7))),
851+
datetime(2023, 12, 8, tzinfo=timezone(-timedelta(hours=8))),
852+
]
853+
854+
# Act / Assert
855+
content_settings = ContentSettings(
856+
content_language='spanish',
857+
content_disposition='inline')
858+
for date1, date2 in zip(date_times[::2], date_times[1::2]):
859+
file_client.set_http_headers(
860+
content_settings=content_settings,
861+
file_creation_time=date1,
862+
file_last_write_time=date2
863+
)
864+
834865
@FileSharePreparer()
835866
@recorded_by_proxy
836867
def test_set_file_properties_trailing_dot(self, **kwargs):

sdk/storage/azure-storage-file-share/tests/test_file_async.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
import base64
99
import os
1010
import tempfile
11-
import uuid
12-
from datetime import datetime, timedelta
11+
from datetime import datetime, timedelta, timezone
1312

1413
import pytest
1514
import requests
@@ -844,6 +843,39 @@ async def test_set_file_properties(self, **kwargs):
844843
assert properties.creation_time is not None
845844
assert properties.permission_key is not None
846845

846+
@FileSharePreparer()
847+
@recorded_by_proxy_async
848+
async def test_set_datetime_all_ms_precision(self, **kwargs):
849+
storage_account_name = kwargs.pop("storage_account_name")
850+
storage_account_key = kwargs.pop("storage_account_key")
851+
852+
self._setup(storage_account_name, storage_account_key)
853+
file_client = await self._create_file(storage_account_name, storage_account_key)
854+
855+
date_times = [
856+
datetime(3005, 5, 11, 12, 24, 7),
857+
datetime(3005, 5, 11, 12, 24, 7, 0),
858+
datetime(3005, 5, 11, 12, 24, 7, 1),
859+
datetime(3005, 5, 11, 12, 24, 7, 12),
860+
datetime(3005, 5, 11, 12, 24, 7, 123),
861+
datetime(3005, 5, 11, 12, 24, 7, 1234),
862+
datetime(3005, 5, 11, 12, 24, 7, 12345),
863+
datetime(3005, 5, 11, 12, 24, 7, 123456),
864+
datetime(2023, 12, 8, tzinfo=timezone(-timedelta(hours=7))),
865+
datetime(2023, 12, 8, tzinfo=timezone(-timedelta(hours=8))),
866+
]
867+
868+
# Act / Assert
869+
content_settings = ContentSettings(
870+
content_language='spanish',
871+
content_disposition='inline')
872+
for date1, date2 in zip(date_times[::2], date_times[1::2]):
873+
await file_client.set_http_headers(
874+
content_settings=content_settings,
875+
file_creation_time=date1,
876+
file_last_write_time=date2
877+
)
878+
847879
@FileSharePreparer()
848880
@recorded_by_proxy_async
849881
async def test_set_file_properties_trailing_dot(self, **kwargs):

0 commit comments

Comments
 (0)