您的位置:首页 > 编程语言 > Python开发

python中的time,datetime,logger模块

2016-09-03 18:07 561 查看
      以前学python的时候,里面关于时间的模块学的很糙,没太明白,所以今天又复习了一下,记了一下笔记

# -*- coding: utf-8 -*-

import time,datetime

t = time.time() #返回当前时间戳
print(t)

t2 = time.ctime()  #返回当前时间字符串 星期几 几月几号
print(t2)
t3 = time.ctime(time.time()-86400)  #减去一天
print(t3)

print(''.center(40,'-'))
t4 = time.gmtime() #将时间转换成struct_time格式   默认格林尼治时间
print(t4)

time_str = '{year}-{month}-{day} {hour}:{minute}:{second}'.format(year=t4.tm_year,
month=t4.tm_mon,
day=t4.tm_mday,
hour=t4.tm_hour,
minute=t4.tm_min,
second=t4.tm_sec)
print(time_str)

#获得本地时区的格式化时间
t5 = time.localtime()
local_time_str = '{year}-{month}-{day} {hour}:{minute}:{second}'.format(year=t5.tm_year,
month=t5.tm_mon,
day=t5.tm_mday,
hour=t5.tm_hour,
minute=t5.tm_min,
second=t5.tm_sec)
print(local_time_str)
print(''.center(40,'-'))

t6 = time.mktime(t4)  #将一个时间对象 转成时间戳
print(t6)

#输出你想要的时间字符串样式
time_str_type = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
print(time_str_type,type(time_str_type))  #字符串

# 转成 struct_time 时间格式
time_obj_type = time.strptime(time_str_type,'%Y-%m-%d %H:%M:%S')
print(time_obj_type,type(time_obj_type))
print(time.mktime(time_obj_type))  #转成时间戳

print(''.center(40,'-'))
current_time = datetime.date.today()
print(current_time)  #输出格式 2016-09-03
print(time.time())  #输出时间戳
print(datetime.date.fromtimestamp(time.time()))  #将时间戳转成时期格式
print(datetime.datetime.now())  #输出当前时间
print(current_time.timetuple())  #返回struct_time 格式时间

print(''.center(40,'-'))

#将字符串转成时期格式
str_to_date = datetime.datetime.strptime('1/9/16 14:30:26','%d/%m/%y %H:%M:%S')
print(str_to_date)

new_date = datetime.datetime.now() + datetime.timedelta(days=10)#比现在加10天
print(new_date,'比现在加10天')

new_date = datetime.datetime.now() + datetime.timedelta(days=-10)#比现在减去10天
print(new_date,'比现在减去10天')

new_date = datetime.datetime.now() + datetime.timedelta(hours=10)#比现在加10个小时
print(new_date,'比现在加10个小时')

print(datetime.datetime.now(),'现在时间')

new_date = datetime.datetime.now() + datetime.timedelta(seconds=120)#比现在加120秒
print(new_date,'比现在加120秒')

 日志(logger)也是我们开发中经常用到的东西,代码如下

# -*- coding: utf-8 -*-

import logging

#create logger
logger = logging.getLogger('TEST-LOG')  #创建log对象
logger.setLevel(logging.INFO)  #设置全局的日志级别   其他日志级别只能比我高 不能比我低

# create console handler and set level to debug
console_log = logging.StreamHandler()   #输出到控制台(屏幕)
console_log.setLevel(logging.DEBUG)  #debug及以上级别开始

# create file handler and set level to warning
file_log = logging.FileHandler("access.log")  #输出到文件
file_log.setLevel(logging.WARNING) #warning及以上级别开始

# create formatter
formatter_console = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')  #格式化的样子
formatter_file =  logging.Formatter('%(asctime)s - %(filename)s '
'- %(funcName)s- %(lineno)d- '
'-%(levelname)s - %(message)s')

# add formatter to ch and fh
console_log.setFormatter(formatter_console)  #设置输出到屏幕的日志格式化
file_log.setFormatter(formatter_file)  #输出到文件的日志格式化

# add ch and fh to logger
logger.addHandler(console_log)  #创建的输出到控制台的logger对象注册到全局对象里面
logger.addHandler(file_log)      #创建的输出到文件的logger对象注册到全局对象里面

# 'application' code  级别从低到高
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: