Python: serializing / deserializing datetime.time

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

What is the best way to serialize an 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'])
+5
source share
1 answer

If you are repran datetime.timeobject, Python gives you isoformat. Since reprtrying to be serialized versions of its objects, this is a good indication of what value you should use.

import datetime

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

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

All Articles