8
votes

I have a form with dropdowns full of times, represented with datetime.time objects.

What's the best way to serialize the object? eg:

<option value="${time.serialize()}">${time.isoformat()}</option>

And then deserialize it on the other end? eg:

time = datetime.time.deserialize(request.params['time'])
1

1 Answers

10
votes

If you repr a datetime.time object, Python gives you isoformat. As reprs attempt to be serialized versions of their objects, that's a good indication it's the value you should use.

import datetime

timestring = datetime.datetime.now().time().isoformat()

timeobj = datetime.datetime.strptime(timestring, "%H:%M:%S.%f").time()