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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
