Skip to content
This repository was archived by the owner on Oct 2, 2024. It is now read-only.

Commit 29516bb

Browse files
committed
Add datetime/string conversion methods to OneDriveObjectBase
1 parent c6f171a commit 29516bb

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

src/onedrivesdk/model/item.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,13 @@ def created_date_time(self):
8282
The createdDateTime
8383
"""
8484
if "createdDateTime" in self._prop_dict:
85-
time_format = "%Y-%m-%dT%H:%M:%S.%f"
86-
return datetime.strptime(self._prop_dict["createdDateTime"].replace("Z",""), time_format)
85+
return self.get_datetime_from_string(self._prop_dict["createdDateTime"])
8786
else:
8887
return None
8988

9089
@created_date_time.setter
9190
def created_date_time(self, val):
92-
time_format = "%Y-%m-%dT%H:%M:%S.%fZ"
93-
self._prop_dict["createdDateTime"] = val.strftime(time_format)
91+
self._prop_dict["createdDateTime"] = self.get_string_from_datetime(val)
9492

9593
@property
9694
def c_tag(self):
@@ -196,15 +194,13 @@ def last_modified_date_time(self):
196194
The lastModifiedDateTime
197195
"""
198196
if "lastModifiedDateTime" in self._prop_dict:
199-
time_format = "%Y-%m-%dT%H:%M:%S.%f"
200-
return datetime.strptime(self._prop_dict["lastModifiedDateTime"].replace("Z",""), time_format)
197+
return self.get_datetime_from_string(self._prop_dict["lastModifiedDateTime"])
201198
else:
202199
return None
203200

204201
@last_modified_date_time.setter
205202
def last_modified_date_time(self, val):
206-
time_format = "%Y-%m-%dT%H:%M:%S.%fZ"
207-
self._prop_dict["lastModifiedDateTime"] = val.strftime(time_format)
203+
self._prop_dict["lastModifiedDateTime"] = self.get_string_from_datetime(val)
208204

209205
@property
210206
def name(self):

src/onedrivesdk/one_drive_object_base.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121
THE SOFTWARE.
2222
------------------------------------------------------------------------------
2323
'''
24-
24+
from datetime import datetime
2525

2626
class OneDriveObjectBase(object):
2727

28+
DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"
29+
DATETIME_FORMAT_NO_MILLISECONDS = "%Y-%m-%dT%H:%M:%SZ"
30+
2831
def to_dict(self):
2932
"""Returns the serialized form of the :class:`OneDriveObjectBase`
3033
as a dict. All sub-objects that are based off of :class:`OneDriveObjectBase`
@@ -42,3 +45,20 @@ def to_dict(self):
4245
serialized[prop] = self._prop_dict[prop]
4346

4447
return serialized
48+
49+
@staticmethod
50+
def get_datetime_from_string(s):
51+
try:
52+
dt = datetime.strptime(
53+
s,
54+
OneDriveObjectBase.DATETIME_FORMAT)
55+
except ValueError as ve:
56+
# Try again with other format
57+
dt = datetime.strptime(
58+
s,
59+
OneDriveObjectBase.DATETIME_FORMAT_NO_MILLISECONDS)
60+
return dt
61+
62+
@staticmethod
63+
def get_string_from_datetime(dt):
64+
return dt.strftime(OneDriveObjectBase.DATETIME_FORMAT)

0 commit comments

Comments
 (0)