1Time
import time print(time.time()) print(time.clock())#计算cpu执行时间 print(time.gmtime())#世界标准时间 print(time.strftime()) #Commonly used format codes: 设置时间第一个参数为设置时间格式 # %Y Year with century as a decimal number. # %m Month as a decimal number [01,12]. # %d Day of the month as a decimal number [01,31]. # %H Hour (24-hour clock) as a decimal number [00,23]. # %M Minute as a decimal number [00,59]. # %S Second as a decimal number [00,61]. # %z Time zone offset from UTC. # %a Locale's abbreviated weekday name. # %A Locale's full weekday name. # %b Locale's abbreviated month name. # %B Locale's full month name. # %c Locale's appropriate date and time representation. # %I Hour (12-hour clock) as a decimal number [01,12]. # %p Locale's equivalent of either AM or PM. print(time.strptime())#字符串时间转换为结构化时间 第一个参数为字符串第二个为时间格式 print(time.ctime())#当前时间格式固定 参数转化为标准时间从1970年开始算 print(time.mktime())#转换为时间戳
import datetime print(datetime.datetime.now())#直接看时间
2logging模块
日志格式:
import logging # logging.debug('debug message') # logging.info('info message') # logging.warning('warning message') # logging.error('error message') # logging.critical('critical message') # # logging.basicConfig(level=logging.DEBUG,#级别 # format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)',#格式 # datefmt='%a,%d %b %Y %H:%M:%S',#时间格式 # filename='/tmp/test.log',#文件目录名字 # filemode='w')#设置输出到文件 logger=logging.getLogger() #创建一个handler,用于写入日志文件 fh=logging.FileHandler('test.log') #再创建一个handler,用于输出控制台 ch=logging.StreamHandler() logger.setLevel(logging.DEBUG) formatter=logging.Formatter('%(asctime)s-%(name)s-%(levelname)s-%(message)') fh.setFormatter(formatter) ch.setFormatter(formatter) logger.addHandler(fh) logger.addHandler(ch)