-
Notifications
You must be signed in to change notification settings - Fork 0
Description
When making a new booking of an available experiment (i.e. no existing bookings) via python without including a user the datetime format that is returned when checking availability is e.g. 2024-08-29T09:32:58.857Z and this is the same format returned when checking the format of the new booking that is made. This is the format that datetime.fromisoformat() cannot handle before Python3.11, hence the need to use datetime.strp(text, fmt) and provide the format above.
However, if there is an existing booking (made via the booking UI) then the format of the datetime returned from the booking backend is e.g. 2024-08-29T10:33:18.526+01:00 and this does not conform with the above format provided to datetime.strp and an error is thrown.
Proposed solution - when checking for availability and bookings, check both formats
Line 162 and 163 in check_slot_available:
start = datetime.strptime(avail[0]["start"], "%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=timezone.utc)
end = datetime.strptime(avail[0]["end"], "%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=timezone.utc)
Become:
for fmt in ("%Y-%m-%dT%H:%M:%S.%fZ", "%Y-%m-%dT%H:%M:%S.%f%z"):
try:
start = datetime.strptime(avail[0]["start"], fmt)
end = datetime.strptime(avail[0]["end"], fmt)
except ValueError:
pass
And similarly line 299 and 300 in get_bookings:
start = datetime.strptime(booking["when"]["start"], "%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=timezone.utc)
end = datetime.strptime(booking["when"]["end"], "%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=timezone.utc)
Become:
for fmt in ("%Y-%m-%dT%H:%M:%S.%fZ", "%Y-%m-%dT%H:%M:%S.%f%z"):
try:
start = datetime.strptime(booking["when"]["start"], fmt)
end = datetime.strptime(booking["when"]["end"], fmt)
except ValueError:
pass
The `.replace(tzinfo=timezone.utc) has been removed as this sets the timezone to utc on times received that are set to +01.00 and makes the bookings unavailable (it seems).
This solution has been checked and enables bookings to be made from python both with and without a user and with and without a pre-existing booking.
Perhaps a better solution would be to force equivalent time formats for bookings made from python rather than deal with different formats depending on how the booking is made.