易截截图软件、单文件、免安装、纯绿色、仅160KB

python中datetime的比较方法使用


python中的datetime module是专门处理时间相关内容的模块,功能很强大,但是反而显得比较复杂。
一下代码是用来求从mysql中取到的timestamp和当前时间比较,求时间差的方法
import datetime
lasttime=a.get_last_timestamp(sid=40)[-1]["last_time"] #取到timestamp
delta = datetime.datetime.now()-lasttime   #使用datetime.datetime.now()得到当前的时间,然后求时间差
if delta > datetime.timedelta(minutes=8):   #如果时间差大于 8分钟的话,(如果是8小时则是hours=8,如果是8秒则是 seconds=8)
   .....
#datetime.timedelta()这个方法比较特别,在python.org对这个class的解释是:
class datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])
参数可以多个一起使用,比如datetime.timedelta(hours=5,minutes=8,seconds=10),就代表5小时8分10秒


相关文档:

Python中Range和XRange的区别

Python中Range和XRange的区别(Difference between Range and XRange in Python)
最近机器出了点问题,所以一直没有写新的东西出来。之前在读Python的代码的时候,发觉好多人喜欢用XRange,而不是Range,我也只是记得学习的时候开始学到的是Range,后面又看到有个XRange,当时也没有深究,为什么Python里面要有两个同样的功 ......

python安装之setuptools

David 在本文中将带领我们了解一下 setuptools 框架,它是 Python Enterprise Application Kit(PEAK)的一个副项目。 setuptools 替换了标准的 distutils 库,并为 Python 添加了版本化的包和依赖性管理。Perl 用户比较熟悉 CPAN,而 Ruby 用户则比较熟悉 Gems;引导 setuptools 的 ez_setup 工具和随之而生的扩展后的 eas ......

使用pdb进行python的调试

1 在想要插入断点的地方插入代码
                import pdb
                pdb.set_trace()
2然后使用指令进行debug
查看代码上下文,l(小写L)
监视变量 ......

Python int类型插入数据库 MySQLdb

 def test2():
 32     db = util.DBUnit('mysql_ab')                                    &nb ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号