您的位置:首页 > 编程语言 > Python开发

Python 之time模块

2014-03-20 13:37 197 查看
>>> time.
time.__class__(         time.__reduce__(        time.daylight
time.__delattr__(       time.__reduce_ex__(     time.gmtime(
time.__dict__           time.__repr__(          time.localtime(
time.__doc__            time.__setattr__(       time.mktime(
time.__file__           time.__sizeof__(        time.sleep(
time.__format__(        time.__str__(           time.strftime(
time.__getattribute__(  time.__subclasshook__(  time.strptime(
time.__hash__(          time.accept2dyear       time.struct_time(
time.__init__(          time.altzone            time.time(
time.__name__           time.asctime(           time.timezone
time.__new__(           time.clock(             time.tzname
time.__package__        time.ctime(             time.tzset(
1.有关时间的概念:
时间戳:指的是从1970年1月1日00:00:00开始按秒计算的偏移量。
格式化时间字符串:以指定的模式显示
元组时间格式:(年,月,日,时,分,秒,一周的第几天,一年的第几天,是否夏令时)
1.asctime([tuple]) -> string :把一个时间元组转化为:'Sat Jun 06 16:26:11 1998'这种时间格式,如果元组tuple参数没有给出,默认使用的是time.localtime()返回的元组
>>> time.asctime()
'Thu Mar 20 11:28:21 2014'
>>> time.localtime()
time.struct_time(tm_year=2014, tm_mon=3, tm_mday=20, tm_hour=11, tm_min=28, tm_sec=31, tm_wday=3, tm_yday=79, tm_isdst=0)
2.ctime(seconds) -> string :把一个时间戳转换为:'Thu Mar 20 11:33:20 2014'这种格式的字符串,如果秒数参数未给出,则使用time.time()返回的时间戳。
>>> time.ctime()
'Thu Mar 20 11:36:00 2014'
>>> time.time()
1395286565.6086259
3.gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min,
tm_sec, tm_wday, tm_yday, tm_isdst) :把时间戳转换为时间元组,若没给参数则使用当前时间
>>> time.gmtime()
time.struct_time(tm_year=2014, tm_mon=3, tm_mday=20, tm_hour=3, tm_min=36, tm_sec=44, tm_wday=3, tm_yday=79, tm_isdst=0)
4.localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,
tm_sec,tm_wday,tm_yday,tm_isdst):同上
5.mktime(tuple) -> floating point number:将日期元组转换为时间戳
6. strftime(format[, tuple]) -> string :把一个代表时间的元组转换为格式化时间字符串。
%a:本地星期的简写
%A:本地星期的全写
%b :本地月份的简写
%B :本地月份的全称
>>> time.strftime('%a',time.localtime())
'Thu'
>>> time.strftime('%A',time.localtime())
'Thursday'
>>> time.strftime('%b',time.localtime())
'Mar'
>>> time.strftime('%B',time.localtime())
'March'
%c:本地相应日期和时间的表示
>>> time.strftime('%c',time.localtime())
'Thu Mar 20 13:21:30 2014'
%d:一个月中的第几天
%H:一个月中的第几个小时
>>> time.strftime('%H',time.localtime())
'13'
>>> time.strftime('%h',time.localtime())
'Mar'
%Y :年
%m :月
%d :日
%H : 时
%M :分
%S :秒
%X :本地时间
>>> time.strftime('%Y-%m-%d-%X',time.localtime())
'2014-03-20-13:35:15'
>>> time.strftime('%Y-%m-%d-%H%M%S',time.localtime())
'2014-03-20-133555'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ww