suchen
HeimBackend-EntwicklungPython-TutorialPython编程中time模块的一些关键用法解析

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

2016119152517366.jpg (984×445)

  • 黑色细箭头表示输入值,参数
  • 深黄色的粗箭头表示返回值,输出格式
  • 绿色圆圈表示各类对象
  • 蓝色方框表示具体的方法 (先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)

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

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

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

[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

Stellungnahme
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
详细讲解Python之Seaborn(数据可视化)详细讲解Python之Seaborn(数据可视化)Apr 21, 2022 pm 06:08 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

详细了解Python进程池与进程锁详细了解Python进程池与进程锁May 10, 2022 pm 06:11 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

Python自动化实践之筛选简历Python自动化实践之筛选简历Jun 07, 2022 pm 06:59 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

归纳总结Python标准库归纳总结Python标准库May 03, 2022 am 09:00 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于标准库总结的相关问题,下面一起来看一下,希望对大家有帮助。

分享10款高效的VSCode插件,总有一款能够惊艳到你!!分享10款高效的VSCode插件,总有一款能够惊艳到你!!Mar 09, 2021 am 10:15 AM

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

python中文是什么意思python中文是什么意思Jun 24, 2019 pm 02:22 PM

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

Python数据类型详解之字符串、数字Python数据类型详解之字符串、数字Apr 27, 2022 pm 07:27 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

详细介绍python的numpy模块详细介绍python的numpy模块May 19, 2022 am 11:43 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

See all articles

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

Heiße Werkzeuge

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

SAP NetWeaver Server-Adapter für Eclipse

SAP NetWeaver Server-Adapter für Eclipse

Integrieren Sie Eclipse mit dem SAP NetWeaver-Anwendungsserver.

mPDF

mPDF

mPDF ist eine PHP-Bibliothek, die PDF-Dateien aus UTF-8-codiertem HTML generieren kann. Der ursprüngliche Autor, Ian Back, hat mPDF geschrieben, um PDF-Dateien „on the fly“ von seiner Website auszugeben und verschiedene Sprachen zu verarbeiten. Es ist langsamer und erzeugt bei der Verwendung von Unicode-Schriftarten größere Dateien als Originalskripte wie HTML2FPDF, unterstützt aber CSS-Stile usw. und verfügt über viele Verbesserungen. Unterstützt fast alle Sprachen, einschließlich RTL (Arabisch und Hebräisch) und CJK (Chinesisch, Japanisch und Koreanisch). Unterstützt verschachtelte Elemente auf Blockebene (wie P, DIV),

Herunterladen der Mac-Version des Atom-Editors

Herunterladen der Mac-Version des Atom-Editors

Der beliebteste Open-Source-Editor