table of Contents
time()
python time module
- Timestamp:
- To see the computer, 1970-01-01 00:00:00 to the current time, in seconds calculated
- Formatting time (Format String):
- Posters, and returns the string of time "2019-11-16 14:20:42"
- Time Object Format (struct_time):
- It returns a tuple, 9-tuple values
- Year, month, day, hour, minute, second, day of the week, the first day of the year, daylight saving time
- It returns a tuple, 9-tuple values
1, timestamp ( time.time()
)
print(time.time())
#1573885583.36139
2, acquisition time format ( time.strftime()
)
print(time.strftime('%Y-%m-%d %H:%M:%S')) #%X == %H:%M:%S
2019-11-16 14:27:43
3, target acquisition time ( time.locatime()
)
t = time.localtime()
print(t.tm_year)
print(t.tm_mon)
print(t.tm_mday)
2019
11
16
strftime()不带参数默认当前时间
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
#2019-11-16 14:39:43
print( time.strptime('2019-01-01', '%Y-%m-%d'))
#time.struct_time(tm_year=2019, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=1, tm_isdst=-1)
- measure time:
perf_counter()
- Generation time:
sleep()
function | description |
---|---|
perf_counter() |
CPU returns several times a precise time count value in seconds; starting point because the value of this uncertain terms, the difference between successive calls makes sense |
start_time = time.perf_counter() #精确开始时间
end_time = time.perf_counter() #精确结束时间
restime = end_time - start_time
function | description |
---|---|
sleep(s) |
s proposed sleep time, in seconds, may be a float |
datetime()
Mainly used for date and time calculation
Get the current date
import datetime
print(datetime.date.today())
#2019-11-16
Minutes obtain the current date
import datetime
print(datetime.datetime.today())
#2019-11-16 14:49:02.755061
t = datetime.datetime.today()
print(t.year)
print(t.month)
#2019
#11
print(datetime.datetime.now()) #北京时间
print(datetime.datetime.utcnow()) #格林威治
#2019-11-16 14:52:58.447166
#2019-11-16 06:52:58.447166
Calculate date and time
Date Date Time Time = "+" or "-" Time Object
Target = Date Time Time "+" or "-" Date Time
datetime.timedelta(day=7)
Time Object
current_time = datetime.datetime.now() #获取现在时间
print(current_time)
time_obj = datetime.timedelta(days=7) #时间对象,获取7天时间
print(time_obj)
later_time = current_time + time_obj #获取7天后的时间,加上7天
print(later_time)
before = current_time - time_obj #获取7天之前的时间,减上7天
print(before)