6.9 time module
method | meaning | Remark |
---|---|---|
time.time() | Timestamp | 1561013092.997079 |
time.strftime('%Y-%m-%d %H:%M:%S %p') | Structured formatted string rotation time struct_time | 2019-06-20 10:21:13 AM |
time.strptime('2011-05-05 16:37:06', '%Y-%m-%d %X') | Forwarding the formatted string structured time struct_time | time.struct_time(tm_year=2011, tm_mon=5...) |
time.localtime() | Timestamp turn structured time struct_time East eight-time | time.struct_time(tm_year=2019,tm_mon...) |
time.gmtime() | UTC time stamp the time of the transfer zone structure | time.struct_time(tm_year=2019,tm_mon=6...) |
time.mktime(time.localtime() | The time stamp into a struct_time | 15663646462642646 |
time.asctime(time.localtime() | The Linux into a struct_time display style | Thu Jun 20 14:32:05 2019 |
time.ctime(12312312321) | Linux will display a timestamp into style | Mon Feb 29 10:45:21 p.m. 2360 |
1, the timestamp (in seconds)
import time
print(time.time())
start_time=time.time()
time.sleep(3)
stop_time=time.time()
print(stop_time-start_time)
2, the formatted string
print(time.strftime('%Y-%m-%d %H:%M:%S %p')) # 2019-06-20 10:21:13 AM
print(time.strftime('%Y-%m-%d %X %p')) # 2019-06-20 10:21:13 AM
the strftime ( the format [, T]) # representing time to a tuple or struct_time (as returned by time.localtime () and time.gmtime ()) is converted to time format string. If t is not specified, the incoming time.localtime (). If any element of the tuple in cross-border, ValueError error will be thrown.
Print ( Time. the strftime ( "% D%% Y-X-M-%", Time. localtime ())) # 2019-06-20 00:49:56
3, struct_time () Object
Print ( Time. localtime ()) # Shanghai: East eight districts a time.struct_time (tm_year = 2019, tm_mon = 6, 20 = tm_mday, tm_hour = 10, tm_min = 24-, tm_sec = 52, tm_wday = 3, tm_yday = 171, = 0 the tm_isdst) Print ( time. localtime ( 1111111111)) # s to be converted into a time.struct_time () format, do not fill the current default time Print ( time. localtime (). tm_year) # 2019 Print ( time. localtime (). tm_mday) # 20 is Print ( time. gmtime, ()) region when the difference between eight hours #UTC # time.struct_time (tm_year = 2019, tm_mon = 6, tm_mday = 20, tm_hour = 2, tm_min = 29, tm_sec = 51, tm_wday = 3, tm_yday = 171, tm_isdst = 0)
4, mktime (t): The conversion of a stamp struct_time
print(time.mktime(time.localtime())) #15663646462642646
5、time.strptime()
Print ( Time. the strptime ( '2017/04/08', 'the Y% /% m /% D'))
Time. the strptime ( String [, the format]) # the string into a time format struct_time. In fact it strftime () is the inverse operation.
Print ( Time. the strptime ( '2011-05-05 16:37:06', '%% Y-X-M-% D%'))
# a time.struct_time (tm_year = 2011, tm_mon =. 5, tm_mday =. 5, = 16 tm_hour, = 37 [tm_min, tm_sec =. 6,
# = tm_wday. 3, tm_yday = 125, the tm_isdst = -1)
# in this function, format default: "% a% b% d % H:% M:% S% Y ".
6、time.asctime()
Print ( Time. the asctime ( Time. localtime ())) # 20 is Thu On Jun 2019 14:32:05
the asctime ([ T]): the tuple indicates a time or as expressed in this form struct_time: 'On Jun 20 is 23 is the Sun : 21: 051993 ' .
Print ( Time. asctime ()) # If there are no parameters, it will be time.localtime () as a parameter. Sun Sep 11 00:43:43 2016
7、time.ctime()
Print ( Time. the ctime ( 12,312,312,321)) # model 2360 22:45:21 Mon On Feb 29
#ctime ([secs]): to a timestamp (in seconds calculated in floating point) is converted to that time.asctime () form. If the parameter is not given time or None, it will default time.time () as a parameter. It acts time.asctime (time.localtime (secs)).
Print ( Time. ctime ()) # Sun Sep 11 00:46:38 2016
Print ( Time. ctime ( Time. Time ())) # Sun Sep 11 00:46:38 2016
6.10 datetime module
method | meaning | Remark |
---|---|---|
datetime.datetime.now() | 2019-06-20 17:06:25.170859 | |
datetime.datetime.now() + datetime.timedelta(days=3) | Current time +3 days | 2019-06-23 17:14:24.660116 |
current_time.replace(year=1977) | Change the current time | 1977-06-20 17:18:11.543876 |
datetime.date.fromtimestamp(time.time()) | Date stamp format directly converted into | 2019-08-19 |
Import datetime
print ( . datetime . datetime now () + . datetime timedelta ( . 3)) # current time day +3
print ( . datetime . datetime now () + . datetime timedelta ( - . 3)) # Day -3 current time
print ( datetime. datetime. now () + datetime. timedelta ( hours = . 3)) # current time +3 hours
Print ( datetime. datetime. now () + datetime. timedelta ( minutes = 30)) # current time + 30 minutes
current_time=datetime.datetime.now()
print(current_time.replace(year=1977))#1977-06-20 17:18:11.543876
Print ( datetime. DATE. fromtimestamp ( 1111111111)) # 2005-03-18
Print ( datetime. DATE. fromtimestamp ( Time. Time ())) # stamp date format directly converted into 2016-08-19
6.11 Print progress bar
def progress(percent,width=50):
if percent > 1:
percent=1
show_str=('[%%-%ds]' %width) %(int(width*percent) * '#')
print('\r%s %d%%' %(show_str,int(100*percent)),end='')
import time
recv_size=0
total_size=8097
while recv_size < total_size:
time.sleep(0.1)
recv_size+=80
percent=recv_size / total_size
progress(percent)