shanghaipc 发表于 2018-8-16 07:11:56

python模块--时间模块

  python模块--时间模块
  一、python中时间介绍:
  python中时间的表示形式有两种:
  1、时间戳表示法,即以整型或浮点型表示的是一个以秒为单位的时间间隔。这个时间的基础值是从1970年的1月1号零点开始算起。
  2、元组格式表示法,即一种Python的数据结构表示。这个元组有9个整型内容。分别表示不同的时间含义。
  二、datetime模块
  2.1 datetime模块的所有函数
In : dir(datetime)  
Out:
  
['MAXYEAR',
  
'MINYEAR',
  
'__doc__',
  
'__file__',
  
'__name__',
  
'__package__',
  
'date',
  
'datetime',
  
'datetime_CAPI',
  
'time',
  
'timedelta',
  
'tzinfo']
  

  
In :
  2.2 datetime模块函数详解
  datetime.datetime.now()    获取当前系统时间
In : print datetime.datetime.now()  
2016-11-29 12:27:55.391272
  

  
#datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  
#一般用于数据库跟时间有关字段的赋值
  
#In : print datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  
2016-11-29 13:26:22
  datetime.date.today()   获取今天日期
In : print datetime.date.today()  
2016-11-29
  datetime.timedelta(days=x)给定两个时间差值
In : help(datetime.timedelta)  
class timedelta(__builtin__.object)
  
|Difference between two datetime values.
  
|
  
|Methods defined here:
  
|
  
|__abs__(...)
  
|      x.__abs__() <==> abs(x)
  
|
  
|__add__(...)
  
|      x.__add__(y) <==> x+y
  
|
  
|__div__(...)
  
|      x.__div__(y) <==> x/y
  
|
  
|__eq__(...)
  
|      x.__eq__(y) <==> x==y
  
|
  
|__floordiv__(...)
  
|      x.__floordiv__(y) <==> x//y
  
|
  
|__ge__(...)
  
|      x.__ge__(y) <==> x>=y
  
|
  
|__getattribute__(...)
  
|      x.__getattribute__('name') <==> x.name
  

  
##小李子
  
In : print datetime.timedelta(days=1)
  
1 day, 0:00:00
  datetime.datetime.strptime()    将字符串格式转换成时间
##注意这个函数字符串中连接符只能是'-',其他则会报错  
In : print datetime.datetime.strptime("2016-12-20",'%Y-%m-%d')
  
2016-12-20 00:00:00
  

  
In : print datetime.datetime.strptime("2016 12 20",'%Y-%m-%d')
  
---------------------------------------------------------------------------
  
ValueError                              Traceback (most recent call last)
  
<ipython-input-25-aac02a333bd5> in <module>()
  
----> 1 print datetime.datetime.strptime("2016 12 20",'%Y-%m-%d')
  

  
/usr/local/python2.7/lib/python2.7/_strptime.pyc in _strptime(data_string, format)
  
    323   if not found:
  
    324         raise ValueError("time data %r does not match format %r" %
  
--> 325                        (data_string, format))
  
    326   if len(data_string) != found.end():
  
    327         raise ValueError("unconverted data remains: %s" %
  

  
ValueError: time data '2016 12 20' does not match format '%Y-%m-%d'
  

  
In : import time
  

  
In : time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
  
Out: '2016-11-29 13:18:21'
  三、time模块
  3.1 time模块的所有函数
In : dir(time)  
Out:
  
['__doc__',
  
'__file__',
  
'__name__',
  
'__package__',
  
'accept2dyear',
  
'altzone',
  
'asctime',
  
'clock',
  
'ctime',
  
'daylight',
  
'gmtime',
  
'localtime',
  
'mktime',
  
'sleep',
  
'strftime',
  
'strptime',
  
'struct_time',
  
'time',
  
'timezone',
  
'tzname',
  
'tzset']
  3.2 time模块具体函数讲解
  time.time()    -- 返回当前时间戳,浮点数形式。不接受参数
In : time.time()  
Out: 1480398573.196829
  time.sleep() -- 延迟一个时间段,接受整型、浮点型。
In : time.sleep(3)#执行完成后睡眠3秒  time.localtime()      -- 将时间戳转换为本地时间元组格式。接受一个浮点型时间戳参数,其默认值为当前时间戳。
In : time.localtime()  
Out: time.struct_time(tm_year=2016, tm_mon=12, tm_mday=10, tm_hour=11, tm_min=49, tm_sec=26, tm_wday=5, tm_yday=345, tm_isdst=0)
  
In : time.localtime(1481341446.412076)
  
Out: time.struct_time(tm_year=2016, tm_mon=12, tm_mday=10, tm_hour=11, tm_min=44, tm_sec=6, tm_wday=5, tm_yday=345, tm_isdst=0)
  time.asctime()             -- 将时间元组格式转换为字符串形式。接受一个时间元组,其默认值为localtime()返回值
In : time.asctime()  
Out: 'Sat Dec 10 11:51:26 2016'
  

  
In : time.asctime((2016,12,10,11,52,47,5,345,0))
  
Out: 'Sat Dec 10 11:52:47 2016'
  time.strftime()             -- 将时间元组以指定的格式转换为字符串形式。接受字符串格式化串、时间元组。时间元组为可选,默认为localtime()
In : time.strftime("%Y   %H:%M:%S")  
Out: '2016 11:59:55'
  strptime() -- 将指定格式的时间字符串解析为时间元组,strftime()的逆向过程。接受字符串,时间格式2个参数,都是必选。
In : time.strptime('Sat Dec 10 12:00:59 2016')  
Out: time.struct_time(tm_year=2016, tm_mon=12, tm_mday=10, tm_hour=12, tm_min=0, tm_sec=59, tm_wday=5, tm_yday=345, tm_isdst=-1)
页: [1]
查看完整版本: python模块--时间模块