Usage time module

## time module

  • Computer start time:
    • London, UK Time: 1970.1.1.0:0:0
    • Beijing: 1970.1.1.8:0:0
  • Timestamp Time:
    • time.time () to get the current time timestamp
    • Timestamp format is a float, for example: 1,581,652,597.4924521
  • Formatting time (str format time):
    • time.strftime('%Y-%m-%d %H:%m:%s')
    • The resulting output is: 2020-02-1411: 02: 1581652723
    • A: represents the day of the week
    • str data type
    • Note: Y, H, M, S must be capitalized, m, d lowercase, if change Y y, then display only the second half of the year, for example: 21 2021 displayed
  • Structured time:
    • time_obj = time.localtime () to get the current time structuring
    • 输出格式:time.struct_time(tm_year=2020, tm_mon=2, tm_mday=14, tm_hour=11, tm_min=50, tm_sec=15, tm_wday=4, tm_yday=45, tm_isdst=0)
    • time Object Type

    Structured intermediate time stamp type time conversion time to time format, and may acquire a value corresponding to each attribute by time_obj. Property.

Time stamp to see the machine, the time is formatted posters, structured intermediate state transition time between the time stamp and time format

graph LR A [Time stamp] - gmtime: Greenwich Mean Time -> B [structured Time] A [Time stamp] - localtime: GMT -> B [structured Time] B [Structured time] - strftime -> C [time format] C [time format] - strptime -> B [structured time] B [structured time] - mktime -> A [time stamp]

Time format conversion:

  • the strptime ( 'need to be converted to time', 'format conversion')
  • the strftime ( 'format conversion', conversion object)
'''时间转换'''
print(time.time())

time_obj = time.localtime(1581655609)
str_time = time.strftime('%H-%m-%d %H:%M:%S', time_obj)
# 从结构化时间戳转换为格式化时间需要两个参数:格式化要求和结构化时间对象
print(str_time)
time_obj = time.strptime('2020-02-14', '%Y-%m-%d')
print(time_obj)
time = time.mktime(time_obj)
print(time)

Guess you like

Origin www.cnblogs.com/ddzc/p/12307131.html