{"id":167020,"date":"2019-12-26T11:00:19","date_gmt":"2019-12-26T03:00:19","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=167020"},"modified":"2020-01-01T20:29:20","modified_gmt":"2020-01-01T12:29:20","slug":"time-module-introduction","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/time-module-introduction.html","title":{"rendered":"Python3 Time \u6a21\u5757\u4ecb\u7ecd"},"content":{"rendered":"

Python3 Time \u6a21\u5757\u5305\u542b\u4e86\u8bb8\u591a\u5185\u7f6e\u51fd\u6570\uff0c\u65e2\u6709\u65f6\u95f4\u5904\u7406\u7684\uff0c\u4e5f\u6709\u8f6c\u6362\u65f6\u95f4\u683c\u5f0f\u7684\u3002
\n\"\"<\/p>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\u5e8f\u53f7<\/th>\n\u51fd\u6570\u53ca\u63cf\u8ff0<\/th>\n\u5b9e\u4f8b<\/th>\n<\/tr>\n
1<\/td>\ntime.altzone
\n\u8fd4\u56de\u683c\u6797\u5a01\u6cbb\u897f\u90e8\u7684\u590f\u4ee4\u65f6\u5730\u533a\u7684\u504f\u79fb\u79d2\u6570\u3002\u5982\u679c\u8be5\u5730\u533a\u5728\u683c\u6797\u5a01\u6cbb\u4e1c\u90e8\u4f1a\u8fd4\u56de\u8d1f\u503c\uff08\u5982\u897f\u6b27\uff0c\u5305\u62ec\u82f1\u56fd\uff09\u3002\u5bf9\u590f\u4ee4\u65f6\u542f\u7528\u5730\u533a\u624d\u80fd\u4f7f\u7528\u3002<\/td>\n
\u4ee5\u4e0b\u5b9e\u4f8b\u5c55\u793a\u4e86 altzone()\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n
>>><\/span> import<\/span> time\r\n<\/span>>>><\/span> print<\/span> (<\/span>\"time.altzone %d \"<\/span> %<\/span> time<\/span>.<\/span>altzone<\/span>)<\/span>\r\ntime<\/span>.<\/span>altzone <\/span>-<\/span>28800<\/span><\/pre>\n<\/td>\n<\/tr>\n
2<\/td>\ntime.asctime([tupletime])
\n\u63a5\u53d7\u65f6\u95f4\u5143\u7ec4\u5e76\u8fd4\u56de\u4e00\u4e2a\u53ef\u8bfb\u7684\u5f62\u5f0f\u4e3a\"Tue Dec 11 18:07:14 2008\"\uff082008\u5e7412\u670811\u65e5\u00a0\u5468\u4e8c18\u65f607\u520614\u79d2\uff09\u768424\u4e2a\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u3002<\/td>\n
\u4ee5\u4e0b\u5b9e\u4f8b\u5c55\u793a\u4e86 asctime()\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n
>>><\/span> import<\/span> time\r\n<\/span>>>><\/span> t <\/span>=<\/span> time<\/span>.<\/span>localtime<\/span>()<\/span>\r\n>>><\/span> print<\/span> (<\/span>\"time.asctime(t): %s \"<\/span> %<\/span> time<\/span>.<\/span>asctime<\/span>(<\/span>t<\/span>))<\/span>\r\ntime<\/span>.<\/span>asctime<\/span>(<\/span>t<\/span>):<\/span> Thu<\/span> Apr<\/span>  7<\/span> 10<\/span>:<\/span>36<\/span>:<\/span>20<\/span> 2016<\/span><\/pre>\n<\/td>\n<\/tr>\n
3<\/td>\ntime.clock()<\/a>
\n\u7528\u4ee5\u6d6e\u70b9\u6570\u8ba1\u7b97\u7684\u79d2\u6570\u8fd4\u56de\u5f53\u524d\u7684CPU\u65f6\u95f4\u3002\u7528\u6765\u8861\u91cf\u4e0d\u540c\u7a0b\u5e8f\u7684\u8017\u65f6\uff0c\u6bd4time.time()\u66f4\u6709\u7528\u3002<\/td>\n
\u5b9e\u4f8b<\/a><\/p>\n

\u7531\u4e8e\u8be5\u65b9\u6cd5\u4f9d\u8d56\u64cd\u4f5c\u7cfb\u7edf\uff0c\u5728 Python 3.3 \u4ee5\u540e\u4e0d\u88ab\u63a8\u8350\uff0c\u800c\u5728 3.8 \u7248\u672c\u4e2d\u88ab\u79fb\u9664\uff0c\u9700\u4f7f\u7528\u4e0b\u5217\u4e24\u4e2a\u51fd\u6570\u66ff\u4ee3\u3002<\/p>\n

time<\/span>.<\/span>perf_counter<\/span>()<\/span>  # \u8fd4\u56de\u7cfb\u7edf\u8fd0\u884c\u65f6\u95f4<\/span>\r\ntime<\/span>.<\/span>process_time<\/span>()<\/span>  # \u8fd4\u56de\u8fdb\u7a0b\u8fd0\u884c\u65f6\u95f4<\/span><\/pre>\n<\/td>\n<\/tr>\n
4<\/td>\ntime.ctime([secs])
\n\u4f5c\u7528\u76f8\u5f53\u4e8easctime(localtime(secs))\uff0c\u672a\u7ed9\u53c2\u6570\u76f8\u5f53\u4e8easctime()<\/td>\n
\u4ee5\u4e0b\u5b9e\u4f8b\u5c55\u793a\u4e86 ctime()\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n
>>><\/span> import<\/span> time\r\n<\/span>>>><\/span> print<\/span> (<\/span>\"time.ctime() : %s\"<\/span> %<\/span> time<\/span>.<\/span>ctime<\/span>())<\/span>\r\ntime<\/span>.<\/span>ctime<\/span>()<\/span> :<\/span> Thu<\/span> Apr<\/span>  7<\/span> 10<\/span>:<\/span>51<\/span>:<\/span>58<\/span> 2016<\/span><\/pre>\n<\/td>\n<\/tr>\n
5<\/td>\ntime.gmtime([secs])
\n\u63a5\u6536\u65f6\u95f4\u6233\uff081970\u7eaa\u5143\u540e\u7ecf\u8fc7\u7684\u6d6e\u70b9\u79d2\u6570\uff09\u5e76\u8fd4\u56de\u683c\u6797\u5a01\u6cbb\u5929\u6587\u65f6\u95f4\u4e0b\u7684\u65f6\u95f4\u5143\u7ec4t\u3002\u6ce8\uff1at.tm_isdst\u59cb\u7ec8\u4e3a0<\/td>\n
\u4ee5\u4e0b\u5b9e\u4f8b\u5c55\u793a\u4e86 gmtime()\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n
>>><\/span> import<\/span> time\r\n<\/span>>>><\/span> print<\/span> (<\/span>\"gmtime :\"<\/span>,<\/span> time<\/span>.<\/span>gmtime<\/span>(<\/span>1455508609.34375<\/span>))<\/span>\r\ngmtime <\/span>:<\/span> time<\/span>.<\/span>struct_time<\/span>(<\/span>tm_year<\/span>=<\/span>2016<\/span>,<\/span> tm_mon<\/span>=<\/span>2<\/span>,<\/span> tm_mday<\/span>=<\/span>15<\/span>,<\/span> tm_hour<\/span>=<\/span>3<\/span>,<\/span> tm_min<\/span>=<\/span>56<\/span>,<\/span> tm_sec<\/span>=<\/span>49<\/span>,<\/span> tm_wday<\/span>=<\/span>0<\/span>,<\/span> tm_yday<\/span>=<\/span>46<\/span>,<\/span> tm_isdst<\/span>=<\/span>0<\/span>)<\/span><\/pre>\n<\/td>\n<\/tr>\n
6<\/td>\ntime.localtime([secs]
\n\u63a5\u6536\u65f6\u95f4\u6233\uff081970\u7eaa\u5143\u540e\u7ecf\u8fc7\u7684\u6d6e\u70b9\u79d2\u6570\uff09\u5e76\u8fd4\u56de\u5f53\u5730\u65f6\u95f4\u4e0b\u7684\u65f6\u95f4\u5143\u7ec4t\uff08t.tm_isdst\u53ef\u53d60\u62161\uff0c\u53d6\u51b3\u4e8e\u5f53\u5730\u5f53\u65f6\u662f\u4e0d\u662f\u590f\u4ee4\u65f6\uff09\u3002<\/td>\n
\u4ee5\u4e0b\u5b9e\u4f8b\u5c55\u793a\u4e86 localtime()\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n
>>><\/span> import<\/span> time\r\n<\/span>>>><\/span> print<\/span> (<\/span>\"localtime(): \"<\/span>,<\/span> time<\/span>.<\/span>localtime<\/span>(<\/span>1455508609.34375<\/span>))<\/span>\r\nlocaltime<\/span>():<\/span>  time<\/span>.<\/span>struct_time<\/span>(<\/span>tm_year<\/span>=<\/span>2016<\/span>,<\/span> tm_mon<\/span>=<\/span>2<\/span>,<\/span> tm_mday<\/span>=<\/span>15<\/span>,<\/span> tm_hour<\/span>=<\/span>11<\/span>,<\/span> tm_min<\/span>=<\/span>56<\/span>,<\/span> tm_sec<\/span>=<\/span>49<\/span>,<\/span> tm_wday<\/span>=<\/span>0<\/span>,<\/span> tm_yday<\/span>=<\/span>46<\/span>,<\/span> tm_isdst<\/span>=<\/span>0<\/span>)<\/span><\/pre>\n<\/td>\n<\/tr>\n
7<\/td>\ntime.mktime(tupletime)<\/a>
\n\u63a5\u53d7\u65f6\u95f4\u5143\u7ec4\u5e76\u8fd4\u56de\u65f6\u95f4\u6233\uff081970\u7eaa\u5143\u540e\u7ecf\u8fc7\u7684\u6d6e\u70b9\u79d2\u6570\uff09\u3002<\/td>\n
\u5b9e\u4f8b<\/a><\/td>\n<\/tr>\n
8<\/td>\ntime.sleep(secs)
\n\u63a8\u8fdf\u8c03\u7528\u7ebf\u7a0b\u7684\u8fd0\u884c\uff0csecs\u6307\u79d2\u6570\u3002<\/td>\n
\u4ee5\u4e0b\u5b9e\u4f8b\u5c55\u793a\u4e86 sleep()\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n
#!\/usr\/bin\/python3<\/span>\r\nimport<\/span> time\r\n\r\n<\/span>print<\/span> (<\/span>\"Start : %s\"<\/span> %<\/span> time<\/span>.<\/span>ctime<\/span>())<\/span>\r\ntime<\/span>.<\/span>sleep<\/span>(<\/span> 5<\/span> )<\/span>\r\nprint<\/span> (<\/span>\"End : %s\"<\/span> %<\/span> time<\/span>.<\/span>ctime<\/span>())<\/span><\/pre>\n<\/td>\n<\/tr>\n
9<\/td>\ntime.strftime(fmt[,tupletime])
\n\u63a5\u6536\u4ee5\u65f6\u95f4\u5143\u7ec4\uff0c\u5e76\u8fd4\u56de\u4ee5\u53ef\u8bfb\u5b57\u7b26\u4e32\u8868\u793a\u7684\u5f53\u5730\u65f6\u95f4\uff0c\u683c\u5f0f\u7531fmt\u51b3\u5b9a\u3002<\/td>\n
\u4ee5\u4e0b\u5b9e\u4f8b\u5c55\u793a\u4e86 strftime()\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n
>>><\/span> import<\/span> time\r\n<\/span>>>><\/span> print<\/span> (<\/span>time<\/span>.<\/span>strftime<\/span>(<\/span>\"%Y-%m-%d %H:%M:%S\"<\/span>,<\/span> time<\/span>.<\/span>localtime<\/span>()))<\/span>\r\n2016<\/span>-<\/span>04<\/span>-<\/span>07<\/span> 11<\/span>:<\/span>18<\/span>:<\/span>05<\/span><\/pre>\n<\/td>\n<\/tr>\n
10<\/td>\ntime.strptime(str,fmt='%a %b %d %H:%M:%S %Y')
\n\u6839\u636efmt\u7684\u683c\u5f0f\u628a\u4e00\u4e2a\u65f6\u95f4\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u65f6\u95f4\u5143\u7ec4\u3002<\/td>\n
\u4ee5\u4e0b\u5b9e\u4f8b\u5c55\u793a\u4e86 strptime()\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n
>>><\/span> import<\/span> time\r\n<\/span>>>><\/span> struct_time <\/span>=<\/span> time<\/span>.<\/span>strptime<\/span>(<\/span>\"30 Nov 00\"<\/span>,<\/span> \"%d %b %y\"<\/span>)<\/span>\r\n>>><\/span> print<\/span> (<\/span>\"\u8fd4\u56de\u5143\u7ec4: \"<\/span>,<\/span> struct_time<\/span>)<\/span>\r\n\u8fd4\u56de\u5143\u7ec4:<\/span>  time<\/span>.<\/span>struct_time<\/span>(<\/span>tm_year<\/span>=<\/span>2000<\/span>,<\/span> tm_mon<\/span>=<\/span>11<\/span>,<\/span> tm_mday<\/span>=<\/span>30<\/span>,<\/span> tm_hour<\/span>=<\/span>0<\/span>,<\/span> tm_min<\/span>=<\/span>0<\/span>,<\/span> tm_sec<\/span>=<\/span>0<\/span>,<\/span> tm_wday<\/span>=<\/span>3<\/span>,<\/span> tm_yday<\/span>=<\/span>335<\/span>,<\/span> tm_isdst<\/span>=-<\/span>1<\/span>)<\/span><\/pre>\n<\/td>\n<\/tr>\n
11<\/td>\ntime.time( )
\n\u8fd4\u56de\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233\uff081970\u7eaa\u5143\u540e\u7ecf\u8fc7\u7684\u6d6e\u70b9\u79d2\u6570\uff09\u3002<\/td>\n
\u4ee5\u4e0b\u5b9e\u4f8b\u5c55\u793a\u4e86 time()\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n
>>><\/span> import<\/span> time\r\n<\/span>>>><\/span> print<\/span>(<\/span>time<\/span>.<\/span>time<\/span>())<\/span>\r\n1459999336.1963577<\/span><\/pre>\n<\/td>\n<\/tr>\n
12<\/td>\ntime.tzset()<\/a>
\n\u6839\u636e\u73af\u5883\u53d8\u91cfTZ\u91cd\u65b0\u521d\u59cb\u5316\u65f6\u95f4\u76f8\u5173\u8bbe\u7f6e\u3002<\/td>\n
\u5b9e\u4f8b<\/a><\/td>\n<\/tr>\n
13<\/td>\n
\ntime.perf_counter()<\/b>
\n\u8fd4\u56de\u8ba1\u65f6\u5668\u7684\u7cbe\u51c6\u65f6\u95f4\uff08\u7cfb\u7edf\u7684\u8fd0\u884c\u65f6\u95f4\uff09\uff0c\u5305\u542b\u6574\u4e2a\u7cfb\u7edf\u7684\u7761\u7720\u65f6\u95f4\u3002\u7531\u4e8e\u8fd4\u56de\u503c\u7684\u57fa\u51c6\u70b9\u662f\u672a\u5b9a\u4e49\u7684\uff0c\u6240\u4ee5\uff0c\u53ea\u6709\u8fde\u7eed\u8c03\u7528\u7684\u7ed3\u679c\u4e4b\u95f4\u7684\u5dee\u624d\u662f\u6709\u6548\u7684\u3002<\/td>\n
\u5b9e\u4f8b<\/a><\/td>\n<\/tr>\n
14<\/td>\n
\ntime.process_time() <\/b>
\n\u8fd4\u56de\u5f53\u524d\u8fdb\u7a0b\u6267\u884c CPU \u7684\u65f6\u95f4\u603b\u548c\uff0c\u4e0d\u5305\u542b\u7761\u7720\u65f6\u95f4\u3002\u7531\u4e8e\u8fd4\u56de\u503c\u7684\u57fa\u51c6\u70b9\u662f\u672a\u5b9a\u4e49\u7684\uff0c\u6240\u4ee5\uff0c\u53ea\u6709\u8fde\u7eed\u8c03\u7528\u7684\u7ed3\u679c\u4e4b\u95f4\u7684\u5dee\u624d\u662f\u6709\u6548\u7684\u3002<\/td>\n
<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

Time\u6a21\u5757\u5305\u542b\u4e86\u4ee5\u4e0b2\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u5c5e\u6027\uff1a<\/p>\n\n\n\n\n\n
\u5e8f\u53f7<\/th>\n\u5c5e\u6027\u53ca\u63cf\u8ff0<\/th>\n<\/tr>\n
1<\/td>\ntime.timezone<\/b>
\n\u5c5e\u6027time.timezone\u662f\u5f53\u5730\u65f6\u533a\uff08\u672a\u542f\u52a8\u590f\u4ee4\u65f6\uff09\u8ddd\u79bb\u683c\u6797\u5a01\u6cbb\u7684\u504f\u79fb\u79d2\u6570\uff08>0\uff0c\u7f8e\u6d32;<=0\u5927\u90e8\u5206\u6b27\u6d32\uff0c\u4e9a\u6d32\uff0c\u975e\u6d32\uff09\u3002<\/td>\n<\/tr>\n
2<\/td>\ntime.tzname<\/b>
\n\u5c5e\u6027time.tzname\u5305\u542b\u4e00\u5bf9\u6839\u636e\u60c5\u51b5\u7684\u4e0d\u540c\u800c\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\uff0c\u5206\u522b\u662f\u5e26\u590f\u4ee4\u65f6\u7684\u672c\u5730\u65f6\u533a\u540d\u79f0\uff0c\u548c\u4e0d\u5e26\u7684\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"

Python3 Time \u6a21\u5757\u5305\u542b\u4e86\u8bb8\u591a\u5185\u7f6e\u51fd\u6570\uff0c\u65e2\u6709\u65f6\u95f4\u5904\u7406\u7684\uff0c\u4e5f\u6709\u8f6c\u6362\u65f6\u95f4\u683c\u5f0f\u7684\u3002 \u5e8f\u53f7 \u51fd\u6570\u53ca\u63cf\u8ff0 \u5b9e\u4f8b […]<\/p>\n","protected":false},"author":1481,"featured_media":167023,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-167020","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167020","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1481"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=167020"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167020\/revisions"}],"predecessor-version":[{"id":167024,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/167020\/revisions\/167024"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/167023"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=167020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=167020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=167020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}