Python unixtime 比较
Python unixtime compare
我在日期比较方面有点问题。函数检查数据库的域和时间,如果没有找到域,则执行代码并插入数据库。如果域和日期存在,则检查日期是否早于 24 小时并执行函数。
数据库条目如下所示:
domain inform_date
pl23.domain.lt 1426690221
函数是:
### Inform function
def information( domain ):
inform_timelimit = now - (inform_time*3600)
cur.execute("SELECT inform_date FROM php_inform_date WHERE `domain` = %s", (domain))
time_send = cur.fetchone()
if time_send == None:
cur.execute("REPLACE INTO php_inform_date VALUES (%s, %s)", (domain, now))
(...)
if inform_timelimit > time_send[0]:
cur.execute("REPLACE INTO php_inform_date VALUES (%s, %s)", (domain, now))
(...)
可变时间限制是 unixtime:
now = int(time.time())
inform_timelimit = now - (24*3600)
找到域时出现的错误:
Traceback (most recent call last):
File "/opt/php-secure-sendmail/secure_sendmail.py", line 84, in <module>
information(domain)
File "/opt/php-secure-sendmail/secure_sendmail.py", line 41, in information
if inform_timelimit > time_send[0]:
TypeError: 'NoneType' object is unsubscriptable
PS。抱歉我的英语不好 ;)
我找到了解决方案。只是改变逻辑:
### Inform function
def information( domain ):
cur.execute("SELECT inform_date FROM php_inform_date WHERE `domain` = %s", (domain))
time_send = cur.fetchone()
### if no information about time in DB do action:
if time_send == None:
(.....)
else:
### if found entry, then check 24 hour limit / inform_time - includet from config file (24 hours / or can change)
inform_timelimit = now - time_send[0]
if inform_timelimit > inform_time:
(.....)
我在日期比较方面有点问题。函数检查数据库的域和时间,如果没有找到域,则执行代码并插入数据库。如果域和日期存在,则检查日期是否早于 24 小时并执行函数。
数据库条目如下所示:
domain inform_date
pl23.domain.lt 1426690221
函数是:
### Inform function
def information( domain ):
inform_timelimit = now - (inform_time*3600)
cur.execute("SELECT inform_date FROM php_inform_date WHERE `domain` = %s", (domain))
time_send = cur.fetchone()
if time_send == None:
cur.execute("REPLACE INTO php_inform_date VALUES (%s, %s)", (domain, now))
(...)
if inform_timelimit > time_send[0]:
cur.execute("REPLACE INTO php_inform_date VALUES (%s, %s)", (domain, now))
(...)
可变时间限制是 unixtime:
now = int(time.time())
inform_timelimit = now - (24*3600)
找到域时出现的错误:
Traceback (most recent call last):
File "/opt/php-secure-sendmail/secure_sendmail.py", line 84, in <module>
information(domain)
File "/opt/php-secure-sendmail/secure_sendmail.py", line 41, in information
if inform_timelimit > time_send[0]:
TypeError: 'NoneType' object is unsubscriptable
PS。抱歉我的英语不好 ;)
我找到了解决方案。只是改变逻辑:
### Inform function
def information( domain ):
cur.execute("SELECT inform_date FROM php_inform_date WHERE `domain` = %s", (domain))
time_send = cur.fetchone()
### if no information about time in DB do action:
if time_send == None:
(.....)
else:
### if found entry, then check 24 hour limit / inform_time - includet from config file (24 hours / or can change)
inform_timelimit = now - time_send[0]
if inform_timelimit > inform_time:
(.....)