Home Backend Development Python Tutorial Python编程中time模块的一些关键用法解析

Python编程中time模块的一些关键用法解析

Jun 10, 2016 pm 03:06 PM
python time module

python中time模块其实不难,就是关系转换有点老记不住,先看下图可以说明几个时间对象的的关系.供参考理解.

2016119152517366.jpg (984×445)

  • 黑色细箭头表示输入值,参数
  • 深黄色的粗箭头表示返回值,输出格式
  • 绿色圆圈表示各类对象
  • 蓝色方框表示具体的方法 (先import time,在使用time模块中的方法)

time.time():获取当前时间的时间戳

time.localtime():接受一个时间戳,并把它转化为一个当前时间的元组。不给参数的话就会默认将time.time()作为参数传入,localtime返回tuple格式的时间,有一个和它类似的函数叫gmtime(),2个函数的差别是时区,下面会说到。

>>> time.ctime()
Copy after login

'Wed Jan 18 19:54:12 2016'
Copy after login

>>> time.localtime()
Copy after login

(2016, 1, 18, 19, 54, 19, 2, 100, 1)
 年,月,日,时,分,秒,周,年中的第几日,时区标识.
Copy after login

>>> time.gmtime()
Copy after login

(2016, 1, 18, 23, 54, 25, 2, 100, 0)

Copy after login

2016119152733184.png (363×414)

  • 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
Copy after login

format:

2016119152733184.png (363×414)

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()#打印处理器时间
Copy after login

######运行结果######

[root@localhost ~]# python time1.py
Copy after login

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

Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can the Python interpreter be deleted in Linux system? Can the Python interpreter be deleted in Linux system? Apr 02, 2025 am 07:00 AM

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...

How to solve the problem of Pylance type detection of custom decorators in Python? How to solve the problem of Pylance type detection of custom decorators in Python? Apr 02, 2025 am 06:42 AM

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

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

Python 3.6 loading pickle file error ModuleNotFoundError: What should I do if I load pickle file '__builtin__'? Python 3.6 loading pickle file error ModuleNotFoundError: What should I do if I load pickle file '__builtin__'? Apr 02, 2025 am 06:27 AM

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

Do FastAPI and aiohttp share the same global event loop? Do FastAPI and aiohttp share the same global event loop? Apr 02, 2025 am 06:12 AM

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

What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6? What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6? Apr 02, 2025 am 07:12 AM

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

How to ensure that the child process also terminates after killing the parent process via signal in Python? How to ensure that the child process also terminates after killing the parent process via signal in Python? Apr 02, 2025 am 06:39 AM

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...

See all articles