龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > python编程 >

python time模块用法实例详解(2)

时间:2014-09-12 11:56来源:网络整理 作者:网络 点击:
分享到:
在struct_time中,夏令时好像没有用,例如 a = (2009, 6, 28, 23, 8, 34, 5, 87, 1) b = (2009, 6, 28, 23, 8, 34, 5, 87, 0) a和b分别表示的是夏令时和标准时间,它们之间转换为

在struct_time中,夏令时好像没有用,例如
a = (2009, 6, 28, 23, 8, 34, 5, 87, 1)
b = (2009, 6, 28, 23, 8, 34, 5, 87, 0)
a和b分别表示的是夏令时和标准时间,它们之间转换为时间戳应该相关3600,但是转换后输出都为646585714.0
 
四、小应用

1.python获取当前时间

time.time() 获取当前时间戳
time.localtime() 当前时间的struct_time形式
time.ctime() 当前时间的字符串形式
  
2.python格式化字符串 

格式化成2009-03-20 11:45:39形式

time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 

格式化成Sat Mar 28 22:24:24 2009形式

time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()) 

 
3.将格式字符串转换为时间戳

a = "Sat Mar 28 22:24:24 2009"
b = time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))

相信本文所述对大家的Python程序设计有一定的借鉴价值。

精彩图集

赞助商链接