时间模块

1 时间模块优先掌握的操作

1.1 time模块

时间分为三种格式:

1.1.1 时间戳

时间戳是从1970年到现在经过的秒数
作用:用于时间间隔的计算

import time

print(time.time())

1.1.2 格式化时间

按照某种格式显示的时间:2020-03-30 11:11:11
作用:用于展示时间

import time

print(time.strftime('%Y-%m-%d %H:%M:%S %p'))
print(time.strftime('%Y-%m-%d %X'))
%a    Locale’s abbreviated weekday name.     
%A    Locale’s full weekday name.     
%b    Locale’s abbreviated month name.     
%B    Locale’s full month name.     
%c    Locale’s appropriate date and time representation.     
%d    Day of the month as a decimal number [01,31].     
%H    Hour (24-hour clock) as a decimal number [00,23].     
%I    Hour (12-hour clock) as a decimal number [01,12].     
%j    Day of the year as a decimal number [001,366].     
%m    Month as a decimal number [01,12].     
%M    Minute as a decimal number [00,59].     
%p    Locale’s equivalent of either AM or PM.    (1)
%S    Second as a decimal number [00,61].    (2)
%U    Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.    (3)
%w    Weekday as a decimal number [0(Sunday),6].     
%W    Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.    (3)
%x    Locale’s appropriate date representation.     
%X    Locale’s appropriate time representation.     
%y    Year without century as a decimal number [00,99].     
%Y    Year with century as a decimal number.     
%z    Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].     
%Z    Time zone name (no characters if no time zone exists).     
%%    A literal '%' character.

格式化字符串的时间格式

1.1.3 结构化的时间

作用:用于单独获取时间的某一部分

import time

res=time.localtime()
print(res)
print(res.tm_year)
print(res.tm_yday)
# time.struct_time(tm_year=2020, tm_mon=3, tm_mday=30, tm_hour=22, tm_min=22, tm_sec=17, tm_wday=0, tm_yday=90, tm_isdst=0)
# 2020
# 90

1.1.4 sleep

可以将代码等待一定时间

import time
time.sleep(3)

1.2 datetime模块

import datetime
# 可以直接做时间的加减

print(datetime.datetime.now())
print(datetime.datetime.now() + datetime.timedelta(days=3))
print(datetime.datetime.now() + datetime.timedelta(weeks=1))

2 时间模块需要掌握的操作

2.1 时间格式的转换

2.1.1 struct_time->时间戳

import time
s_time=time.localtime()
print(time.mktime(s_time))

2.1.2 时间戳->struct_time

import time
tp_time=time.time()
print(time.localtime(tp_time))

print(time.localtime())# 本地时间
print(time.gmtime()) # 世界标准时间
print(time.localtime(333333333))
print(time.gmtime(333333333))

2.1.3 struct_time->格式化的字符串形式的时间

import time
s_time=time.localtime()

print(time.strftime('%Y-%m-%d %H:%M:%S',s_time))

2.1.4 格式化的字符串形式的时间->struct_time

import time

print(time.strptime('1988-03-03 11:11:11','%Y-%m-%d %H:%M:%S'))

2.1.5 format string<------>timestamp

# 如果想实现'1988-03-03 11:11:11'+7天

# format string--->struct_time--->timestamp
struct_time=time.strptime('1988-03-03 11:11:11','%Y-%m-%d %H:%M:%S')
timestamp=time.mktime(struct_time)+7*24*3600
print(timestamp)

# format string<---struct_time<---timestamp
res=time.strftime('%Y-%m-%d %X',time.localtime(timestamp))
print(res)

3 时间模块仅需了解的操作

import time
print(time.asctime())
# Mon Mar 30 22:34:59 2020


import datetime
print(datetime.datetime.now())
# 2020-03-30 22:34:59.313774

print(datetime.datetime.utcnow())
# 2020-03-30 14:34:59.313774

print(datetime.datetime.fromtimestamp(333333))
# 1970-01-05 04:35:33

4 实例应用:打印进度条

import time

def progress(percent):
    if percent > 1:
        percent = 1
    res = int(50 * percent) * '#'
    print('\r[%-50s] %d%%' % (res, int(100 * percent)), end='')

recv_size=0
total_size=1025011

while recv_size < total_size:
    time.sleep(0.01) # 模拟下载了1024个字节的数据
    recv_size+=1024  # recv_size=1024
	percent = recv_size / total_size  # 1024 / 333333
	progress(percent)
 posted on 2020-03-30 23:24  wwwpy  阅读(264)  评论(0编辑  收藏  举报