python中calendar模块常用方法_Python的time模块中的常用方法整理
import time
print time.gmtime()
#獲取當前時間的struct_time對象
print time.gmtime(time.time() - 24 * 60 * 60)
#獲取昨天這個時間的struct_time對象
#---- result
#time.struct_time(tm_year=2009, tm_mon=6, tm_mday=23, tm_hour=15, tm_min=16, tm_sec=3, tm_wday=1, tm_yday=174, tm_isdst=0)
#time.struct_time(tm_year=2009, tm_mon=6, tm_mday=22, tm_hour=15, tm_min=16, tm_sec=3, tm_wday=0, tm_yday=173, tm_isdst=0)
time.localtime
time.localtime與time.gmtime非常類似,也返回一個struct_time對象,可以把它看作是gmtime()的本地化版本。
time.mktime
time.mktime執行與gmtime(), localtime()相反的操作,它接收struct_time對象作為參數,返回用秒數來表示時間的浮點數。例如:
import time
#下面兩個函數返回相同(或相近)的結果
print time.mktime(time.localtime())
print time.time()
time.strftime
time.strftime將日期轉換為字符串表示,它的函數原型為:time.strftime(format[, t])。參數format是格式字符串(格式字符串的知識可以參考:time.strftime),可選的參數t是一個struct_time對象。下面的例子將struct_time對象轉換為字符串表示:
import time
print time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())
print time.strftime('Weekday: %w; Day of the yesr: %j')
#---- result
#2009-06-23 15:30:53
#Weekday: 2; Day of the yesr: 174
time.strptime
按指定格式解析一個表示時間的字符串,返回struct_time對象。該函數原型為:time.strptime(string, format),兩個參數都是字符串,下面是一個簡單的例子,演示將一個字符串解析為一個struct_time對象:
import time
print time.strptime('2009-06-23 15:30:53', '%Y-%m-%d %H:%M:%S')
#---- result
#time.struct_time(tm_year=2009, tm_mon=6, tm_mday=23, tm_hour=15, tm_min=30, tm_sec=53, tm_wday=1, tm_yday=174, tm_isdst=-1)
以上介紹的方法是time模塊中最常用的幾個方法,在Python手冊中還介紹了其他的方法和屬性,如:time.timezone, time.tzname …感興趣的朋友可以參考Python手冊 time 模塊。
本條技術文章來源于互聯網,如果無意侵犯您的權益請點擊此處反饋版權投訴
總結
以上是生活随笔為你收集整理的python中calendar模块常用方法_Python的time模块中的常用方法整理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 全排列代码python_python全排
- 下一篇: 多模态理论张德禄_结构动力学中的模态分析