Python3学习之路~5.2 time & datetime模块

time模块

时间相关的操作,时间有三种表示方式:

  • 时间戳               1970年1月1日之后的秒,即:time.time()
  • 格式化的字符串    2014-11-11 11:11,    即:time.strftime('%Y-%m-%d')
  • 结构化时间          元组包含了:年、日、星期(取值0-6, Monday is 0)等... time.struct_time    即:time.localtime()

time模块下常用方法:

asctime([tuple]) -> string(e.g. 'Sat Jun 06 16:26:11 1998')
ctime(seconds) -> string
gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst)
localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)
mktime(tuple) -> floating point number
sleep(seconds)
strftime(format[, tuple]) -> string
strptime(string, format) -> struct_time
time() -> floating point number

常用的格式代码:

格式参照:
%a    本地(Locale)简化星期名称    
%A    本地完整星期名称    
%b    本地简化月份名称   
%B    本地完整月份名称
%c    本地相应的日期和时间表示    
%d    一个月中的第几天(01-31%H    一天中的第几个小时(24小时制,00-23%I    第几个小时(12小时制,01-12%j    一年中的第几天(001-366%m    月份(01-12%M    分钟数(00-59%p    本地AM或PM的相应符
%S    秒(01-61%U    一年中的星期数(00-53,星期天是一个星期的开始)第一个星期天之前的所有天数都放在第0周
%w    一个星期中的第几天(0-6,0是星期天)   
%W    和%U基本相同,不同的是%W以星期一为一个星期的开始
%x    本地相应日期   
%X    本地相应时间    
%y    去掉世纪的年份(00-99%Y    完整的年份    
%z    时区偏移量,指示格式为+ HHMM或-HHMM的UTC / GMT的正负时差,其中H表示小时数,M表示分钟数(-23:59 - +23:59%Z    时区的名字(如果不存在则为空字符) 
%%    ‘%’字符
View Code

代码举例:

import time
# print(help(time)) #查看time的帮助文档
print(time.timezone) # -28800,time模块下的变量,返回difference in seconds between UTC and local standard time,-28800s=-28800/3600=-8h,即东八区,比UTC早8小时
print(time.altzone)  # --32400,time模块下的变量,返回difference in seconds between UTC and local DST time,-32400s=--32400/3600=-9h,即比标准时间早9小时,
                    # 所谓的DST(夏令时),就是利用夏季天亮得早这一自然现象,人为地将时间提前一小时,即比当地标准时间(东八区)早1小时

print(time.time()) #当前时间戳,是一个整数或浮点数,单位秒,如1529976123.6539726
# time.sleep(3) # 当前程序睡眠3秒
print(time.gmtime()) # 返回当前时间的元组,可加时间戳参数
print(time.gmtime(0)) # 返回utc时间的struct time对象格式,即一个代表1970年1月1日的元组:
                      # time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
print(time.gmtime(1529976123.6539726)) #  gmtime([seconds]) -> time tuple
print(time.localtime()) # 返回本地时间的struct time对象格式, localtime([seconds]) -> time tuple
print(time.clock()) # 返回处理器时间,3.3开始已废弃 , 改成了time.process_time()测量处理器运算时间,不包括sleep时间,不稳定,mac上测不出来
print(time.asctime()) # asctime([tuple]) -> string(e.g."Tue Jun 26 09:54:52 2018")
print(time.asctime(time.localtime())) # 同上,返回时间格式"Tue Jun 26 09:54:52 2018"
print(time.ctime()) #  ctime(seconds) -> string(e.g."Tue Jun 26 09:54:52 2018")


# 日期字符串 转成 时间戳
string_struct_time = time.strptime("2018/06/26","%Y/%m/%d") #将 日期字符串 转成 struct时间对象格式
print(string_struct_time) # time.struct_time(tm_year=2018, tm_mon=6, tm_mday=26, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=177, tm_isdst=-1)

struct_stamp_time = time.mktime(string_struct_time) #将struct时间对象转成时间戳 mktime(tuple) -> floating point number
print(struct_stamp_time) # 1529942400.0

# 时间戳 转为 字符串格式
print(time.gmtime(time.time()-86640)) #将utc时间戳转换成struct_time格式
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) # 将utc struct_time格式转成指定的字符串格式
print(time.strftime('%Y-%m-%d') ) # 默认当前时间,2018-06-26
View Code

 

datetime模块

import datetime
import time
'''
datetime.date:表示日期的类。常用的属性有year, month, day
datetime.time:表示时间的类。常用的属性有hour, minute, second, microsecond
datetime.datetime:表示日期时间
datetime.timedelta:表示时间间隔,即两个时间点之间的长度
timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])
strftime("%Y-%m-%d")
'''

print(datetime.datetime.now()) # 当前时间,2018-06-26 10:40:16.553391
#时间加减
print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
print(datetime.datetime.now() - datetime.timedelta(days=5)) # 当前时间-5天
print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分

print(datetime.date.fromtimestamp(time.time()) )  # 时间戳直接转成日期格式 2018-06-26
c_time  = datetime.datetime.now()
print(c_time.replace(minute=3,hour=2)) #时间替换,2018-06-26 02:03:27.844764
View Code

 

posted @ 2018-12-11 10:10  zhengna  阅读(267)  评论(0编辑  收藏  举报