详解Python time库的使用

作者:雪急飞绪 时间:2021-06-06 07:18:02 

一、时间获取函数


>>> import time
>>> time.time()
1570530861.740123
>>> time.ctime()
'Tue Oct 8 18:34:27 2019'
>>> time.gmtime()
time.struct_time(tm_year=2019, tm_mon=10, tm_mday=8, tm_hour=10, tm_min=34, tm_sec=52, tm_wday=1, tm_yday=281, tm_isdst=0)

二、时间格式化


time.strftime(format[, t])
format – 格式字符串。
t – 可选的参数t是一个struct_time对象。
python中时间日期格式化符号:
%Y 年份
%m 月份
%B 月份名称 January
%b 月份名称缩写 Jan
%d 日期
%A 星期 Monday
%a 星期缩写 Mon
%H 小时 24
%h 小时 12
%p 上下午
%M 分钟
%S 秒
>>> t=time.gmtime()
>>> time.strftime("%Y-%m-%d %H:%M:%S", t)
'2019-10-08 10:38:06'
>>> time.strftime("%Y-%B-%d-%A-%H-%p-%S")
'2019-October-08-Tuesday-18-PM-50'
>>> time.strftime("%A-%p")
'Tuesday-PM'
>>> time.strftime("%M:%S")
'39:59'

三、时间进度条

测量时间:perf_counter() 返回系统运行时间。由于返回值的基准点是未定义的,所以,只有连续调用的结果之间的差才是有效的。


>>> start = time.perf_counter()
>>> start
684.980333384
>>> end = time.perf_counter()
>>> end
696.094559111
>>> end-start
11.114225726999962

产生时间:sleep(secs) 推迟调用线程的运行

secs:休眠时间;可以是浮点数,如time.sleep(2.7)


#TextProBarV3.py
import time
scale = 40
print('执行开始'.center(scale//2,'-'))
start = time.perf_counter()
for i in range(scale+1):
 a = '*' * i
 b = '.' * (scale - i)
 c = (i / scale) * 100
 dur = time.perf_counter() - start
 print("\r{:^3.0f}%[{}->{}]{:.2f}".format(c,a,b,dur),end='')
 time.sleep(0.1)
print('\n'+'执行结果'.center(scale//2,'-'))

四、七段数码管

七段数码管(seven-segment indicator)由7 段数码管拼接而成,每段有亮或不亮两种情况,改进型的七段数码管还包括一个小数点位置。

详解Python time库的使用

七段数码管能形成27=128 种不同状态,其中部分状态能够显示易于人们理解的数字或字母含义,因此被广泛使用。

详解Python time库的使用


#DrawSevenSegDisplay.py
import turtle, datetime
def drawGap(): #绘制数码管间隔
 turtle.penup()
 turtle.fd(5)
def drawLine(draw):  #绘制单段数码管
 drawGap()
 turtle.pendown() if draw else turtle.penup()
 turtle.fd(40)
 drawGap()
 turtle.right(90)
def drawDigit(d): #根据数字绘制七段数码管
 drawLine(True) if d in [2,3,4,5,6,8,9] else drawLine(False)
 drawLine(True) if d in [0,1,3,4,5,6,7,8,9] else drawLine(False)
 drawLine(True) if d in [0,2,3,5,6,8,9] else drawLine(False)
 drawLine(True) if d in [0,2,6,8] else drawLine(False)
 turtle.left(90)
 drawLine(True) if d in [0,4,5,6,8,9] else drawLine(False)
 drawLine(True) if d in [0,2,3,5,6,7,8,9] else drawLine(False)
 drawLine(True) if d in [0,1,2,3,4,7,8,9] else drawLine(False)
 turtle.left(180)
 turtle.penup()
 turtle.fd(20)
def drawDate(date):
 turtle.pencolor("red")
 for i in date:
   if i == '-':
     turtle.write('年',font=("Arial", 18, "normal"))
     turtle.pencolor("green")
     turtle.fd(40)
   elif i == '=':
     turtle.write('月',font=("Arial", 18, "normal"))
     turtle.pencolor("blue")
     turtle.fd(40)
   elif i == '+':
     turtle.write('日',font=("Arial", 18, "normal"))
   else:
     drawDigit(eval(i))
def main():
 turtle.setup(800, 350, 200, 200)
 turtle.penup()
 turtle.fd(-350)
 turtle.pensize(5)
 drawDate(datetime.datetime.now().strftime('%Y-%m=%d+'))
 turtle.hideturtle()
main()

总结

以上所述是小编给大家介绍的Python time库的使用,网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

来源:https://blog.csdn.net/qq_38689395/article/details/102372351

标签:python,time,库
0
投稿

猜你喜欢

  • 一个很棒的js图片代码

    2009-12-17 18:36:00
  • PHP设计模式之模板方法模式Template Method Pattern详解

    2023-05-25 00:24:26
  • 教你为SQL Server数据库构造安全门

    2009-01-20 11:34:00
  • Python表示矩阵的方法分析

    2022-06-24 01:31:59
  • 深入php var_dump()函数的详解

    2023-11-08 16:09:01
  • Python 中 -m 的典型用法、原理解析与发展演变

    2023-07-09 17:11:40
  • tensorflow使用L2 regularization正则化修正overfitting过拟合方式

    2023-11-26 04:07:32
  • mssql 大小写区分方法

    2008-12-29 14:08:00
  • 如何用变量实现群聊和悄悄话?

    2010-05-19 21:33:00
  • 工程师必须了解的LRU缓存淘汰算法以及python实现过程

    2023-05-22 22:46:00
  • 在ASP.NET 2.0中操作数据之四十:自定义DataList编辑界面

    2023-07-07 04:45:20
  • python提取照片坐标信息的实例代码

    2023-06-01 16:37:10
  • Jquery作者John Resig自己封装的javascript 常用函数

    2023-09-11 13:14:16
  • python实现简易猜数小游戏

    2022-08-08 09:51:55
  • 利用Python将list列表写入文件并读取的方法汇总

    2023-12-07 13:40:07
  • 使用Python+selenium实现第一个自动化测试脚本

    2021-01-26 17:52:01
  • Sql Server 2012 分页方法分析(offset and fetch)

    2012-10-07 10:51:04
  • PHP 修改SESSION的生存时间案例详解

    2023-06-11 19:44:20
  • Python 串口读写的实现方法

    2021-04-24 13:25:12
  • python中Scrapy shell的使用

    2021-03-23 08:23:24
  • asp之家 网络编程 m.aspxhome.com