Python编程中time模块的一些关键用法解析
python中time模块其实不难,就是关系转换有点老记不住,先看下图可以说明几个时间对象的的关系.供参考理解.
- 黑色细箭头表示输入值,参数
- 深黄色的粗箭头表示返回值,输出格式
- 绿色圆圈表示各类对象
- 蓝色方框表示具体的方法 (先import time,在使用time模块中的方法)
time.time():获取当前时间的时间戳
time.localtime():接受一个时间戳,并把它转化为一个当前时间的元组。不给参数的话就会默认将time.time()作为参数传入,localtime返回tuple格式的时间,有一个和它类似的函数叫gmtime(),2个函数的差别是时区,下面会说到。
>>> time.ctime()
'Wed Jan 18 19:54:12 2016'
>>> time.localtime()
(2016, 1, 18, 19, 54, 19, 2, 100, 1) 年,月,日,时,分,秒,周,年中的第几日,时区标识.
>>> time.gmtime()
(2016, 1, 18, 23, 54, 25, 2, 100, 0)
- time.mktime():和time.localtime()相反,它把一个时间元组转换成时间戳(这个必须要给一个参数)。
- time.asctime():把一个时间元组表示为:“Sun Jul 28 03:35:26 2013”这种格式,不给参数的话就会默认将time.localtime()作为参数传入。
- time.ctime():把一个时间戳转换为time.asctime()的表达格式,不给参数的话就会默认将time.time()作为参数传入。
- time.gmtime():将一个时间戳转换为UTC+0时区(中国应该是+8时区,相差8个小时)的时间元组,不给参数的话就会默认将time.time()作为参数传入。gmtime()返回的是0时区的值,localtime返回的是当前时区的值。
- time.strftime(format,time.localtime()):将一个时间元组转换为格式化的时间字符,不给时间元组参数的话就会默认将time.localtime()作为参数传入。
例如web日志里面的时间格式就是time.strftime('%d/%b/%Y:%X')
返回结果:
Sun Jul 28 04:37:38 2013
format:
time.strptime(stringtime,format):将时间字符串根据指定的格式化符转换成数组形式的时间,
例如:time.strptime('28/Jul/2013:04:33:29', '%d/%b/%Y:%X')
返回结果:
time.struct_time(tm_year=2013, tm_mon=7, tm_mday=28, tm_hour=4, tm_min=33, tm_sec=29, tm_wday=6, tm_yday=209, tm_isdst=-1)
time.clock():返回处理器时钟时间,一般用于性能测试和基准测试等,因为他们反映了程序使用的实际时间,平常用不到这个。
time.sleep():推迟指定的时间运行,单位为秒。
import time print time.time() #打印时间戳 print time.localtime()#打印本地时间元组 print time.gmtime()#答应UTC+0时区的时间元组 print time.ctime()#打印asctime格式化时间 print time.mktime(time.localtime())#将时间元组转换为时间戳 print time.asctime()#打印格式化时间 print time.strftime('%d/%b/%Y:%X')#打印指定格式的时间格式 #把时间字符串和它的格式翻译成时间元组 print time.strptime('28/Jul/2013:04:33:29', '%d/%b/%Y:%X') print '%0.5f'%time.clock() #打印处理器时间 for i in range(100000): pass print '%0.5f'%time.clock()#打印处理器时间
######运行结果######
[root@localhost ~]# python time1.py
1364028568.55 time.struct_time(tm_year=2013, tm_mon=3, tm_mday=23, tm_hour=4, tm_min=49, tm_sec=28, tm_wday=5, tm_yday=82, tm_isdst=1) time.struct_time(tm_year=2013, tm_mon=3, tm_mday=23, tm_hour=8, tm_min=49, tm_sec=28, tm_wday=5, tm_yday=82, tm_isdst=0) Sat Mar 23 04:49:28 2013 1364028568.0 Sat Mar 23 04:49:28 2013 23/Mar/2013:04:49:28 time.struct_time(tm_year=2013, tm_mon=7, tm_mday=28, tm_hour=4, tm_min=33, tm_sec=29, tm_wday=6, tm_yday=209, tm_isdst=-1) 0.02000 0.03000

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Regarding the problem of removing the Python interpreter that comes with Linux systems, many Linux distributions will preinstall the Python interpreter when installed, and it does not use the package manager...

Pylance type detection problem solution when using custom decorator In Python programming, decorator is a powerful tool that can be used to add rows...

About Pythonasyncio...

Using python in Linux terminal...

Loading pickle file in Python 3.6 environment error: ModuleNotFoundError:Nomodulenamed...

Compatibility issues between Python asynchronous libraries In Python, asynchronous programming has become the process of high concurrency and I/O...

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...

The problem and solution of the child process continuing to run when using signals to kill the parent process. In Python programming, after killing the parent process through signals, the child process still...
