{"id":5479,"date":"2022-04-02T21:29:23","date_gmt":"2022-04-02T13:29:23","guid":{"rendered":"https:\/\/egonlin.com\/?p=5479"},"modified":"2022-04-02T21:29:23","modified_gmt":"2022-04-02T13:29:23","slug":"03-04-time%e5%ba%93%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=5479","title":{"rendered":"03-04 time\u5e93\u7684\u4f7f\u7528"},"content":{"rendered":"<h1>\u4e00\u3001\u6982\u8ff0<\/h1>\n<ul>\n<li>time\u5e93\u57fa\u672c\u4ecb\u7ecd<\/li>\n<li>\u65f6\u95f4\u83b7\u53d6<\/li>\n<li>\u65f6\u95f4\u683c\u5f0f\u5316<\/li>\n<li>\u7a0b\u5e8f\u8ba1\u65f6\u5e94\u7528<\/li>\n<\/ul>\n<h1>\u4e8c\u3001time\u5e93\u57fa\u672c\u4ecb\u7ecd<\/h1>\n<p>time\u5e93\u662fPython\u4e2d\u5904\u7406\u65f6\u95f4\u7684\u6807\u51c6\u5e93<\/p>\n<ul>\n<li>\u8ba1\u7b97\u673a\u65f6\u95f4\u7684\u8868\u8fbe<\/li>\n<li>\u63d0\u4f9b\u83b7\u53d6\u7cfb\u7edf\u65f6\u95f4\u5e76\u683c\u5f0f\u5316\u8f93\u51fa\u529f\u80fd<\/li>\n<li>\u63d0\u4f9b\u7cfb\u7edf\u7ea7\u7cbe\u786e\u8ba1\u65f6\u529f\u80fd\uff0c\u7528\u4e8e\u7a0b\u5e8f\u6027\u80fd\u5206\u6790<\/li>\n<\/ul>\n<h2>2.1 time\u5e93\u6982\u8ff0<\/h2>\n<p>time\u5e93\u5305\u62ec\u4e09\u7c7b\u51fd\u6570<\/p>\n<ul>\n<li>\u65f6\u95f4\u83b7\u53d6\uff1a<code>time()\uff0cctime()\uff0cgmtime()<\/code><\/li>\n<li>\u65f6\u95f4\u683c\u5f0f\u5316\uff1a<code>strftime()\uff0cstrptime()<\/code><\/li>\n<li>\u7a0b\u5e8f\u8ba1\u65f6\uff1a<code>sleep()\uff0cperf_counter()<\/code><\/li>\n<\/ul>\n<h1>\u4e09\u3001\u65f6\u95f4\u83b7\u53d6<\/h1>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u51fd\u6570<\/th>\n<th style=\"text-align: center;\">\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\"><code>time()<\/code><\/td>\n<td style=\"text-align: center;\">\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233\uff0c\u5373\u8ba1\u7b97\u673a\u5185\u90e8\u65f6\u95f4\u503c\uff0c\u6d6e\u70b9\u6570<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\"><code>ctime()<\/code><\/td>\n<td style=\"text-align: center;\">\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5e76\u4ee5\u6613\u8bfb\u65b9\u5f0f\u8868\u793a\uff0c\u8fd4\u56de\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\"><code>gmtime()<\/code><\/td>\n<td style=\"text-align: center;\">\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff0c\u8868\u793a\u4e3a\u8ba1\u7b97\u673a\u53ef\u5904\u7406\u7684\u65f6\u95f4\u683c\u5f0f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre><code class=\"language-python\">import time<\/code><\/pre>\n<pre><code class=\"language-python\">time.time()<\/code><\/pre>\n<pre><code>1571026828.488849<\/code><\/pre>\n<pre><code class=\"language-python\">time.ctime()<\/code><\/pre>\n<pre><code>&#039;Mon Oct 14 12:20:58 2019&#039;<\/code><\/pre>\n<pre><code class=\"language-python\">time.gmtime()<\/code><\/pre>\n<pre><code>time.struct_time(tm_year=2019, tm_mon=10, tm_mday=14, tm_hour=4, tm_min=21, tm_sec=9, tm_wday=0, tm_yday=287, tm_isdst=0)<\/code><\/pre>\n<h1>\u56db\u3001\u65f6\u95f4\u683c\u5f0f\u5316<\/h1>\n<p>\u5c06\u65f6\u95f4\u4ee5\u5408\u7406\u7684\u65b9\u5f0f\u5c55\u793a\u51fa\u6765<\/p>\n<ul>\n<li>\u683c\u5f0f\u5316\uff1a\u7c7b\u4f3c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\uff0c\u9700\u8981\u6709\u5c55\u793a\u6a21\u677f<\/li>\n<li>\u5c55\u793a\u6a21\u677f\u7531\u7279\u5b9a\u7684\u683c\u5f0f\u5316\u63a7\u5236\u7b26\u7ec4\u6210<\/li>\n<li>strftime()\u65b9\u6cd5<\/li>\n<\/ul>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u51fd\u6570<\/th>\n<th style=\"text-align: left;\">\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\"><code>strftime(tpl, ts)<\/code><\/td>\n<td style=\"text-align: left;\">tpl\u662f\u683c\u5f0f\u5316\u6a21\u677f\u5b57\u7b26\u4e32\uff0c\u7528\u6765\u5b9a\u4e49\u8f93\u51fa\u6548\u679c\uff1bts\u662f\u8ba1\u7b97\u673a\u5185\u90e8\u65f6\u95f4\u7c7b\u578b\u53d8\u91cf<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre><code class=\"language-python\">t = time.gmtime()<\/code><\/pre>\n<pre><code class=\"language-python\">time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;,t)<\/code><\/pre>\n<pre><code>&#039;2019-10-14 04:21:44&#039;<\/code><\/pre>\n<h2>4.1 \u683c\u5f0f\u5316\u63a7\u5236\u7b26<\/h2>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/th>\n<th style=\"text-align: center;\">\u65e5\u671f\/\u65f6\u95f4\u8bf4\u660e<\/th>\n<th style=\"text-align: center;\">\u503c\u8303\u56f4\u548c\u5b9e\u4f8b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">%Y<\/td>\n<td style=\"text-align: center;\">\u5e74\u4efd<\/td>\n<td style=\"text-align: center;\">0000~9999\uff0c\u4f8b\u5982\uff1a1900<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">%m<\/td>\n<td style=\"text-align: center;\">\u6708\u4efd<\/td>\n<td style=\"text-align: center;\">01~12\uff0c\u4f8b\u5982\uff1a10<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">%B<\/td>\n<td style=\"text-align: center;\">\u6708\u4efd\u540d\u79f0<\/td>\n<td style=\"text-align: center;\">January~December\uff0c\u4f8b\u5982\uff1aApril<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">%b<\/td>\n<td style=\"text-align: center;\">\u6708\u4efd\u540d\u79f0\u7f29\u5199<\/td>\n<td style=\"text-align: center;\">Jan~Dec\uff0c\u4f8b\u5982\uff1aApr<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">%d<\/td>\n<td style=\"text-align: center;\">\u65e5\u671f<\/td>\n<td style=\"text-align: center;\">01~31\uff0c\u4f8b\u5982\uff1a25<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">%A<\/td>\n<td style=\"text-align: center;\">\u661f\u671f<\/td>\n<td style=\"text-align: center;\">Monday~Sunday\uff0c\u4f8b\u5982\uff1aWednesday<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">%a<\/td>\n<td style=\"text-align: center;\">\u661f\u671f\u7f29\u5199<\/td>\n<td style=\"text-align: center;\">Mon~Sun\uff0c\u4f8b\u5982\uff1aWed<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">%H<\/td>\n<td style=\"text-align: center;\">\u5c0f\u65f6\uff0824h\u5236\uff09<\/td>\n<td style=\"text-align: center;\">00~23\uff0c\u4f8b\u5982\uff1a12<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">%h<\/td>\n<td style=\"text-align: center;\">\u5c0f\u65f6\uff0812h\u5236\uff09<\/td>\n<td style=\"text-align: center;\">01~12\uff0c\u4f8b\u5982\uff1a7<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">%p<\/td>\n<td style=\"text-align: center;\">\u4e0a\/\u4e0b\u5348<\/td>\n<td style=\"text-align: center;\">AM, PM\uff0c\u4f8b\u5982\uff1aPM<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">%M<\/td>\n<td style=\"text-align: center;\">\u5206\u949f<\/td>\n<td style=\"text-align: center;\">00~59\uff0c\u4f8b\u5982\uff1a26<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">%S<\/td>\n<td style=\"text-align: center;\">\u79d2<\/td>\n<td style=\"text-align: center;\">00~59\uff0c\u4f8b\u5982\uff1a26<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>4.2 \u65f6\u95f4\u683c\u5f0f\u5316-\u4ee3\u7801<\/h2>\n<pre><code class=\"language-python\">import time\n\nt = time.gmtime()\nt<\/code><\/pre>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u51fd\u6570<\/th>\n<th style=\"text-align: center;\">\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\"><code>strptime(str, tpl)<\/code><\/td>\n<td style=\"text-align: center;\">str\u662f\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u65f6\u95f4\u503c\uff1btpl\u662f\u683c\u5f0f\u5316\u6a21\u677f\u5b57\u7b26\u4e32\uff0c\u7528\u6765\u5b9a\u4e49\u8f93\u5165\u6548\u679c\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre><code class=\"language-python\">timeStr = &#039;2018-01-26 12:55:20&#039;\ntime.strptime(timeStr, &quot;%Y-%m-%d %H:%M:%S&quot;)<\/code><\/pre>\n<h1>\u4e94\u3001\u7a0b\u5e8f\u8ba1\u65f6\u5e94\u7528<\/h1>\n<h2>5.1 \u7a0b\u5e8f\u8ba1\u65f6<\/h2>\n<p>\u7a0b\u5e8f\u8ba1\u65f6\u5e94\u7528\u5e7f\u6cdb<\/p>\n<ul>\n<li>\u7a0b\u5e8f\u8ba1\u65f6\u6307\u6d4b\u91cf\u8d77\u6b62\u52a8\u4f5c\u6240\u7ecf\u5386\u65f6\u95f4\u7684\u8fc7\u7a0b<\/li>\n<li>\u6d4b\u91cf\u65f6\u95f4\uff1a<code>perf_counter()<\/code><\/li>\n<li>\u4ea7\u751f\u65f6\u95f4\uff1a<code>sleep()<\/code><\/li>\n<\/ul>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u51fd\u6570<\/th>\n<th style=\"text-align: center;\">\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\"><code>perf_counter()<\/code><\/td>\n<td style=\"text-align: center;\">\u8fd4\u56de\u4e00\u4e2aCPU\u7ea7\u522b\u7684\u7cbe\u786e\u65f6\u95f4\u8ba1\u6570\u503c\uff0c\u5355\u4f4d\u4e3a\u79d2\uff1b\u7531\u4e8e\u8fd9\u4e2a\u8ba1\u6570\u503c\u8d77\u70b9\u4e0d\u786e\u5b9a\uff0c\u8fde\u7eed\u8c03\u7528\u5dee\u503c\u624d\u6709\u610f\u4e49<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre><code class=\"language-python\">start = time.perf_counter()\nstart<\/code><\/pre>\n<pre><code>3913.931866374<\/code><\/pre>\n<pre><code>end = time.perf_counter()\nend<\/code><\/pre>\n<pre><code>3916.573579334<\/code><\/pre>\n<pre><code class=\"language-python\">end-start<\/code><\/pre>\n<pre><code>2.64171295999995<\/code><\/pre>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u51fd\u6570<\/th>\n<th style=\"text-align: center;\">\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\"><code>sleep(s)<\/code><\/td>\n<td style=\"text-align: center;\">s\u62df\u4f11\u7720\u7684\u65f6\u95f4\uff0c\u5355\u4f4d\u662f\u79d2\uff0c\u53ef\u4ee5\u662f\u6d6e\u70b9\u6570<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre><code class=\"language-python\">import time\n\ndef wait():\n    time.sleep(3.3)\n\nwait()  # #\u7a0b\u5e8f\u5c06\u7b49\u5f853.3\u79d2\u540e\u518d\u9000\u51fa<\/code><\/pre>\n<h1>\u516d\u3001\u5355\u5143\u5c0f\u7ed3<\/h1>\n<ul>\n<li>\u65f6\u95f4\u83b7\u53d6\uff1a<code>time()\uff0cctime()\uff0cgmtime()<\/code><\/li>\n<li>\u65f6\u95f4\u683c\u5f0f\u5316\uff1a<code>strftime()\uff0cstrptime()<\/code><\/li>\n<li>\u7a0b\u5e8f\u8ba1\u65f6\uff1a<code>perf_counter()\uff0csleep()<\/code><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u6982\u8ff0 time\u5e93\u57fa\u672c\u4ecb\u7ecd \u65f6\u95f4\u83b7\u53d6 \u65f6\u95f4\u683c\u5f0f\u5316 \u7a0b\u5e8f\u8ba1\u65f6\u5e94\u7528 \u4e8c\u3001time\u5e93\u57fa\u672c\u4ecb\u7ecd time\u5e93\u662fPyth [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":5473,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[371,374],"tags":[],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/5479"}],"collection":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5479"}],"version-history":[{"count":0,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/5479\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/media\/5473"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}