time module
1.import time
2. timestamp
- Timestamp (timestamp): timestamp indicates that from January 1970 00:00:00 1st time in seconds calculated.
(模块名)time.time()(方法名)
3. Format time
- Formatted time string (format string): formatting time is an ordinary time string format.
time.strftime('%Y_%m_%d %X') # 2019_08_16 10:26:36
4. Structured time
- Structured time (struct time): struct_time tuple total of nine elements were nine elements, respectively (year, month, day, hour, minute, second, the first few weeks of the year, day of the year, summer time)
time.localtime() # 当地时间(中国)
time.gmtime() # 标准时间
# time.struct_time(tm_year=2019, tm_mon=8, tm_mday=16, tm_hour=19, tm_min=42, tm_sec=44, tm_wday=4, tm_yday=228, tm_isdst=0)
# time.struct_time(tm_year=2019, tm_mon=8, tm_mday=16, tm_hour=11, tm_min=42, tm_sec=44, tm_wday=4, tm_yday=228, tm_isdst=0)
The conversion of three kinds of time
- Formatting time -----> Structured time
- strftime
print(time.strftime('%Y-%m-%d %X', time.localtime()))
# 2019-08-16 19:42:44
- Formatting time ------> Structured time
- striptime
print(time.strptime('2019-08-16 10:30:44', '%Y-%m-%d %X'))
# time.struct_time(tm_year=2019, tm_mon=8, tm_mday=16, tm_hour=10, tm_min=30, tm_sec=44, tm_wday=4, tm_yday=228, tm_isdst=-1)
- Structured -----> timestamps
- mktime
print(time.mktime(time.localtime()))
# 1565955764.0
- Timestamp -----> Structured
- localtime
print(time.localtime(time.time()))
# time.struct_time(tm_year=2019, tm_mon=8, tm_mday=16, tm_hour=19, tm_min=42, tm_sec=44, tm_wday=4, tm_yday=228, tm_isdst=0)
Emphasis
time.time()
time.sleep()