-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update to use timespan #20233
Update to use timespan #20233
Changes from 5 commits
f6049d5
e8fedd6
4dde404
cc2b73a
286fe9f
f96188a
179b556
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
# Licensed under the MIT License. See License.txt in the project root for | ||
# license information. | ||
# -------------------------------------------------------------------------- | ||
from datetime import datetime, timedelta | ||
from typing import TYPE_CHECKING | ||
from msrest import Serializer | ||
from azure.core.exceptions import HttpResponseError | ||
|
@@ -49,26 +50,34 @@ def order_results(request_order, responses): | |
ordered = [mapping[id] for id in request_order] | ||
return ordered | ||
|
||
def construct_iso8601(start=None, end=None, duration=None): | ||
def construct_iso8601(timespan=None): | ||
if not timespan: | ||
return None | ||
try: | ||
start, end, duration = None, None, None | ||
if isinstance(timespan[1], datetime): | ||
start, end = timespan[0], timespan[1] | ||
elif isinstance(timespan[1], timedelta): | ||
start, duration = timespan[0], timespan[1] | ||
else: | ||
raise ValueError('Tuple must be a start datetime with a timedelta or an end datetime.') | ||
except TypeError: | ||
duration = timespan | ||
if duration is not None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can just use if duration: :) |
||
duration = 'PT{}S'.format(duration.total_seconds()) | ||
try: | ||
duration = 'PT{}S'.format(duration.total_seconds()) | ||
except AttributeError: | ||
raise ValueError('timespan must be a timedelta or a tuple.') | ||
iso_str = None | ||
if start is not None: | ||
start = Serializer.serialize_iso(start) | ||
if end and duration: | ||
raise ValueError("start_time can only be provided with duration or end_time, but not both.") | ||
if end is not None: | ||
end = Serializer.serialize_iso(end) | ||
iso_str = start + '/' + end | ||
elif duration is not None: | ||
iso_str = start + '/' + duration | ||
else: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In which case will enter here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. premeptively raising in case start is provided with something other than a timedelta or a datetime There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks to me here is the case start is not None while end and duration are None? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. improved the message to warn about none explicitly |
||
raise ValueError("Start time must be provided along with duration or end time.") | ||
elif end is not None: | ||
if not duration: | ||
raise ValueError("End time must be provided along with duration or start time.") | ||
end = Serializer.serialize_iso(end) | ||
iso_str = duration + '/' + end | ||
else: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, in which case will enter here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in case timespan is a timedelta and no start is provided |
||
iso_str = duration | ||
return iso_str |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add some comments to describe the situation?
e.g. if isinstance(timespan[1], datetime): # if timespan is tuple[datetime, datetime], we treat it as [start time, end time].