使用Python将可读时间转换为unix时间

时间:2012-04-03 06:33:42

标签: python sqlite date time

我正在构建一种日历网络应用

我在HTML中设置了以下表单,用户在其中设置了一个事件/约会。

用户输入的数据然后进入python。

我希望能够将他们输入的日期作为unix时间戳存储在SQLite数据库中,同时在需要时将其显示为可读日期。

有什么方法可以在两者之间快速转换?

               <form method="post" action="/writeEvent">

                    <div>
                     <label>Year ("yyyy") <span class="required">*</span></label>
                     <input name="year" type="text" id="year" value="" />
                    </div>

                    <div>
                     <label>Month ("mm")  <span class="required">*</span></label>
                     <input name="month" type="password" id="month" value="" />
                    </div>

                    <div>
                     <label>Day ("dd") <span class="required">*</span></label>
                     <input name="day" type="password" id="day" value="" />
                    </div>

                    <div>
                     <label>Message <span class="required">*</span></label>
                     <textarea name="message" rows="20" cols="30"  id="message" value="" ></textarea><br /><br />
                    </div>

                    <div>
                    <input type="submit"  value="Submit" class="button">
                    <input type="reset" value="Reset" class="button">
                    </div>


                </form>

编辑: 除了我在终端中尝试外,我本身没有任何python代码。

我尝试使用localtime()给我一个人类可读的时间。但是,这是以

的格式给出的
(tm_year=2012, tm_mon=4, tm_mday=3, tm_hour=19, tm_min=9, tm_sec=13, tm_wday=1, tm_yday=94, tm_isdst=0) 

我试图通过首先通过

将其分配给变量来提取年,月和日
newTime=time.localtime()

但是出现了一条错误消息,指出等号上的语法无效。看来我无法将结果分配给变量。

我也尝试使用mktime()生成时间戳。但是当我做的时候

timeStamp = time.mktime(2012,04,03)

我遇到了一个错误说

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: mktime() takes exactly one argument (0 given)

所以现在我不知道接下来该做什么,任何帮助都会非常感激。

提前谢谢!

3 个答案:

答案 0 :(得分:0)

为什么不将它存储为字符串而不进行转换? sqlite仍会让你做日期数学:

SQLite version 3.7.10 2012-01-16 13:28:40
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> CREATE TABLE dates (the_date TEXT);
sqlite> INSERT INTO dates VALUES ('2012-09-13 02:13');
sqlite> INSERT INTO dates VALUES ('2012-10-01 01:15');
sqlite> INSERT INTO dates VALUES ('2012-10-16 08:42');
sqlite> SELECT MIN(the_date) FROM dates;
2012-09-13 02:13
sqlite> SELECT MAX(the_date) FROM dates;
2012-10-16 08:42
sqlite> SELECT the_date FROM dates ORDER BY the_date;
2012-09-13 02:13
2012-10-01 01:15
2012-10-16 08:42
sqlite> SELECT DATE(the_date, '+1 year') FROM dates ORDER BY the_date;
2013-09-13
2013-10-01
2013-10-16

答案 1 :(得分:0)

time.mktime()给出了这个错误,因为它期望的是一个9项长元组,这是一个参数,而你给它3个整数。

有几种方法可以将您的年,月,日值转换为unix时间戳。

首先,由于您只有3个元素,因此最简单的方法是使用datetime module

import datetime
dt = datetime.datetime(2012, 1, 10)

拥有此日期时间对象后,您可以通过几种方式将其转换为纪元时间。如果您希望以UTC时间的形式工作,可以将其转换为UTC时间段,然后使用calendar module将其转换为纪元:

timetuple = dt.utctimetuple()
epoch = calendar.timegm(timetuple)

如果您正在使用当地时间,那么您可以使用time module

执行此部分操作
timetuple = dt.timetuple()
epoch = time.mktime(timetuple)

正如@Here You Go在他的回答中建议的那样,如果您可以将您的字符串存储在sql中并让它将日期转换作为实际日期类型处理,那将更可取,我将推迟他对该路由的回答。

要从utc时间戳返回日期时间对象,您可以执行以下操作:

datetime.datetime.utcfromtimestamp(epoch)

使用日期时间对象可以访问其所有单独的组件:

dt.year
dt.month
dt.day

对于简单的字符串格式化,您可以创建如下字符串:

dateString = "%s-%s-%s" % (year, month, day)

答案 2 :(得分:0)

您可以使用标准库中的日期时间和时间模块来执行此操作。

from datetime import date, datetime

# Create a date object from the form fields
appointment = date(day=20, month=12, year=2013)

# Return a unix time stamp from it
stamp = appointment.strftime('%s')

# Create a datetime object from a time stamp
now = datetime.fromtimestamp(stamp)

# Returns a prettily formatted string of the datetime object
print now.strftime('%H:%M - %A the %d of %B')
# See the docs here 
# http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior 
# for additional options provided for strftime

现在,你可以这样做。但是当您使用Python时,您可能想要考虑将datetime对象作为时间元组存储在数据库中,或者使用tz识别日期时间对象捕获时区信息。