Python的时间:秒和字符串之间的转换
来源:互联网 发布:whois数据库 编辑:程序博客网 时间:2024/05/22 15:24
http://edu.codepub.com/2010/0725/24551.php
1)秒数 ==》字符串
from
time
import
*
def
secs2str(secs):
return
strftime(
"%Y-%m-%d %H:%M:%S"
,localtime(secs))
>>> secs2str(
1227628280.0
)
'2008-11-25 23:51:20'
2)字符串 ==》 秒数
from
time
import
*
#先将时间字符串解析为9元组,例如:
#2008-11-25 23:51:20,解析为9元组:[2008,11,25,23,51,20,0,0,0]。
>>> tmlist
=
[
2008
,
11
,
25
,
23
,
51
,
20
,
0
,
0
,
0
]
>>> mktime(tmlist)
1227628280.0
完整的函数:
def
secs2str(secs):
""" 将秒转化为字符串 """
if
int
(secs) <
0
:
return
""
return
str
(time.strftime(
"%Y-%m-%d %H:%M:%S"
,time.localtime(secs)))
def
str2secs(tmlist
=
[]):
""" 将字符串的时间转化为秒 """
if
len
(tmlist) !
=
9
:
return
0
return
int
(time.mktime(tmlist))
Python的time模块的部分翻译:
一、简介
time模块提供各种操作时间的函数
说明:一般有两种表示时间的方式:
第一种是时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量),时间戳是惟一的
第二种以数组的形式表示即(struct_time),共有九个元素,分别表示,同一个时间戳的struct_time会因为时区不同而不同
year (four digits, e.g. 1998)
month (1-12)
day (1-31)
hours (0-23)
minutes (0-59)
seconds (0-59)
weekday (0-6, Monday is 0)
Julian day (day in the year, 1-366)
DST (Daylight Savings Time) flag (-1, 0 or 1) 是否是夏令时
If the DST flag is 0, the time is given in the regular time zone;
if it is 1, the time is given in the DST time zone;
if it is -1, mktime() should guess based on the date and time.
二、函数介绍
1.asctime()
asctime([tuple]) -> string
将一个struct_time(默认为当时时间),转换成字符串
Convert a time tuple to a string, e.g. ‘Sat Jun 06 16:26:11 1998′.
When the time tuple is not present, current time as returned by localtime()
is used.
2.clock()
clock() -> floating point number
该函数有两个功能,
在第一次调用的时候,返回的是程序运行的实际时间;
以第二次之后的调用,返回的是自第一次调用后,到这次调用的时间间隔
示例:
view plaincopy to clipboardprint?import timeif __name__ == '__main__': time.sleep(1) print "clock1:%s" % time.clock() time.sleep(1) print "clock2:%s" % time.clock() time.sleep(1) print "clock3:%s" % time.clock()
输出:
clock1:3.35238137808e-006
clock2:1.00004944763
clock3:2.00012040636
其中第一个clock输出的是程序运行时间
第二、三个clock输出的都是与第一个clock的时间间隔
3.sleep(…)
sleep(seconds)
线程推迟指定的时间运行,经过测试,单位为秒,但是在帮助文档中有以下这样一句话,这关是看不懂
“The argument may be a floating point number for subsecond precision.”
4.ctime(…)
ctime(seconds) -> string
将一个时间戳(默认为当前时间)转换成一个时间字符串
例如:
time.ctime()
输出为:’Sat Mar 28 22:24:24 2009′
5.gmtime(…)
gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,tm_sec, tm_wday, tm_yday, tm_isdst)
将一个时间戳转换成一个UTC时区(0时区)的struct_time,如果seconds参数未输入,则以当前时间为转换标准
6.localtime(…)
localtime([seconds]) -> (tm_year,tm_mon,tm_day,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)
将一个时间戳转换成一个当前时区的struct_time,如果seconds参数未输入,则以当前时间为转换标准
7.mktime(…)
mktime(tuple) -> floating point number
将一个以struct_time转换为时间戳
8.strftime(…)
strftime(format[, tuple]) -> string
将指定的struct_time(默认为当前时间),根据指定的格式化字符串输出
python中时间日期格式化符号:
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00=59)
%S 秒(00-59)
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366)
%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身
9.strptime(…)
strptime(string, format) -> struct_time
将时间字符串根据指定的格式化符转换成数组形式的时间
例如:
2009-03-20 11:45:39 对应的格式化字符串为:%Y-%m-%d %H:%M:%S
Sat Mar 28 22:24:24 2009 对应的格式化字符串为:%a %b %d %H:%M:%S %Y
10.time(…)
time() -> floating point number
返回当前时间的时间戳
三、疑点
1.夏令时
在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 time datetime模块详解Time模块:--------------------------time() #以浮点形式返回自Linux新世纪以来经过的秒数。在linux中,00:00:00 UTC,January 1, 1970是新**49**的开始。>>> time.time()1150269086.6630149>>> time.ctime(1150269086.6630149)>>> 'Wed Jun 14 15:11:26 2006'time.ctime([sec])#把秒数转换成日期格式,如果不带参数,则显示当前的时间。>>> import time>>> time.ctime()>>> 'Wed Jun 14 15:02:50 2006'>>> time.ctime(1138068452427683)'Sat Dec 14 04:51:44 1901'>>> time.ctime(os.path.getmtime('E:\\untitleds.bmp'))'Fri Sep 19 16:35:37 2008'>>> time.gmtime(os.path.getmtime('E:\\untitleds.bmp'))time.struct_time(tm_year=2008, tm_mon=9, tm_mday=19, tm_hour=8, tm_min=35,tm_sec=37, tm_wday=4, tm_yday=263, tm_isdst=0)将一个文件的修改时间转换为日期格式(秒 转 日期)>>> time.strftime('%Y-%m-%d %X',time.localtime(os.path.getmtime('E:\\untitleds.bmp')))'2008-09-19 16:35:37'#定时3秒。>>> time.sleep(3)TIME模块参考:---------------------------------#取一个文件的修改时间>>> os.path.getmtime('E:\\untitleds.bmp')1221813337.7626641变量timezone 通用协调时间和本地标准时间的差值,以秒为单位。altzone 通用协调时间和本地夏令时的差值daylight 标志,本地时间是否反映夏令时。tzname (标准时区名,夏令时时区名)函数time() 以浮点数返回纪元至今以来的秒数。clock() 以浮点数返回CPU开始这个process的时间,(或者至上次调用这个函数的时间)sleep() 延迟一段以浮点数表示的秒数。gmtime() 把以秒表示的时间转换为通用协调时序列localtime() 把秒时转换为本地时序列asctime() 将时间序列转换成文本描述ctime() 将秒时转换成文本描述mktime() 将本地时序列转换成秒时strftime() 以指定格式将序列时转为文本描述strptime() 以指定格式从文本描述中解析出时间序列tzset() 改变当地时区值DateTime模块----------------------------datetime 将日期转化为秒------------------------------------->>> import datetime,time>>> time.mktime(datetime.datetime(2009,1,1).timetuple())1230739200.0>>> cc=[2000,11,3,12,43,33] #Attributes: year, month, day, hour, minute,second>>> time.mktime(datetime.datetime(cc[0],cc[1],cc[2],cc[3],cc[4],cc[5]).timetuple())973226613.0将秒转换为日期格式>>> cc = time.localtime(os.path.getmtime('E:\\untitleds.bmp'))>>> print cc[0:3](2008, 9, 19)DateTime示例-----------------演示计算两个日期相差天数的计算>>> import datetime>>> d1 = datetime.datetime(2005, 2, 16)>>> d2 = datetime.datetime(2004, 12, 31)>>> (d1 - d2).days47演示计算运行时间的例子,以秒进行显示import datetimestarttime = datetime.datetime.now()#long runningendtime = datetime.datetime.now()print (endtime - starttime).seconds演示计算当前时间向后10小时的时间。>>> d1 = datetime.datetime.now()>>> d3 = d1 + datetime.timedelta(hours=10)>>> d3.ctime()其本上常用的类有:datetime和timedelta两个。它们之间可以相互加减。每个类都有一些方法和属性可以查看具体的值3)glob可以使用简单的方法匹配某个目录下的所有子目录或文件,用法也很简单。3.1 glob.glob(regression) 返回一个列表3.2 glob.iglob(regression) 返回一个遍历器这个模块简单好用,强力推荐。
- Python的时间:秒和字符串之间的转换
- Python的时间:秒和字符串之间的转换
- Python的时间:秒和字符串之间的转换
- Python的时间:秒和字符串之间的转换
- python进行时间和秒的转换
- python时间与字符串之间的转换
- Android 时间和字符串之间的转换
- Python的时间字符串,不同格式,时间戳之间转换
- Python 时间与秒数的转换
- python时间和时间戳之间的转换
- python时间和时间戳之间的相互转换
- python时间时分秒与秒数的互相转换
- java中字符串和时间之间的转换
- java中字符串和时间之间的转换
- 7.3.2 时间和字符串之间的转换
- CString 时间字符串、COleDateTime 和 CTime 类之间的转换
- CString 时间字符串、COleDateTime 和 CTime 类之间的转换
- oracle 时间和字符串之间的互相转换
- How do I monitor HTTP communication in Eclipse?
- char* 赋值时单引号和双引号的区别?include”“和include<>的区别
- 在Windows下使用命令提示符以及VS提供的CL编译器编译运行C/C++
- opencv 数组与数相乘函数为cvCvtScale()
- JQuery learning(6):JQuery callback function
- Python的时间:秒和字符串之间的转换
- 4. OpenGL综合知识 --- 窗口系统Windows和Linux/X
- C# 向一个DataTable内插入另一个DataTable的行,error“该行已属于另一个表”
- float与double的范围和精度
- 图像格式之--YUV
- js parseInt的陷阱分析小结
- Oracle 11g: How to add/enable/disable/drop a constraint
- 发现一个比较全的处理date格式js类库
- 数据库随机IO优化总结