日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python time模块

發布時間:2023/12/20 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python time模块 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 import time 2 3 # 2種表示時間的方法: 4 # (1)時間戳,從1970.1.1開始 5 # (2)一個9個整數的元組 6 # 這個元組內容: 7 # year (including century, e.g. 1998) 8 # month (1-12) 9 # day (1-31) 10 # hours (0-23) 11 # minutes (0-59) 12 # seconds (0-59) 13 # weekday (0-6, Monday is 0) 14 # Julian day (day in the year, 1-366) 15 # DST (Daylight Savings Time) flag (-1, 0 or 1) 16 # 1 if summer time is in effect, 0 if not, and -1 if unknown 17 18 19 # FUNCTIONS 20 """ 21 time(...) 22 time() -> floating point number 23 24 Return the current time in seconds since the Epoch. 25 Fractions of a second may be present if the system clock provides them. 26 本地的當前時間的時間戳 27 """ 28 print(time.time()) 29 30 """ 31 localtime(...) 32 localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min, 33 tm_sec,tm_wday,tm_yday,tm_isdst) 34 35 Convert seconds since the Epoch to a time tuple expressing local time. 36 When 'seconds' is not passed in, convert the current time instead. 37 沒有參數時本地的當前時間的元組形式 38 """ 39 print(time.localtime()) 40 41 """ 42 gmtime(...) 43 gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min, 44 tm_sec, tm_wday, tm_yday, tm_isdst) 45 46 Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a. 47 GMT). When 'seconds' is not passed in, convert the current time instead. 48 49 If the platform supports the tm_gmtoff and tm_zone, they are available as 50 attributes only. 51 """ 52 print(time.gmtime(time.time())) 53 54 55 """ 56 mktime(...) 57 mktime(tuple) -> floating point number 58 59 Convert a time tuple in local time to seconds since the Epoch. 60 Note that mktime(gmtime(0)) will not generally return zero for most 61 time zones; instead the returned value will either be equal to that 62 of the timezone or altzone attributes on the time module. 63 """ 64 print(time.mktime(time.localtime())) 65 66 67 """ 68 asctime(...) 69 asctime([tuple]) -> string 70 71 Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'. 72 When the time tuple is not present, current time as returned by localtime() 73 is used. 74 tuple轉字符串,默認是當前時間 75 """ 76 print(time.asctime()) # Thu Jan 18 16:36:07 2018 77 78 """ 79 ctime(...) 80 ctime(seconds) -> string 81 82 Convert a time in seconds since the Epoch to a string in local time. 83 This is equivalent to asctime(localtime(seconds)). When the time tuple is 84 not present, current time as returned by localtime() is used. 85 時間戳轉字符串,默認是當前時間 86 """ 87 print(time.ctime()) 88 89 """ 90 strftime(...) 91 strftime(format[, tuple]) -> string 92 93 Convert a time tuple to a string according to a format specification. 94 See the library reference manual for formatting codes. When the time tuple 95 is not present, current time as returned by localtime() is used. 96 97 Commonly used format codes: 98 99 %Y Year with century as a decimal number. 100 %m Month as a decimal number [01,12]. 101 %d Day of the month as a decimal number [01,31]. 102 %H Hour (24-hour clock) as a decimal number [00,23]. 103 %M Minute as a decimal number [00,59]. 104 %S Second as a decimal number [00,61]. 105 %z Time zone offset from UTC. +0800 106 %a Locale's abbreviated weekday name. Thu 107 %A Locale's full weekday name. Thursday 108 %b Locale's abbreviated month name. Jan 109 %B Locale's full month name. January 110 %c Locale's appropriate date and time representation. Thu Jan 18 16:53:35 2018 111 %I Hour (12-hour clock) as a decimal number [01,12]. 04 112 %p Locale's equivalent of either AM or PM. PM 113 114 Other codes may be available on your platform. See documentation for 115 the C library strftime function. 116 格式化元組到字符串 117 """ 118 print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())) 119 120 """ 121 strptime(...) 122 strptime(string, format) -> struct_time 123 124 Parse a string to a time tuple according to a format specification. 125 See the library reference manual for formatting codes (same as 126 strftime()). 127 128 Commonly used format codes: 129 130 %Y Year with century as a decimal number. 131 %m Month as a decimal number [01,12]. 132 %d Day of the month as a decimal number [01,31]. 133 %H Hour (24-hour clock) as a decimal number [00,23]. 134 %M Minute as a decimal number [00,59]. 135 %S Second as a decimal number [00,61]. 136 %z Time zone offset from UTC. 137 %a Locale's abbreviated weekday name. 138 %A Locale's full weekday name. 139 %b Locale's abbreviated month name. 140 %B Locale's full month name. 141 %c Locale's appropriate date and time representation. 142 %I Hour (12-hour clock) as a decimal number [01,12]. 143 %p Locale's equivalent of either AM or PM. 144 145 Other codes may be available on your platform. See documentation for 146 the C library strftime function. 147 字符串到元組 148 """ 149 # 150 print(time.strptime('2018-01-18 16:55:01', '%Y-%m-%d %H:%M:%S')) 151 print(time.strptime('Thu Jan 18 16:55:56 2018', '%c')) 152 153 """ 154 sleep(...) 155 sleep(seconds) 156 157 Delay execution for a given number of seconds. The argument may be 158 a floating point number for subsecond precision. 159 """ 160 """ 161 clock(...) 162 clock() -> floating point number 163 164 Return the CPU time or real time since the start of the process or since 165 the first call to clock(). This has as much precision as the system 166 records. 167 """ 168 def func(): 169 sum = 0 170 for i in range(1000): 171 sum = sum + 1 172 time.sleep(0.01) 173 174 t0 = time.clock() 175 p0 = time.time() 176 print(t0, p0) 177 func() 178 t1 = time.clock() 179 p1 = time.time() 180 print(t1, p1) 181 print(t1-t0, p1-p0) # 0.08869099999999999 10.986821174621582 182 # linux端,clock只統計cpu運行的時間,執行語句,sleep不統計。windows不一樣 183 184 """ 185 get_clock_info(...) 186 get_clock_info(name: str) -> dict 187 188 Get information of the specified clock. 189 """ 190 print(time.get_clock_info('clock')) 191 print(time.get_clock_info('monotonic')) 192 print(time.get_clock_info('perf_counter')) 193 print(time.get_clock_info('process_time')) 194 print(time.get_clock_info('time')) 195 196 197 """ 198 monotonic(...) 199 monotonic() -> float 200 201 Monotonic clock, cannot go backward. 202 系統啟動,開啟的一個時鐘,也就是系統開啟時間 203 """ 204 205 206 """ 207 perf_counter(...) 208 perf_counter() -> float 209 210 Performance counter for benchmarking. 211 系統運行時間 212 """ 213 214 215 """ 216 process_time(...) 217 process_time() -> float 218 219 Process time for profiling: sum of the kernel and user-space CPU time. 220 """ 221 def func(): 222 sum = 0 223 for i in range(1000): 224 sum = sum + 1 225 time.sleep(0.01) 226 227 t0 = time.process_time() 228 p0 = time.time() 229 print(t0, p0) 230 func() 231 t1 = time.process_time() 232 p1 = time.time() 233 print(t1, p1) 234 print(t1-t0, p1-p0) # 0.08869099999999999 10.986821174621582 235 # 統計內核和用戶空間占用cpu的時間

?

轉載于:https://www.cnblogs.com/gundan/p/8311183.html

總結

以上是生活随笔為你收集整理的python time模块的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。