String To Datetime With Fractional Seconds, On Google App Engine
Solution 1:
Parsing
Without the %f
format support for datetime.datetime.strptime()
you can still sufficiently easy enter it into a datetime.datetime
object (randomly picking a value for your val
here) using datetime.datetime.replace()
), tested on 2.5.5:
>>>val = '2010-08-06T10:00:14.143896'>>>nofrag, frag = val.split('.')>>>nofrag_dt = datetime.datetime.strptime(nofrag, "%Y-%m-%dT%H:%M:%S")>>>dt = nofrag_dt.replace(microsecond=int(frag))>>>dt
datetime.datetime(2010, 8, 6, 10, 0, 14, 143896)
Now you have your datetime.datetime
object.
Storing
Reading further into http://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html#datetime
I can see no mentioning that fractions isn't supported, so yes, it's probably only the datastore viewer. The docs points directly to Python 2.5.2's module docs for datetime
, and it does support fractions, just not the %f
parsing directive for strptime
. Querying for fractions might be trickier, though..
Solution 2:
All ancient history by now, but in these modern times you can also conveniently use dateutil
from dateutil import parser as DUp
funky_time_str = "1/1/2011 12:51:00.0123 AM"
foo = DUp.parse(funky_time_str)
print foo.timetuple()
# time.struct_time(tm_year=2011, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=51, tm_sec=0, tm_wday=5, tm_yday=1, tm_isdst=-1)print foo.microsecond
# 12300print foo
# 2011-01-01 00:51:00.012300
dateutil
supports a surprising variety of possible input formats, which it parses without pattern strings.
Post a Comment for "String To Datetime With Fractional Seconds, On Google App Engine"