Long Way To Go 之 Python 5

來源:互聯網
上載者:User

標籤:--   style   view   本地   pytho   isp   ace   strong   port   

模組

 

time & datetime 模組

 

time 模組

 

import timeprint("  time applications  ".center(80,"*"))print(time.process_time()) # 測量處理器運算時間,不包括sleep時間print(time.time())   # 時間戳記,1970 年的時間到現在,以秒計算print(time.gmtime())  # utc 時間的sturct time(格林威治時間)print("---------  本地時間  -----------")print(time.altzone) # 返回與utc時間的時間差,已秒計算print(time.localtime()) # 返回本地時間的struct time對象格式print(time.localtime(time.time()))  # 返回本地時間的struct time對象格式print(time.localtime(time.time()+3600*3))  # 修改本地時間並返回struct time對象格式t1 = time.localtime(time.time())print(t1.tm_year,t1.tm_yday)  # year to day    month to dayprint("-----------  時間格式  ------------")# 返回時間格式 week month day H:M:S yearprint(time.asctime())print(time.asctime(time.localtime()))print(time.ctime())print("----------  日期文字 轉 時間對象 轉 時間戳記  -----------")# 日期文字 轉 時間對象struct_time = time.strptime("2016-11-11 23:30","%Y-%m-%d %H:%M")#struct_time = time.strptime("16-11-11 23:30","%y-%m-%d %H:%M")print(struct_time)# 擷取了時間對象,但是不能對其進行運算,得轉成時間戳記才能運算struct_time_stamp = time.mktime(struct_time)  # 轉時間戳記print(struct_time_stamp)print("-----------  時間戳記 轉 時間對象 轉 字串  -----------")# 時間戳記 轉 時間對象struct_time2 = time.localtime(struct_time_stamp)print(struct_time2)# 時間對象 轉 字串string_time = time.strftime("%Y_%m_%d_%H_%M.log",struct_time2)print(string_time)
View Code

 

*****************************  time applications  ******************************0.1560011494746244.919801time.struct_time(tm_year=2017, tm_mon=5, tm_mday=14, tm_hour=7, tm_min=17, tm_sec=24, tm_wday=6, tm_yday=134, tm_isdst=0)---------  本地時間  ------------3600time.struct_time(tm_year=2017, tm_mon=5, tm_mday=14, tm_hour=8, tm_min=17, tm_sec=24, tm_wday=6, tm_yday=134, tm_isdst=1)time.struct_time(tm_year=2017, tm_mon=5, tm_mday=14, tm_hour=8, tm_min=17, tm_sec=24, tm_wday=6, tm_yday=134, tm_isdst=1)time.struct_time(tm_year=2017, tm_mon=5, tm_mday=14, tm_hour=11, tm_min=17, tm_sec=24, tm_wday=6, tm_yday=134, tm_isdst=1)2017 134----------  時間格式  ------------Sun May 14 08:17:24 2017Sun May 14 08:17:24 2017Sun May 14 08:17:24 2017----------  日期文字 轉 時間對象 轉 時間戳記  -----------time.struct_time(tm_year=2016, tm_mon=11, tm_mday=11, tm_hour=23, tm_min=30, tm_sec=0, tm_wday=4, tm_yday=316, tm_isdst=-1)1478907000.0----------  時間戳記 轉 時間對象 轉 字串  -----------time.struct_time(tm_year=2016, tm_mon=11, tm_mday=11, tm_hour=23, tm_min=30, tm_sec=0, tm_wday=4, tm_yday=316, tm_isdst=0)2016_11_11_23_30.log
View Code

datetime 模組

import datetimeprint("  datetime applications  ".center(80,"*"))print("---------------  本地時間  ----------------")print(datetime.datetime.now()) # 當前本地時間print(datetime.datetime.fromtimestamp(time.time()))  # 時間戳記  直接轉  日期格式#print(datetime.datetime.fromtimestamp(time.time()-3600))  # 時間戳記  直接轉  日期格式print("---------------  時間運算  ----------------")#  時間運算print(datetime.datetime.now() + datetime.timedelta(days = 3)) # 目前時間 +3 天print(datetime.datetime.now() - datetime.timedelta(days = 3))  # 目前時間 -3 天print(datetime.datetime.now() + datetime.timedelta(hours = 3))  # 目前時間 +3 小時print(datetime.datetime.now() + datetime.timedelta(minutes = 30 ))  # 目前時間 +30 分鐘print("---------------  時間替換  ----------------")#  時間替換now = datetime.datetime.now()print(now.replace(month=1,day=3))
View Code
***************************  datetime applications  ****************************---------------  本地時間  ----------------2017-05-14 08:17:24.9708032017-05-14 08:17:24.970804---------------  時間運算  ----------------2017-05-17 08:17:24.9708032017-05-11 08:17:24.9708032017-05-14 11:17:24.9708032017-05-14 08:47:24.970803---------------  時間替換  ----------------2017-01-03 08:17:24.970803
View Code

 

字串 & 時間戳記 的轉換:

 

字串 --------------------------------------->   時間對象(stuct_time)   ---------------> 時間戳記(stuct_time_stamp)

     time.strptime("日期文字內容","想轉的日期格式")                            time.mktime(stuct_time)

 

 

時間戳記 -------------------------------------->   時間對象(stuct_time)   ----------------> 字串(string_time)         

              time.gmtime(stuct_time_stamp)                          time.strftime("想轉的日期格式", stuct_time)

                                 or

              time.localtime(stuct_time_stamp)

 

Long Way To Go 之 Python 5

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.