python当前时间字符串,Python常用时间操作总结【取得当前时间、时间函数...

python当前时间字符串,Python常⽤时间操作总结【取得当前
时间、时间函数、应⽤等】...中华灵芝宝
本⽂实例讲述了Python常⽤时间操作。分享给⼤家供⼤家参考,具体如下:
我们先导⼊必须⽤到的⼀个module
>>> import time
设置⼀个时间的格式,下⾯会⽤到
>>>ISOTIMEFORMAT='%Y-%m-%d %X'
看⼀下当前的时间,和其他很多语⾔相似这是从epoch(1970 年 1 ⽉ 1 ⽇ 00:00:00)开始到当前的秒数。
>>> time.time()
1180759620.859
上⾯的看不懂,换个格式来看看
>>> time.localtime()
(2007, 6, 2, 12, 47, 7, 5, 153, 0)
localtime返回tuple格式的时间,有⼀个和它类似的函数叫gmtime(),2个函数的差别是时区,gmtime()返回的是0时区的值,localtime返回的是当前时区的值。
>>> time.strftime( ISOTIMEFORMAT, time.localtime() )
'2007-06-02 12:54:29′
⽤上我们的时间格式定义了,使⽤strftime对时间做⼀个转换,如果取现在的时间,time.localtime() 可以不⽤。
>>> time.strftime( ISOTIMEFORMAT, time.localtime( time.time() ) )
'2007-06-02 12:54:31′
>>> time.strftime( ISOTIMEFORMAT, ime( time.time() ) )
'2007-06-02 04:55:02′
上⾯展⽰了gmtime和localtime的区别。
查看时区⽤
>>> time.timezone
-28800
上⾯的值是⼀个秒值,是当前时区和0时区相差的描述,-28800=-8*3600,即为东⼋区。
帖⼏个简单的函数爱思唯尔
def ISOString2Time( s ):
'''
convert a ISO format time to second
from:2006-04-12 16:46:40 to:23123123
把⼀个时间转化为秒
'''
return time.strptime( s, ISOTIMEFORMAT )
def Time2ISOString( s ):
'''
convert second to a ISO format time
from: 23123123 to: 2006-04-12 16:46:40
把给定的秒转化为定义的格式
'''
return time.strftime( ISOTIMEFORMAT, time.localtime( float( s) ) )
def dateplustime( d, t ):
'''
d=2006-04-12 16:46:40
t=2⼩时
return 2006-04-12 18:46:40
计算⼀个⽇期相差多少秒的⽇期,time2sec是另外⼀个函数,可以处理,3天,13分钟,10⼩时等字符串,回头再来写这个,需要结合正则表达式。
'''
return Time2ISOString( time.mktime( ISOString2Time( d ))+time2sec( t ) )
def dateMinDate( d1, d2 ):
'''
minus to iso format date,return seconds
计算2个时间相差多少秒
'''
d1=ISOString2Time( d1 )
d2=ISOString2Time( d2 )
return time.mktime( d1 )-time.mktime( d2 )
⼀、简介
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
该函数有两个功能,
在第⼀次调⽤的时候,返回的是程序运⾏的实际时间;
以第⼆次之后的调⽤,返回的是⾃第⼀次调⽤后,到这次调⽤的时间间隔
⽰例:
import time
if __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
tm2007clock3:2.00012040636
其中第⼀个clock输出的是程序运⾏时间
第⼆、三个clock输出的都是与第⼀个clock的时间间隔
3.sleep(...)
sleep(seconds)
线程推迟指定的时间运⾏,经过测试,单位为秒,但是在帮助⽂档中有以下这样⼀句话,这关是看不懂
“The argument may be a floating point number for subsecond precision.”
ctime(seconds) -> string
将⼀个时间戳(默认为当前时间)转换成⼀个时间字符串
例如:
输出为:'Sat Mar 28 22:24:24 2009'
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形式
2.python格式化字符串
格式化成2009-03-20 11:45:39形式

本文发布于:2024-09-21 05:42:43,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/704216.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:时间   字符串   时区   函数
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议