day19 time module
representation of time
There are 3 representations:
- Timestamp: for the machine to see (float format)
- formatted string: for people to see (formatted time)
- tuple: used for computation (structured time)
import time #timestamp #returns a timestamp representing the number of seconds since 1970.1.1 print ( time.time ()) # 1553959680.65606 #String time # The letter representation is fixed , but the format is not fixed, just change it print (time.strftime( " %Y-%m-%d %H:%M:%S " )) # 2019-03-30 23 :44:32 print (time.strftime( " %Y/%m/%d %H:%M:%S " )) # 2019/03/30 23:44:32 print (time.strftime( " %H :%M:%S " )) # 23:44:32 # 结构化时间 struct_time = time.localtime() print(struct_time) # time.struct_time(tm_year=2019, tm_mon=3, tm_mday=30, tm_hour=23, tm_min=45, tm_sec=54, tm_wday=5, tm_yday=89, tm_isdst=0) print(struct_time.tm_year) # 2019
time shift
Timestamps cannot be directly converted to structured time, and can only be mediated by formatted time.
Timestamp -------------localhost/gmtime-------------> structured time
timestamp<---------mktime---------------------------- structured time
import time #Convert timestamp time to formatted time t = time.time() print (t) #Convert the determined timestamp can be converted with localtime print (time.localtime(1553962314 )) #Convert the variable time of dynamic assignment can be used gmtime conversion print (time.gmtime(t)) #Formatted time is converted into timestamp time with mktime conversion print (time.mktime(time.localtime())) #Running result: # 1553962325.1933188 # time.struct_time(tm_year=2019 , tm_mon=3, tm_mday=31, tm_hour=0, tm_min=11, tm_sec=54, tm_wday=6, tm_yday=90, tm_isdst=0) # time.struct_time(tm_year=2019, tm_mon=3, tm_mday=30, tm_hour=16, tm_min=12, tm_sec=5, tm_wday=5, tm_yday=89, tm_isdst=0) # 1553962325.0
String time -------------strptime-------------> structured time
string time <-------------strftime-------------- structured time
#Convert string time to structured time. Converting with strptime requires a specified format print (time.strptime( ' 2000-12.31 ' , ' %Y-%m.%d ' )) # time.struct_time(tm_year=2000, tm_mon =12, tm_mday=31, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=366, tm_isdst=-1) #Convert structured time to string time strftime conversion requires a specified format print (time .strftime( ' %m/%d/%Y %H:%M:%S ' ,time.localtime(3000000000))) # 01/24/2065 13:20:00
ps: some comfortable displays
#Some comfortable display print (time.asctime()) # Sun Mar 31 00:24:40 2019 print (time.asctime(time.localtime(150000000))) # Thu Oct 3 10:40:00 1974 print (time .ctime()) # Sun Mar 31 00:26:46 2019 print (time.ctime(150000000)) # Thu Oct 3 10:40:00 1974
ps: Calculate the time difference:
#Calculate time difference true_time = time.mktime(time.strptime( ' 2017-09-11 08:30:00 ' , ' %Y-%m-%d %H:%M:%S ' )) now_time = time.mktime(time.strptime('2017-09-12 11:00:00','%Y-%m-%d %H:%M:%S')) dif_time = now_time - true_time struct_time = time.gmtime(dif_time) print ( ' The past %d years %d months %d days %d hours %d minutes %d seconds ' %(struct_time.tm_year-1970,struct_time.tm_mon-1 , struct_time.tm_mday-1,struct_time.tm_hour, struct_time.tm_min,struct_time.tm_sec)) # 0 years, 0 months, 1 day, 2 hours, 30 minutes, 0 seconds have passed