I'm working on a journal-type application in Python. The application basically permits the user write entries in the journal and adds a time-stamp for later querying the journal.
我正在使用Python编写日志类型的应用程序。该应用程序基本上允许用户在日志中写入条目,并添加时间戳以便稍后查询日志。
As of now, I use the time.ctime()
function to generate time-stamps that are visually friendly. The journal entries thus look like:
截至目前,我使用time.ctime()函数生成视觉上友好的时间戳。因此日记帐分录如下:
Thu Jan 21 19:59:47 2010 Did something
Thu Jan 21 20:01:07 2010 Did something else
Now, I would like to be able to use these time-stamps to do some searching/querying. I need to be able to search, for example, for "2010", or "feb 2010", or "23 feb 2010".
现在,我希望能够使用这些时间戳来进行一些搜索/查询。例如,我需要能够搜索“2010”,“2010年2月”或“2010年2月23日”。
My questions are:
我的问题是:
1) What time module(s) should I use: time
vs datetime
?
1)我应该使用什么时间模块:时间与日期时间?
2) What would be an appropriate way of creating and using the time-stamp objects?
2)创建和使用时间戳对象的适当方法是什么?
Many thanks!
非常感谢!
2 个解决方案
#1
7
Option 1: Don't change anything. Use time.strptime to parse your timestamps.
选项1:不要改变任何东西。使用time.strptime来解析时间戳。
Option 2: Change to datetime
. You can format the timestamps the same way, and use datetime.strptime to parse them.
选项2:更改为日期时间。您可以以相同的方式格式化时间戳,并使用datetime.strptime来解析它们。
It doesn't much matter, since the code will be similar. Searching will involve matching months, days or years or some such. Use time
tuples for this. Or datetime.datetime
objects. Or, searching will involve comparing ranges of times; use time
in seconds for this. Or use datetime
objects. They will both work and -- for this -- they will be similar in complexity.
它并不重要,因为代码将类似。搜索将涉及匹配数月,日或年或某些此类。为此使用时间元组。或者datetime.datetime对象。或者,搜索将涉及比较时间范围;以秒为单位使用时间。或者使用datetime对象。它们都可以工作 - 为此 - 它们的复杂程度相似。
Doing date calculations (90 days in the future, 3 months in the past, etc.) is the strong suit of datetime
.
进行日期计算(将来90天,过去3个月等)是日期时间的强项。
In general, you'll often be happier with datetime
because it does more and doesn't involve switching between simple double time and time tuple time.
一般来说,你会经常对日期时间感到满意,因为它做得更多,并且不涉及在简单的双倍时间和时间元组时间之间切换。
#2
9
You might want to consider changing to ISO 8601. Will help with sorting for example, or transferring data between different systems.
您可能需要考虑更改为ISO 8601.例如,有助于排序,或在不同系统之间传输数据。
#1
7
Option 1: Don't change anything. Use time.strptime to parse your timestamps.
选项1:不要改变任何东西。使用time.strptime来解析时间戳。
Option 2: Change to datetime
. You can format the timestamps the same way, and use datetime.strptime to parse them.
选项2:更改为日期时间。您可以以相同的方式格式化时间戳,并使用datetime.strptime来解析它们。
It doesn't much matter, since the code will be similar. Searching will involve matching months, days or years or some such. Use time
tuples for this. Or datetime.datetime
objects. Or, searching will involve comparing ranges of times; use time
in seconds for this. Or use datetime
objects. They will both work and -- for this -- they will be similar in complexity.
它并不重要,因为代码将类似。搜索将涉及匹配数月,日或年或某些此类。为此使用时间元组。或者datetime.datetime对象。或者,搜索将涉及比较时间范围;以秒为单位使用时间。或者使用datetime对象。它们都可以工作 - 为此 - 它们的复杂程度相似。
Doing date calculations (90 days in the future, 3 months in the past, etc.) is the strong suit of datetime
.
进行日期计算(将来90天,过去3个月等)是日期时间的强项。
In general, you'll often be happier with datetime
because it does more and doesn't involve switching between simple double time and time tuple time.
一般来说,你会经常对日期时间感到满意,因为它做得更多,并且不涉及在简单的双倍时间和时间元组时间之间切换。
#2
9
You might want to consider changing to ISO 8601. Will help with sorting for example, or transferring data between different systems.
您可能需要考虑更改为ISO 8601.例如,有助于排序,或在不同系统之间传输数据。