Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: serializing/deserializing datetime.time

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'])
like image 988
Theron Luhn Avatar asked Apr 17 '12 19:04

Theron Luhn


1 Answers

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()
like image 114
agf Avatar answered Oct 02 '22 10:10

agf