简体   繁体   English

使用Peewee和SQLite格式化日期和时间

[英]Formatting dates and time with Peewee and SQLite

I'm using Peewee to access a SQLite DB. 我正在使用Peewee访问SQLite数据库。 How can I save a timestamp in the following format? 如何以以下格式保存时间戳?

'%Y-%m-%d %H:%M:%S' # year-month-day hour-minute-second

(basically: just crop out the microseconds which are present by default) (基本上:仅裁剪默认情况下存在的微秒)

EDIT: I was able to make this work, using the following in the class definition: 编辑:我能够使用以下类定义中的内容来使这项工作:

created = DateTimeField(default=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S))

However, I'm still intersted to know if there's a "built in" way of doing this with Peewee, using the 'formats' parameter. 但是,我仍然很想知道Peewee是否使用“格式”参数使用“内置”方式执行此操作。

That actually won't work since the timestamp is evaluated at import time (or the time it's declared). 由于时间戳是在导入时(或声明的时间)求值的,因此实际上不起作用。 Peewee actually doesn't care about the format going into the database, since it'll just take the python datetime object and let pysqlite convert it. Peewee实际上并不关心进入数据库的格式,因为它只需要python datetime对象并让pysqlite对其进行转换。

If you wanted, the best way would be to subclass DateTimeField and override the db_value method. 如果需要,最好的方法是将DateTimeField子类化并重写db_value方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM