{"id":982822,"date":"2024-12-27T07:14:07","date_gmt":"2024-12-26T23:14:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/982822.html"},"modified":"2024-12-27T07:14:09","modified_gmt":"2024-12-26T23:14:09","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96utc%e6%97%b6%e9%97%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/982822.html","title":{"rendered":"python\u5982\u4f55\u83b7\u53d6utc\u65f6\u95f4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211132\/4feef7f4-a21f-4629-9c6c-e2a6f509552b.webp\" alt=\"python\u5982\u4f55\u83b7\u53d6utc\u65f6\u95f4\" \/><\/p>\n<p><p> \u4e00\u3001<strong>\u4f7f\u7528Python\u83b7\u53d6UTC\u65f6\u95f4\u7684\u65b9\u6cd5\uff1adatetime\u6a21\u5757\u3001pytz\u5e93\u3001time\u6a21\u5757<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u63a8\u8350\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u7ed3\u5408<code>pytz<\/code>\u5e93\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u5bf9\u65f6\u533a\u7684\u5168\u9762\u652f\u6301\u5e76\u4e14\u6613\u4e8e\u4f7f\u7528\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e24\u79cd\u65b9\u6cd5\u83b7\u53d6UTC\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u548c<code>pytz<\/code>\u5e93\u83b7\u53d6UTC\u65f6\u95f4\u662f\u6700\u4e3a\u76f4\u63a5\u548c\u6709\u6548\u7684\u65b9\u6cd5\u3002<code>datetime<\/code>\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u6a21\u5757\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u57fa\u672c\u529f\u80fd\u3002\u800c<code>pytz<\/code>\u5e93\u5219\u63d0\u4f9b\u4e86\u4e16\u754c\u4e0a\u6240\u6709\u65f6\u533a\u7684\u4fe1\u606f\uff0c\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u8f6c\u6362\u65f6\u95f4\u5230\u4efb\u4f55\u65f6\u533a\u3002\u8981\u83b7\u53d6\u5f53\u524d\u7684UTC\u65f6\u95f4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u7684<code>datetime.utcnow()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u7684UTC\u65f6\u95f4\uff0c\u4f46\u6b64\u65b9\u6cd5\u4e0d\u5305\u542b\u65f6\u533a\u4fe1\u606f\u3002\u4e3a\u4e86\u786e\u4fdd\u65f6\u95f4\u662fUTC\u65f6\u95f4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>pytz<\/code>\u5e93\u5c06\u5176\u8f6c\u6362\u4e3aUTC\u65f6\u533a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>import pytz<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524dUTC\u65f6\u95f4<\/strong><\/h2>\n<p>utc_now = datetime.now(pytz.utc)<\/p>\n<p>print(&quot;Current UTC time:&quot;, utc_now)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c<code>datetime.now(pytz.utc)<\/code>\u8c03\u7528\u4f1a\u8fd4\u56de\u5f53\u524d\u7684UTC\u65f6\u95f4\uff0c<code>pytz.utc<\/code>\u786e\u4fdd\u4e86\u8fd4\u56de\u7684\u65f6\u95f4\u5bf9\u8c61\u6709\u6b63\u786e\u7684\u65f6\u533a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001<strong>\u4f7f\u7528DATETIME\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p><code>datetime<\/code>\u6a21\u5757\u662fPython\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u3002\u5b83\u63d0\u4f9b\u4e86\u591a\u4e2a\u7c7b\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8fdb\u884c\u65f6\u95f4\u7684\u83b7\u53d6\u3001\u8ba1\u7b97\u548c\u683c\u5f0f\u5316\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u83b7\u53d6\u5f53\u524dUTC\u65f6\u95f4<\/strong><\/li>\n<\/ul>\n<p><p>\u8981\u83b7\u53d6\u5f53\u524d\u7684UTC\u65f6\u95f4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u4e2d\u7684<code>datetime.utcnow()<\/code>\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u5f53\u524dUTC\u65f6\u95f4\u7684<code>datetime<\/code>\u5bf9\u8c61\uff0c\u4f46\u4e0d\u5305\u542b\u65f6\u533a\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524dUTC\u65f6\u95f4<\/strong><\/h2>\n<p>utc_now = datetime.utcnow()<\/p>\n<p>print(&quot;Current UTC time (without timezone):&quot;, utc_now)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136<code>datetime.utcnow()<\/code>\u80fd\u8fd4\u56de\u5f53\u524d\u7684UTC\u65f6\u95f4\uff0c\u4f46\u8fd9\u4e2a\u65f6\u95f4\u6ca1\u6709\u65f6\u533a\u4fe1\u606f\u3002\u5728\u8bb8\u591a\u5e94\u7528\u573a\u5408\uff0c\u6211\u4eec\u9700\u8981\u660e\u786e\u7684\u65f6\u533a\u4fe1\u606f\uff0c\u8fd9\u65f6\u53ef\u4ee5\u501f\u52a9<code>pytz<\/code>\u5e93\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528DATETIME\u4e0ePYTZ\u7ed3\u5408<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>import pytz<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524dUTC\u65f6\u95f4<\/strong><\/h2>\n<p>utc_now = datetime.now(pytz.utc)<\/p>\n<p>print(&quot;Current UTC time:&quot;, utc_now)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u83b7\u53d6\u4e86\u5f53\u524d\u65f6\u95f4\uff0c\u8fd8\u9644\u5e26\u4e86\u6b63\u786e\u7684\u65f6\u533a\u4fe1\u606f\uff0c\u4f7f\u5f97\u65f6\u95f4\u8f6c\u6362\u548c\u6bd4\u8f83\u66f4\u52a0\u51c6\u786e\u548c\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001<strong>\u4f7f\u7528TIME\u6a21\u5757<\/strong><\/p>\n<\/p>\n<p><p><code>time<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e0eC\u8bed\u8a00\u6807\u51c6\u5e93\u7c7b\u4f3c\u7684\u65f6\u95f4\u5904\u7406\u529f\u80fd\uff0c\u5b83\u7684\u6838\u5fc3\u662f\u5904\u7406\u65f6\u95f4\u6233\uff08\u81ea1970\u5e741\u67081\u65e5\u4ee5\u6765\u7684\u79d2\u6570\uff09\u3002<code>time<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\u7528\u4e8e\u83b7\u53d6\u548c\u683c\u5f0f\u5316\u65f6\u95f4\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u83b7\u53d6\u5f53\u524dUTC\u65f6\u95f4<\/strong><\/li>\n<\/ul>\n<p><p>\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u83b7\u53d6UTC\u65f6\u95f4\uff0c\u53ef\u4ee5\u901a\u8fc7<code>time.gmtime()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\uff0c\u8be5\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a<code>time.struct_time<\/code>\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524dUTC\u65f6\u95f4<\/strong><\/h2>\n<p>utc_now = time.gmtime()<\/p>\n<p>print(&quot;Current UTC time (struct_time):&quot;, utc_now)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>time.gmtime()<\/code>\u8fd4\u56de\u7684<code>struct_time<\/code>\u5bf9\u8c61\u5305\u542b\u5e74\u3001\u6708\u3001\u65e5\u3001\u5c0f\u65f6\u3001\u5206\u949f\u3001\u79d2\u7b49\u4fe1\u606f\u3002\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u66f4\u6613\u8bfb\u7684\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>time.strftime()<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u683c\u5f0f\u5316UTC\u65f6\u95f4<\/p>\n<p>formatted_utc_now = time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, utc_now)<\/p>\n<p>print(&quot;Formatted UTC time:&quot;, formatted_utc_now)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001<strong>\u4f7f\u7528ARROW\u5e93<\/strong><\/p>\n<\/p>\n<p><p><code>arrow<\/code>\u662fPython\u7684\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u65e8\u5728\u7b80\u5316\u65e5\u671f\u548c\u65f6\u95f4\u7684\u5904\u7406\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u7528\u7684API\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8fdb\u884c\u65f6\u95f4\u7684\u83b7\u53d6\u3001\u8f6c\u6362\u548c\u683c\u5f0f\u5316\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u83b7\u53d6\u5f53\u524dUTC\u65f6\u95f4<\/strong><\/li>\n<\/ul>\n<p><p>\u4f7f\u7528<code>arrow<\/code>\u5e93\u83b7\u53d6\u5f53\u524dUTC\u65f6\u95f4\u76f8\u5bf9\u7b80\u5355\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import arrow<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524dUTC\u65f6\u95f4<\/strong><\/h2>\n<p>utc_now = arrow.utcnow()<\/p>\n<p>print(&quot;Current UTC time using arrow:&quot;, utc_now)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>arrow.utcnow()<\/code>\u8fd4\u56de\u4e00\u4e2a<code>Arrow<\/code>\u5bf9\u8c61\uff0c\u4ee3\u8868\u5f53\u524d\u7684UTC\u65f6\u95f4\u3002\u4e0e<code>datetime<\/code>\u5bf9\u8c61\u4e0d\u540c\uff0c<code>Arrow<\/code>\u5bf9\u8c61\u81ea\u5e26\u65f6\u533a\u4fe1\u606f\uff0c\u5e76\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u64cd\u4f5c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u65f6\u95f4\u683c\u5f0f\u5316\u4e0e\u8f6c\u6362<\/strong><\/li>\n<\/ul>\n<p><p><code>Arrow<\/code>\u5bf9\u8c61\u53ef\u4ee5\u8f7b\u677e\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\u6216\u8f6c\u6362\u4e3a\u5176\u4ed6\u65f6\u533a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32<\/p>\n<p>formatted_utc_now = utc_now.format(&#39;YYYY-MM-DD HH:mm:ss&#39;)<\/p>\n<p>print(&quot;Formatted UTC time using arrow:&quot;, formatted_utc_now)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u5176\u4ed6\u65f6\u533a<\/strong><\/h2>\n<p>local_time = utc_now.to(&#39;US\/Pacific&#39;)<\/p>\n<p>print(&quot;Local time in US\/Pacific:&quot;, local_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001<strong>\u5e38\u89c1\u5e94\u7528\u573a\u666f<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u8bb0\u5f55\u65e5\u5fd7<\/strong><\/li>\n<\/ul>\n<p><p>\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u7edf\u4e00\u7684\u65f6\u95f4\u6233\u6709\u52a9\u4e8e\u5206\u6790\u65e5\u5fd7\u548c\u6392\u67e5\u95ee\u9898\u3002UTC\u65f6\u95f4\u4e0d\u53d7\u65f6\u533a\u53d8\u5316\u7684\u5f71\u54cd\uff0c\u56e0\u6b64\u662f\u8bb0\u5f55\u65e5\u5fd7\u7684\u7406\u60f3\u9009\u62e9\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u65f6\u95f4\u540c\u6b65<\/strong><\/li>\n<\/ul>\n<p><p>\u5728\u9700\u8981\u65f6\u95f4\u540c\u6b65\u7684\u573a\u666f\u4e2d\uff0c\u5982\u591a\u53f0\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u534f\u4f5c\uff0cUTC\u65f6\u95f4\u662f\u786e\u4fdd\u4e00\u81f4\u6027\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u8de8\u65f6\u533a\u5e94\u7528<\/strong><\/li>\n<\/ul>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u591a\u65f6\u533a\u7528\u6237\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f7f\u7528UTC\u65f6\u95f4\u4f5c\u4e3a\u4e2d\u95f4\u65f6\u95f4\u683c\u5f0f\u53ef\u4ee5\u7b80\u5316\u65f6\u95f4\u8f6c\u6362\u548c\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001<strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u65f6\u533a\u8f6c\u6362<\/strong><\/li>\n<\/ul>\n<p><p>\u83b7\u53d6UTC\u65f6\u95f4\u540e\uff0c\u82e5\u9700\u8981\u8f6c\u6362\u4e3a\u5176\u4ed6\u65f6\u533a\uff0c<code>pytz<\/code>\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u652f\u6301\u3002\u53ef\u4ee5\u4f7f\u7528<code>pytz.timezone()<\/code>\u51fd\u6570\u6307\u5b9a\u76ee\u6807\u65f6\u533a\uff0c\u7136\u540e\u4f7f\u7528<code>astimezone()<\/code>\u65b9\u6cd5\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f6c\u6362\u4e3a\u6307\u5b9a\u65f6\u533a\u65f6\u95f4<\/p>\n<p>local_tz = pytz.timezone(&#39;Asia\/Shangh<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>&#39;)<\/p>\n<p>local_time = utc_now.astimezone(local_tz)<\/p>\n<p>print(&quot;Local time in Asia\/Shanghai:&quot;, local_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u590f\u4ee4\u65f6\u5904\u7406<\/strong><\/li>\n<\/ul>\n<p><p>\u5728\u6d89\u53ca\u5230\u590f\u4ee4\u65f6\u7684\u5730\u533a\uff0c<code>pytz<\/code>\u5e93\u80fd\u591f\u81ea\u52a8\u5904\u7406\u590f\u4ee4\u65f6\u7684\u53d8\u5316\uff0c\u786e\u4fdd\u65f6\u95f4\u8f6c\u6362\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u7cbe\u5ea6\u95ee\u9898<\/strong><\/li>\n<\/ul>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u9ad8\u7cbe\u5ea6\u65f6\u95f4\u7684\u5e94\u7528\u573a\u5408\uff0c<code>datetime<\/code>\u6a21\u5757\u63d0\u4f9b\u7684\u7cbe\u5ea6\u901a\u5e38\u8db3\u591f\uff0c\u4f46\u5bf9\u4e8e\u6781\u7aef\u7cbe\u5ea6\u8981\u6c42\uff0c\u53ef\u80fd\u9700\u8981\u501f\u52a9\u66f4\u4e13\u4e1a\u7684\u65f6\u95f4\u5e93\u6216API\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001<strong>\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u83b7\u53d6UTC\u65f6\u95f4\uff0c\u5176\u4e2d\u4ee5<code>datetime<\/code>\u6a21\u5757\u7ed3\u5408<code>pytz<\/code>\u5e93\u7684\u65b9\u6cd5\u6700\u4e3a\u5e38\u7528\u548c\u4fbf\u6377\u3002<code>time<\/code>\u6a21\u5757\u548c<code>arrow<\/code>\u5e93\u4e5f\u662f\u6709\u6548\u7684\u66ff\u4ee3\u65b9\u6848\uff0c\u5177\u4f53\u9009\u62e9\u53d6\u51b3\u4e8e\u5e94\u7528\u573a\u666f\u548c\u4e2a\u4eba\u504f\u597d\u3002\u5728\u5904\u7406\u8de8\u65f6\u533a\u5e94\u7528\u548c\u5206\u5e03\u5f0f\u7cfb\u7edf\u65f6\uff0c\u7406\u89e3\u548c\u6b63\u786e\u5e94\u7528UTC\u65f6\u95f4\u662f\u786e\u4fdd\u7cfb\u7edf\u4e00\u81f4\u6027\u548c\u53ef\u9760\u6027\u7684\u5173\u952e\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u786e\u4fdd\u65f6\u95f4\u7684\u51c6\u786e\u6027\u548c\u65f6\u533a\u7684\u4e00\u81f4\u6027\u90fd\u662f\u4f7f\u7528\u65f6\u95f4\u529f\u80fd\u65f6\u9700\u8981\u7279\u522b\u6ce8\u610f\u7684\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u5f53\u524d\u7684UTC\u65f6\u95f4\uff1f<\/strong><br \/>\u5728Python\u4e2d\u83b7\u53d6\u5f53\u524d\u7684UTC\u65f6\u95f4\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u4e2d\u7684<code>datetime.utcnow()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u5f53\u524d\u7684UTC\u65f6\u95f4\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from datetime import datetime\n\nutc_time = datetime.utcnow()\nprint(&quot;\u5f53\u524dUTC\u65f6\u95f4:&quot;, utc_time)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u4e00\u4e2a<code>datetime<\/code>\u5bf9\u8c61\uff0c\u8868\u793a\u5f53\u524d\u7684UTC\u65f6\u95f4\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u672c\u5730\u65f6\u95f4\u8f6c\u6362\u4e3aUTC\u65f6\u95f4\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u9700\u8981\u5c06\u672c\u5730\u65f6\u95f4\u8f6c\u6362\u4e3aUTC\u65f6\u95f4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pytz<\/code>\u5e93\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5\u8be5\u5e93\uff0c\u7136\u540e\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n<pre><code class=\"language-python\">from datetime import datetime\nimport pytz\n\nlocal_timezone = pytz.timezone(&quot;Asia\/Shanghai&quot;)  # \u66ff\u6362\u4e3a\u4f60\u7684\u672c\u5730\u65f6\u533a\nlocal_time = local_timezone.localize(datetime.now())\nutc_time = local_time.astimezone(pytz.utc)\nprint(&quot;\u8f6c\u6362\u540e\u7684UTC\u65f6\u95f4:&quot;, utc_time)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u786e\u4fdd\u4f60\u7684\u672c\u5730\u65f6\u95f4\u6b63\u786e\u8f6c\u6362\u4e3aUTC\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6UTC\u65f6\u95f4\u6233\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u9700\u8981\u83b7\u53d6UTC\u65f6\u95f4\u6233\uff0c\u53ef\u4ee5\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u7684<code>time()<\/code>\u51fd\u6570\u3002\u5b83\u8fd4\u56de\u7684\u662f\u81ea1970\u5e741\u67081\u65e5\u4ee5\u6765\u7684\u79d2\u6570\uff0c\u8868\u793aUTC\u65f6\u95f4\u6233\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import time\n\nutc_timestamp = time.time()\nprint(&quot;\u5f53\u524dUTC\u65f6\u95f4\u6233:&quot;, utc_timestamp)\n<\/code><\/pre>\n<p>\u6b64\u65f6\u95f4\u6233\u53ef\u4ee5\u7528\u4e8e\u65f6\u95f4\u8ba1\u7b97\u6216\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e00\u3001\u4f7f\u7528Python\u83b7\u53d6UTC\u65f6\u95f4\u7684\u65b9\u6cd5\uff1adatetime\u6a21\u5757\u3001pytz\u5e93\u3001time\u6a21\u5757\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c [&hellip;]","protected":false},"author":3,"featured_media":982829,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/982822"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=982822"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/982822\/revisions"}],"predecessor-version":[{"id":982833,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/982822\/revisions\/982833"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/982829"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=982822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=982822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=982822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}