python time模块
2018-06-18 00:42:06来源:未知 阅读 ()
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的时间
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- python3基础之“术语表(2)” 2019-08-13
- python3 之 字符串编码小结(Unicode、utf-8、gbk、gb2312等 2019-08-13
- Python3安装impala 2019-08-13
- 小白如何入门 Python 爬虫? 2019-08-13
- python_字符串方法 2019-08-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash