string to datetime with fractional seconds, on Google App Engine

前端 未结 2 1573
无人及你
无人及你 2021-02-07 11:28

I need to convert a string to a datetime object, along with the fractional seconds. I\'m running into various problems.

Normally, i would do:

>>>         


        
2条回答
  •  爱一瞬间的悲伤
    2021-02-07 12:14

    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..

提交回复
热议问题