Python中time库的三种时间格式

    科技2022-07-13  140

    time标准库

    time的三种表示格式

    时间戳 time.time() 结构化时间对象 time.localtime() 时间字符串 time.ctime()

    time.sleep()和Thread里面的Sleep比较像,睡眠等待。

    三种形式的转换

    时间戳——>结构化时间对象 #UTC:格林威治标准时间,比北京时间早八个小时 #file_time是文件的三个时间之一,不填参数默认是time.time(),也就是格林威治标准时间 time.gmtime(file_time) #本地时间,其实也就是北京时间 #file_name也是时间戳格式 time.localtime(file_time) 结构化时间对象——>时间戳 time.mktime(time.localtime()) 结构化时间对象——>时间字符串 #第一个和第二个参数可以自定义 time.strftime("formate","默认为time.localtime()") 时间字符串——>结构化时间对象 time.strptime("str","str对应的格式")

    ctime字符串转化为datetime形式

    import time import dateutil.parser as parser ctime = time.ctime() #将字符串转化成datetime对象 ctime = parser.parse(ctime) print(ctime)
    Processed: 0.010, SQL: 8