time是python的标准库吗_python3关于date和time的标准库
python3中關于日期和時間的標準庫datetime和time,之前都是用的時候隨用隨查,今天系統的看一下用這兩個庫可以做些什么。
1、time標準庫
#首先添加一個time對象,看一下該對象的屬性和方法
>>> import time,datetime
>>> a = time
>>> type(a)
>>> dir(a)
['_STRUCT_TM_ITEMS', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'altzone', 'asctime', 'clock', 'ctime', 'daylight', 'get_clock_info', 'gmtime', 'localtime', 'mktime', 'monotonic', 'perf_counter', 'process_time', 'sleep', 'strftime', 'strptime', 'struct_time', 'time', 'timezone', 'tzname']
使用help(time)查看一下幫助文件,里面有對應方法的說明(英語好是多么重要,我還得翻譯)
Variables:
timezone -- difference in seconds between UTC and local standard time
altzone -- difference in seconds between UTC and local DST time
daylight -- whether local time should reflect DST
tzname -- tuple of (standard time zone name, DST time zone name)
Functions:
time() -- return current time in seconds since the Epoch as a float 返回當前時間的時間戳格式(浮點型)
clock() -- return CPU time since process start as a float 返回進程啟動以來的cpu時間(浮點型)
sleep() -- delay for a number of seconds given as a float 休眠時間
gmtime() -- convert seconds since Epoch to UTC tuple 將一個時間戳類型轉化為time tuple,不加參數就是當前時間
localtime() -- convert seconds since Epoch to local time tuple 當前時間的time tuple
asctime() -- convert time tuple to string time tuple轉化成str型
ctime() -- convert time in seconds to string 將秒數轉化為str型(是這么理解么?)
mktime() -- convert local time tuple to seconds since Epoch 將time tuple轉化為時間戳
strftime() -- convert time tuple to string according to format specification 將time tuple按格式轉化為str型
strptime() -- parse string to time tuple according to format specification 將str型按格式轉化為time tuple
tzset() -- change the local timezone 更改本地時間
上面4個參數基本不怎么用,主要就是下面這些方法會用到
>>> a.time()
1492154597.892713
>>> a.clock()
1.28294687766011e-06
>>> a.sleep(0.001)
>>> time.gmtime(a.time())
time.struct_time(tm_year=2017, tm_mon=4, tm_mday=14, tm_hour=7, tm_min=24, tm_sec=6, tm_wday=4, tm_yday=104, tm_isdst=0)
>>> time.localtime()
time.struct_time(tm_year=2017, tm_mon=4, tm_mday=14, tm_hour=15, tm_min=24, tm_sec=14, tm_wday=4, tm_yday=104, tm_isdst=0)
>>> time.asctime(time.localtime())
'Fri Apr 14 15:24:31 2017'
>>> time.ctime(time.time())
'Fri Apr 14 15:24:46 2017'
>>> time.mktime(time.localtime())
1492155003.0
再來看strptime和strftime,因為這個涉及到格式,所以要說一下time tuple中的格式
幫助文件中也對格式做了說明,不過幫助文檔中并不全面,我在其他人的博客中找到了更全的
%a本地(locale)簡化星期名稱
%A本地完整星期名稱
%b本地簡化月份名稱
%B本地完整月份名稱
%c本地相應的日期和時間表示
%d一個月中的第幾天(01 - 31)
%H一天中的第幾個小時(24小時制,00 - 23)
%I第幾個小時(12小時制,01 - 12)
%j一年中的第幾天(001 - 366)
%m月份(01 - 12)
%M分鐘數(00 - 59)
%p本地am或者pm的相應符
%S秒(01 - 61)
%U一年中的星期數。(00 - 53星期天是一個星期的開始。)第一個星期天之前的所有天數都放在第0周。
%w一個星期中的第幾天(0 - 6,0是星期天)
%W和%U基本相同,不同的是%W以星期一為一個星期的開始。
%x本地相應日期
%X本地相應時間
%y去掉世紀的年份(00 - 99)
%Y完整的年份
%Z時區的名字(如果不存在為空字符)
%%‘%’字符
知道了格式,strptime和strftime的用法了
>>> time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
'2017-04-14 15:50:10'
>>> time.strptime('2017-04-14 15:50:10','%Y-%m-%d %H:%M:%S')
time.struct_time(tm_year=2017, tm_mon=4, tm_mday=14, tm_hour=15, tm_min=50, tm_sec=10, tm_wday=4, tm_yday=104, tm_isdst=-1)
2、datetime標準庫
>>> import datetime
>>> a = datetime
>>> dir(a)
['MAXYEAR', 'MINYEAR', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_divide_and_round', 'date', 'datetime', 'datetime_CAPI', 'time', 'timedelta', 'timezone', 'tzinfo']
>>> dir(a.date)
['__add__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__radd__', '__reduce__', '__reduce_ex__', '__repr__', '__rsub__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', 'ctime', 'day', 'fromordinal', 'fromtimestamp', 'isocalendar', 'isoformat', 'isoweekday', 'max', 'min', 'month', 'replace', 'resolution', 'strftime', 'timetuple', 'today', 'toordinal', 'weekday', 'year']
>>> dir(a.datetime)
['__add__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__radd__', '__reduce__', '__reduce_ex__', '__repr__', '__rsub__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', 'astimezone', 'combine', 'ctime', 'date', 'day', 'dst', 'fold', 'fromordinal', 'fromtimestamp', 'hour', 'isocalendar', 'isoformat', 'isoweekday', 'max', 'microsecond', 'min', 'minute', 'month', 'now', 'replace', 'resolution', 'second', 'strftime', 'strptime', 'time', 'timestamp', 'timetuple', 'timetz', 'today', 'toordinal', 'tzinfo', 'tzname', 'utcfromtimestamp', 'utcnow', 'utcoffset', 'utctimetuple', 'weekday', 'year']
>>> dir(a.time)
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'dst', 'fold', 'hour', 'isoformat', 'max', 'microsecond', 'min', 'minute', 'replace', 'resolution', 'second', 'strftime', 'tzinfo', 'tzname', 'utcoffset']
其實主要就是用的者三個包,而且里面有很多方法都是重復的
>>> a = date(1988,4,29)
>>> a.ctime()
'Fri Apr 29 00:00:00 1988'
>>> a.day
29
>>> a.month
4
>>> a.year
1988
>>> a.toordinal()當前日期距離耶穌生日(- -!公元元年)的天數
725856
>>> b = a.replace(2017,4,14)返回一個新對象,替換原對象的時間(不改變原有對象的屬性)
>>> b
datetime.date(2017, 4, 14)
>>> b.strftime('%Y!%m!%d')按格式將date格式轉化為str
'2017!04!14'
>>> a.timetuple()將date轉化為time tuple
time.struct_time(tm_year=1988, tm_mon=4, tm_mday=29, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=120, tm_isdst=-1
>>> date(1988, 4, 29).isocalendar()Return a 3-tuple containing ISO year, week number, and weekday.(不太理解)
(1988, 17, 5)
>>> (1988, 4, 29).isoformat()Return string in ISO 8601 format, YYYY-MM-DD
'1988-04-29'
>>> date(2017,4,14).isoweekday()返回日期的周數(mon==1,sun==7)
5
>>> date.fromordinal(1988)將一個int型天數轉化為對應的年、月、日數信息
datetime.date(6, 6, 11)
>>> date.fromordinal(365)
datetime.date(1, 12, 31)
>>> date.fromordinal(366)
datetime.date(2, 1, 1)
>>> date.fromtimestamp(time.time())將一個時間戳格式轉化為date格式
datetime.date(2017, 4, 14)
>>> date.max最大的日期數
datetime.date(9999, 12, 31)
>>> date.min最小的日期數
datetime.date(1, 1, 1)
>>> date.today()
datetime.date(2017, 4, 14)當前日期date格式
>>> date.resolution date的最小單位
datetime.timedelta(1)
time與datetime其實大同小異,只是有個別方法不同
總結
以上是生活随笔為你收集整理的time是python的标准库吗_python3关于date和time的标准库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript --- 取得鼠标
- 下一篇: JavaScript --- 表单fo