在Google App Engine上将字符串转换为datetime(分秒) [英] string to datetime with fractional seconds, on Google App Engine

查看:101
本文介绍了在Google App Engine上将字符串转换为datetime(分秒)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将字符串转换为datetime对象以及小数秒。我遇到各种问题。

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

通常我会这样做:

>>> datetime.datetime.strptime(val, "%Y-%m-%dT%H:%M:%S.%f")

但是错误和旧文档告诉我,python2.5的strptime没有%f ...

But errors and old docs showed me that python2.5's strptime does not have %f...

进一步调查,似乎App Engine的数据存储不像小数秒。在编辑数据存储实体时,尝试将.5添加到datetime字段中,给出以下错误:

Investigating further, it seems that the App Engine's data store does not like fractional seconds. Upon editing a datastore entity, trying to add .5 to the datetime field gave me the following error:

ValueError: unconverted data remains: .5

我怀疑不支持小数秒...所以这只是在数据存储区观众,对吧?

I doubt that fractional seconds are not supported... so this is just on the datastore viewer, right?

有人绕过这个问题吗?我想使用本机datetime对象...我不是存储UNIX时间戳...

Has anyone circumvented this issue? I want to use the native datetime objects... I rather not store UNIX timestamps...

谢谢!

编辑:感谢Jacob Oscarson为.replace(...)提示!

Thanks to Jacob Oscarson for the .replace(...) tip!

有一件事要保留头脑是检查nofrag的长度,然后送入。不同的来源使用不同的精度秒。

One thing to keep in mind is to check the length of nofrag before feeding it in. Different sources use different precision for seconds.

这是一个快速的功能,寻找类似的东西:

Here's a quick function for those looking for something similar:

def strptime(val):
    if '.' not in val:
        return datetime.datetime.strptime(val, "%Y-%m-%dT%H:%M:%S")

    nofrag, frag = val.split(".")
    date = datetime.datetime.strptime(nofrag, "%Y-%m-%dT%H:%M:%S")

    frag = frag[:6]  # truncate to microseconds
    frag += (6 - len(frag)) * '0'  # add 0s
    return date.replace(microsecond=int(frag))


推荐答案

解析

没有 $ f 格式支持 datetime.datetime.strptime()您仍然可以很容易地将其输入到 datetime。使用 datetime.datetime.replace()对象(随机选择您的 val 的值) $ c>),在2.5.5上测试:

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)

现在你有你的 datetime.datetime 对象。

存储

进一步阅读 http://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html#datetime

我可以看到没有提到分数不支持,所以是的,它可能只是数据存储查看器。该文档直接指向Python 2.5.2的模块文档,用于 datetime ,它支持分数,而不是%f 解析指令 strptime 。查询分数可能会更棘手,但是..

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

这篇关于在Google App Engine上将字符串转换为datetime(分秒)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆