首页 > 代码库 > Long Way To Go 之 Python 5

Long Way To Go 之 Python 5

模块

 

time & datetime 模块

 

time 模块

 

技术分享
import time

print("  time applications  ".center(80,"*"))

print(time.process_time()) # 测量处理器运算时间,不包括sleep时间

print(time.time())   # 时间戳,1970 年的时间到现在,以秒计算
print(time.gmtime())  # utc 时间的sturct time(格林威治时间)


print("---------  本地时间  -----------")
print(time.altzone) # 返回与utc时间的时间差,已秒计算
print(time.localtime()) # 返回本地时间的struct time对象格式
print(time.localtime(time.time()))  # 返回本地时间的struct time对象格式
print(time.localtime(time.time()+3600*3))  # 修改本地时间并返回struct time对象格式

t1 = time.localtime(time.time())
print(t1.tm_year,t1.tm_yday)  # year to day    month to day


print("-----------  时间格式  ------------")
# 返回时间格式 week month day H:M:S year
print(time.asctime())
print(time.asctime(time.localtime()))
print(time.ctime())


print("----------  日期字符串 转 时间对象 转 时间戳  -----------")
# 日期字符串 转 时间对象
struct_time = time.strptime("2016-11-11 23:30","%Y-%m-%d %H:%M")
#struct_time = time.strptime("16-11-11 23:30","%y-%m-%d %H:%M")
print(struct_time)

# 获取了时间对象,但是不能对其进行运算,得转成时间戳才能运算
struct_time_stamp = time.mktime(struct_time)  # 转时间戳
print(struct_time_stamp)


print("-----------  时间戳 转 时间对象 转 字符串  -----------")
# 时间戳 转 时间对象
struct_time2 = time.localtime(struct_time_stamp)
print(struct_time2)
# 时间对象 转 字符串
string_time = time.strftime("%Y_%m_%d_%H_%M.log",struct_time2)
print(string_time)
View Code

 

技术分享
*****************************  time applications  ******************************
0.156001
1494746244.919801
time.struct_time(tm_year=2017, tm_mon=5, tm_mday=14, tm_hour=7, tm_min=17, tm_sec=24, tm_wday=6, tm_yday=134, tm_isdst=0)
---------  本地时间  -----------
-3600
time.struct_time(tm_year=2017, tm_mon=5, tm_mday=14, tm_hour=8, tm_min=17, tm_sec=24, tm_wday=6, tm_yday=134, tm_isdst=1)
time.struct_time(tm_year=2017, tm_mon=5, tm_mday=14, tm_hour=8, tm_min=17, tm_sec=24, tm_wday=6, tm_yday=134, tm_isdst=1)
time.struct_time(tm_year=2017, tm_mon=5, tm_mday=14, tm_hour=11, tm_min=17, tm_sec=24, tm_wday=6, tm_yday=134, tm_isdst=1)
2017 134
----------  时间格式  ------------
Sun May 14 08:17:24 2017
Sun May 14 08:17:24 2017
Sun May 14 08:17:24 2017
----------  日期字符串 转 时间对象 转 时间戳  -----------
time.struct_time(tm_year=2016, tm_mon=11, tm_mday=11, tm_hour=23, tm_min=30, tm_sec=0, tm_wday=4, tm_yday=316, tm_isdst=-1)
1478907000.0
----------  时间戳 转 时间对象 转 字符串  -----------
time.struct_time(tm_year=2016, tm_mon=11, tm_mday=11, tm_hour=23, tm_min=30, tm_sec=0, tm_wday=4, tm_yday=316, tm_isdst=0)
2016_11_11_23_30.log
View Code

datetime 模块

技术分享
import datetime

print("  datetime applications  ".center(80,"*"))

print("---------------  本地时间  ----------------")
print(datetime.datetime.now()) # 当前本地时间
print(datetime.datetime.fromtimestamp(time.time()))  # 时间戳  直接转  日期格式
#print(datetime.datetime.fromtimestamp(time.time()-3600))  # 时间戳  直接转  日期格式


print("---------------  时间运算  ----------------")
#  时间运算
print(datetime.datetime.now() + datetime.timedelta(days = 3)) # 当前时间 +3 天
print(datetime.datetime.now() - datetime.timedelta(days = 3))  # 当前时间 -3 天
print(datetime.datetime.now() + datetime.timedelta(hours = 3))  # 当前时间 +3 小时
print(datetime.datetime.now() + datetime.timedelta(minutes = 30 ))  # 当前时间 +30 分钟


print("---------------  时间替换  ----------------")
#  时间替换
now = datetime.datetime.now()
print(now.replace(month=1,day=3))
View Code
技术分享
***************************  datetime applications  ****************************
---------------  本地时间  ----------------
2017-05-14 08:17:24.970803
2017-05-14 08:17:24.970804
---------------  时间运算  ----------------
2017-05-17 08:17:24.970803
2017-05-11 08:17:24.970803
2017-05-14 11:17:24.970803
2017-05-14 08:47:24.970803
---------------  时间替换  ----------------
2017-01-03 08:17:24.970803
View Code

 

字符串 & 时间戳 的转换:

 

字符串 --------------------------------------->   时间对象(stuct_time)   ---------------> 时间戳(stuct_time_stamp)

     time.strptime("日期字符串内容","想转的日期格式")                            time.mktime(stuct_time)

 

 

时间戳 -------------------------------------->   时间对象(stuct_time)   ----------------> 字符串(string_time)         

              time.gmtime(stuct_time_stamp)                          time.strftime("想转的日期格式", stuct_time)

                                 or

              time.localtime(stuct_time_stamp)

 

Long Way To Go 之 Python 5