{"id":1173077,"date":"2025-01-15T16:58:30","date_gmt":"2025-01-15T08:58:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1173077.html"},"modified":"2025-01-15T16:58:32","modified_gmt":"2025-01-15T08:58:32","slug":"python%e5%a6%82%e4%bd%95%e8%bd%ac%e6%8d%a2unix%e6%97%b6%e9%97%b4%e6%88%b3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1173077.html","title":{"rendered":"python\u5982\u4f55\u8f6c\u6362unix\u65f6\u95f4\u6233"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26074953\/6f77769c-395a-4ece-8105-caf8f1883e3d.webp\" alt=\"python\u5982\u4f55\u8f6c\u6362unix\u65f6\u95f4\u6233\" \/><\/p>\n<p><p> <strong>Python\u8f6c\u6362Unix\u65f6\u95f4\u6233\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u3001<code>time<\/code>\u6a21\u5757\u3001\u4ee5\u53ca<code>pandas<\/code>\u5e93\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u662f\u6700\u5e38\u7528\u7684\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u63d0\u4f9b\u66f4\u591a\u7684\u65e5\u671f\u548c\u65f6\u95f4\u64cd\u4f5c\u529f\u80fd\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u6765\u8f6c\u6362Unix\u65f6\u95f4\u6233\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u7684\u8be6\u7ec6\u65b9\u6cd5\uff1a<\/strong><\/p>\n<\/p>\n<p><p><code>datetime<\/code>\u6a21\u5757\u4e2d\u7684<code>datetime<\/code>\u7c7b\u63d0\u4f9b\u4e86\u4e00\u4e2a\u65b9\u6cd5<code>fromtimestamp()<\/code>\uff0c\u5b83\u53ef\u4ee5\u5c06Unix\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a<code>datetime<\/code>\u5bf9\u8c61\u3002\u8fd9\u4e2a\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\u4e14\u76f4\u89c2\uff0c\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>Unix\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>timestamp = 1633072800<\/p>\n<h2><strong>\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61<\/strong><\/h2>\n<p>dt_object = datetime.fromtimestamp(timestamp)<\/p>\n<p>print(&quot;Datetime\u5bf9\u8c61:&quot;, dt_object)<\/p>\n<p>print(&quot;\u683c\u5f0f\u5316\u65e5\u671f:&quot;, dt_object.strftime(&#39;%Y-%m-%d %H:%M:%S&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86<code>datetime<\/code>\u6a21\u5757\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2aUnix\u65f6\u95f4\u6233\u3002\u63a5\u7740\u4f7f\u7528<code>fromtimestamp()<\/code>\u65b9\u6cd5\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a<code>datetime<\/code>\u5bf9\u8c61\uff0c\u6700\u540e\u901a\u8fc7<code>strftime()<\/code>\u65b9\u6cd5\u5c06<code>datetime<\/code>\u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a\u53ef\u8bfb\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5176\u4ed6\u65b9\u6cd5\u548c\u76f8\u5173\u5185\u5bb9\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528<code>time<\/code>\u6a21\u5757<\/strong><\/h2>\n<p><p><code>time<\/code>\u6a21\u5757\u662fPython\u4e2d\u5904\u7406\u65f6\u95f4\u76f8\u5173\u64cd\u4f5c\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\u3002\u867d\u7136\u5b83\u7684\u529f\u80fd\u76f8\u5bf9\u8f83\u5c11\uff0c\u4f46\u5bf9\u4e8e\u7b80\u5355\u7684\u65f6\u95f4\u6233\u8f6c\u6362\uff0c<code>time<\/code>\u6a21\u5757\u4f9d\u7136\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h2><code>time<\/code>\u6a21\u5757\u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<\/p>\n<p><p><code>time<\/code>\u6a21\u5757\u4e2d\u7684<code>localtime()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06Unix\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u65f6\u95f4\u5143\u7ec4\uff08<code>time.struct_time<\/code>\uff09\uff0c\u7136\u540e\u4f7f\u7528<code>strftime()<\/code>\u65b9\u6cd5\u8fdb\u884c\u683c\u5f0f\u5316\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>Unix\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>timestamp = 1633072800<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u65f6\u95f4\u5143\u7ec4<\/strong><\/h2>\n<p>time_tuple = time.localtime(timestamp)<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u65f6\u95f4\u5143\u7ec4<\/strong><\/h2>\n<p>formatted_time = time.strftime(&#39;%Y-%m-%d %H:%M:%S&#39;, time_tuple)<\/p>\n<p>print(&quot;\u65f6\u95f4\u5143\u7ec4:&quot;, time_tuple)<\/p>\n<p>print(&quot;\u683c\u5f0f\u5316\u65e5\u671f:&quot;, formatted_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86<code>time<\/code>\u6a21\u5757\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2aUnix\u65f6\u95f4\u6233\u3002\u63a5\u7740\u4f7f\u7528<code>localtime()<\/code>\u65b9\u6cd5\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u65f6\u95f4\u5143\u7ec4\uff0c\u6700\u540e\u901a\u8fc7<code>strftime()<\/code>\u65b9\u6cd5\u5c06\u65f6\u95f4\u5143\u7ec4\u683c\u5f0f\u5316\u4e3a\u53ef\u8bfb\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h2>\u5176\u4ed6<code>time<\/code>\u6a21\u5757\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u9664\u4e86<code>localtime()<\/code>\uff0c<code>time<\/code>\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u65b9\u6cd5\uff0c\u5982<code>gmtime()<\/code>\u548c<code>mktime()<\/code>\uff0c\u5b83\u4eec\u53ef\u4ee5\u7528\u4e8e\u4e0d\u540c\u7684\u65f6\u95f4\u6233\u8f6c\u6362\u9700\u6c42\u3002\u4f8b\u5982\uff0c<code>gmtime()<\/code>\u53ef\u4ee5\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3aUTC\u65f6\u95f4\uff0c\u800c<code>mktime()<\/code>\u5219\u53ef\u4ee5\u5c06\u65f6\u95f4\u5143\u7ec4\u8f6c\u6362\u56de\u65f6\u95f4\u6233\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528gmtime()\u65b9\u6cd5<\/p>\n<p>utc_time_tuple = time.gmtime(timestamp)<\/p>\n<p>print(&quot;UTC\u65f6\u95f4\u5143\u7ec4:&quot;, utc_time_tuple)<\/p>\n<h2><strong>\u4f7f\u7528mktime()\u65b9\u6cd5<\/strong><\/h2>\n<p>new_timestamp = time.mktime(time_tuple)<\/p>\n<p>print(&quot;\u8f6c\u6362\u56de\u65f6\u95f4\u6233:&quot;, new_timestamp)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757<\/strong><\/h2>\n<p><p>\u5982\u524d\u6240\u8ff0\uff0c<code>datetime<\/code>\u6a21\u5757\u662f\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u64cd\u4f5c\u7684\u5f3a\u5927\u5de5\u5177\u3002\u9664\u4e86<code>fromtimestamp()<\/code>\u65b9\u6cd5\u5916\uff0c<code>datetime<\/code>\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u6709\u7528\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h2><code>datetime<\/code>\u6a21\u5757\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7<code>datetime<\/code>\u5bf9\u8c61\uff0c\u6211\u4eec\u53ef\u4ee5\u8fdb\u884c\u66f4\u591a\u7684\u65e5\u671f\u548c\u65f6\u95f4\u64cd\u4f5c\uff0c\u4f8b\u5982\u52a0\u51cf\u65f6\u95f4\u3001\u6bd4\u8f83\u65f6\u95f4\u3001\u683c\u5f0f\u5316\u8f93\u51fa\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime, timedelta<\/p>\n<h2><strong>\u5f53\u524d\u65e5\u671f\u65f6\u95f4<\/strong><\/h2>\n<p>now = datetime.now()<\/p>\n<p>print(&quot;\u5f53\u524d\u65e5\u671f\u65f6\u95f4:&quot;, now)<\/p>\n<h2><strong>\u52a0\u51cf\u65f6\u95f4<\/strong><\/h2>\n<p>one_week_later = now + timedelta(weeks=1)<\/p>\n<p>one_week_earlier = now - timedelta(weeks=1)<\/p>\n<p>print(&quot;\u4e00\u5468\u540e:&quot;, one_week_later)<\/p>\n<p>print(&quot;\u4e00\u5468\u524d:&quot;, one_week_earlier)<\/p>\n<h2><strong>\u6bd4\u8f83\u65f6\u95f4<\/strong><\/h2>\n<p>if now &gt; one_week_earlier:<\/p>\n<p>    print(&quot;\u5f53\u524d\u65f6\u95f4\u5728\u4e00\u5468\u4e4b\u540e&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5f53\u524d\u65f6\u95f4\u5728\u4e00\u5468\u4e4b\u524d&quot;)<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u8f93\u51fa<\/strong><\/h2>\n<p>formatted_now = now.strftime(&#39;%Y-%m-%d %H:%M:%S&#39;)<\/p>\n<p>print(&quot;\u683c\u5f0f\u5316\u5f53\u524d\u65f6\u95f4:&quot;, formatted_now)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2><code>datetime<\/code>\u6a21\u5757\u7684\u5176\u4ed6\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u9664\u4e86<code>fromtimestamp()<\/code>\uff0c<code>datetime<\/code>\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86<code>timestamp()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06<code>datetime<\/code>\u5bf9\u8c61\u8f6c\u6362\u4e3aUnix\u65f6\u95f4\u6233\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06datetime\u5bf9\u8c61\u8f6c\u6362\u4e3aUnix\u65f6\u95f4\u6233<\/p>\n<p>timestamp_from_dt = dt_object.timestamp()<\/p>\n<p>print(&quot;\u8f6c\u6362\u56deUnix\u65f6\u95f4\u6233:&quot;, timestamp_from_dt)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528<code>pandas<\/code>\u5e93<\/strong><\/h2>\n<p><p><code>pandas<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65f6\u95f4\u5e8f\u5217\u529f\u80fd\u3002\u5bf9\u4e8e\u5904\u7406\u5927\u91cf\u65f6\u95f4\u6233\u6570\u636e\uff0c<code>pandas<\/code>\u5e93\u662f\u975e\u5e38\u9ad8\u6548\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h2><code>pandas<\/code>\u5e93\u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528<code>pandas<\/code>\u5e93\u4e2d\u7684<code>to_datetime()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06Unix\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a<code>pandas.Timestamp<\/code>\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>Unix\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>timestamp = 1633072800<\/p>\n<h2><strong>\u8f6c\u6362\u4e3apandas.Timestamp\u5bf9\u8c61<\/strong><\/h2>\n<p>timestamp_pd = pd.to_datetime(timestamp, unit=&#39;s&#39;)<\/p>\n<p>print(&quot;pandas.Timestamp\u5bf9\u8c61:&quot;, timestamp_pd)<\/p>\n<p>print(&quot;\u683c\u5f0f\u5316\u65e5\u671f:&quot;, timestamp_pd.strftime(&#39;%Y-%m-%d %H:%M:%S&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86<code>pandas<\/code>\u5e93\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2aUnix\u65f6\u95f4\u6233\u3002\u63a5\u7740\u4f7f\u7528<code>to_datetime()<\/code>\u65b9\u6cd5\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a<code>pandas.Timestamp<\/code>\u5bf9\u8c61\uff0c\u6700\u540e\u901a\u8fc7<code>strftime()<\/code>\u65b9\u6cd5\u5c06<code>pandas.Timestamp<\/code>\u5bf9\u8c61\u683c\u5f0f\u5316\u4e3a\u53ef\u8bfb\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h2>\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e<\/h2>\n<\/p>\n<p><p><code>pandas<\/code>\u5e93\u7684\u4f18\u52bf\u5728\u4e8e\u5904\u7406\u5927\u91cf\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00\u7cfb\u5217Unix\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a<code>pandas.DatetimeIndex<\/code>\u5bf9\u8c61\uff0c\u5e76\u8fdb\u884c\u5404\u79cd\u65f6\u95f4\u5e8f\u5217\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e00\u7cfb\u5217Unix\u65f6\u95f4\u6233<\/p>\n<p>timestamps = [1633072800, 1633159200, 1633245600]<\/p>\n<h2><strong>\u8f6c\u6362\u4e3apandas.DatetimeIndex\u5bf9\u8c61<\/strong><\/h2>\n<p>datetime_index = pd.to_datetime(timestamps, unit=&#39;s&#39;)<\/p>\n<p>print(&quot;pandas.DatetimeIndex\u5bf9\u8c61:&quot;, datetime_index)<\/p>\n<h2><strong>\u65f6\u95f4\u5e8f\u5217\u64cd\u4f5c<\/strong><\/h2>\n<p>time_series = pd.Series([100, 200, 300], index=datetime_index)<\/p>\n<p>print(&quot;\u65f6\u95f4\u5e8f\u5217\u6570\u636e:&quot;)<\/p>\n<p>print(time_series)<\/p>\n<h2><strong>\u65f6\u95f4\u5e8f\u5217\u5207\u7247<\/strong><\/h2>\n<p>print(&quot;\u65f6\u95f4\u5e8f\u5217\u5207\u7247:&quot;)<\/p>\n<p>print(time_series[&#39;2021-10-01&#39;:&#39;2021-10-02&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u5176\u4ed6\u65b9\u6cd5\u548c\u5de5\u5177<\/strong><\/h2>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u5e38\u7528\u65b9\u6cd5\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u5de5\u5177\u548c\u5e93\u53ef\u4ee5\u7528\u4e8eUnix\u65f6\u95f4\u6233\u8f6c\u6362\u3002\u4f8b\u5982\uff0c<code>arrow<\/code>\u5e93\u548c<code>numpy<\/code>\u5e93\u4e5f\u63d0\u4f9b\u4e86\u65f6\u95f4\u6233\u8f6c\u6362\u548c\u65f6\u95f4\u64cd\u4f5c\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h2>\u4f7f\u7528<code>arrow<\/code>\u5e93<\/h2>\n<\/p>\n<p><p><code>arrow<\/code>\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\u5e93\uff0c\u63d0\u4f9b\u4e86\u7b80\u6d01\u7684API\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>arrow<\/code>\u5e93\u8fdb\u884cUnix\u65f6\u95f4\u6233\u8f6c\u6362\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import arrow<\/p>\n<h2><strong>Unix\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>timestamp = 1633072800<\/p>\n<h2><strong>\u8f6c\u6362\u4e3aarrow\u5bf9\u8c61<\/strong><\/h2>\n<p>arrow_object = arrow.get(timestamp)<\/p>\n<p>print(&quot;arrow\u5bf9\u8c61:&quot;, arrow_object)<\/p>\n<p>print(&quot;\u683c\u5f0f\u5316\u65e5\u671f:&quot;, arrow_object.format(&#39;YYYY-MM-DD HH:mm:ss&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4f7f\u7528<code>numpy<\/code>\u5e93<\/h2>\n<\/p>\n<p><p><code>numpy<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u4e5f\u63d0\u4f9b\u4e86\u65f6\u95f4\u6233\u8f6c\u6362\u7684\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>numpy<\/code>\u5e93\u8fdb\u884cUnix\u65f6\u95f4\u6233\u8f6c\u6362\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>Unix\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>timestamp = 1633072800<\/p>\n<h2><strong>\u8f6c\u6362\u4e3anumpy.datetime64\u5bf9\u8c61<\/strong><\/h2>\n<p>datetime_np = np.datetime64(timestamp, &#39;s&#39;)<\/p>\n<p>print(&quot;numpy.datetime64\u5bf9\u8c61:&quot;, datetime_np)<\/p>\n<p>print(&quot;\u683c\u5f0f\u5316\u65e5\u671f:&quot;, datetime_np.astype(&#39;datetime64[s]&#39;).astype(str))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ecb\u7ecd\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c<strong>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8f6c\u6362Unix\u65f6\u95f4\u6233\uff0c\u5305\u62ec\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u3001<code>time<\/code>\u6a21\u5757\u3001<code>pandas<\/code>\u5e93\u3001<code>arrow<\/code>\u5e93\u548c<code>numpy<\/code>\u5e93\u3002<\/strong> \u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<ul>\n<li><strong><code>datetime<\/code>\u6a21\u5757<\/strong>\uff1a\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u65e5\u671f\u548c\u65f6\u95f4\u64cd\u4f5c\u3002<\/li>\n<li><strong><code>time<\/code>\u6a21\u5757<\/strong>\uff1a\u7b80\u5355\u6613\u7528\uff0c\u9002\u7528\u4e8e\u57fa\u672c\u7684\u65f6\u95f4\u6233\u8f6c\u6362\u3002<\/li>\n<li><strong><code>pandas<\/code>\u5e93<\/strong>\uff1a\u9ad8\u6548\u5904\u7406\u5927\u91cf\u65f6\u95f4\u5e8f\u5217\u6570\u636e\uff0c\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002<\/li>\n<li><strong><code>arrow<\/code>\u5e93<\/strong>\uff1a\u8f7b\u91cf\u7ea7\uff0c\u63d0\u4f9b\u7b80\u6d01\u7684API\u3002<\/li>\n<li><strong><code>numpy<\/code>\u5e93<\/strong>\uff1a\u9002\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\u548c\u79d1\u5b66\u8ba1\u7b97\u3002<\/li>\n<\/ul>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u548c\u5de5\u5177\u8fdb\u884cUnix\u65f6\u95f4\u6233\u8f6c\u6362\u548c\u65f6\u95f4\u64cd\u4f5c\u3002\u65e0\u8bba\u662f\u5904\u7406\u5355\u4e2a\u65f6\u95f4\u6233\u8fd8\u662f\u5904\u7406\u5927\u91cf\u65f6\u95f4\u5e8f\u5217\u6570\u636e\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5e93\u548c\u51fd\u6570\uff0c\u5e2e\u52a9\u6211\u4eec\u9ad8\u6548\u5730\u5b8c\u6210\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06Unix\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u53ef\u8bfb\u7684\u65e5\u671f\u683c\u5f0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u6765\u5c06Unix\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u53ef\u8bfb\u7684\u65e5\u671f\u683c\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528<code>datetime.fromtimestamp()<\/code>\u65b9\u6cd5\uff0c\u5c06Unix\u65f6\u95f4\u6233\u4f20\u5165\u8be5\u65b9\u6cd5\uff0c\u4ece\u800c\u5f97\u5230\u672c\u5730\u65f6\u95f4\u7684\u65e5\u671f\u548c\u65f6\u95f4\u8868\u793a\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import datetime\n\ntimestamp = 1633072800\ndt_object = datetime.datetime.fromtimestamp(timestamp)\nprint(dt_object)\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u5c06\u8f93\u51fa\u4e00\u4e2a\u53ef\u8bfb\u7684\u65e5\u671f\u65f6\u95f4\u683c\u5f0f\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u5c06\u53ef\u8bfb\u7684\u65e5\u671f\u8f6c\u6362\u4e3aUnix\u65f6\u95f4\u6233\uff1f<\/strong><br \/>\u8981\u5c06\u53ef\u8bfb\u7684\u65e5\u671f\u8f6c\u6362\u4e3aUnix\u65f6\u95f4\u6233\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u4e2d\u7684<code>timestamp()<\/code>\u65b9\u6cd5\u3002\u9996\u5148\uff0c\u9700\u8981\u5c06\u53ef\u8bfb\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a<code>datetime<\/code>\u5bf9\u8c61\uff0c\u7136\u540e\u8c03\u7528\u8be5\u5bf9\u8c61\u7684<code>timestamp()<\/code>\u65b9\u6cd5\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import datetime\n\ndate_string = &quot;2021-10-01 00:00:00&quot;\ndt_object = datetime.datetime.strptime(date_string, &#39;%Y-%m-%d %H:%M:%S&#39;)\nunix_timestamp = dt_object.timestamp()\nprint(unix_timestamp)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa\u5bf9\u5e94\u7684Unix\u65f6\u95f4\u6233\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u65f6\u533a\u95ee\u9898\u4ee5\u786e\u4fddUnix\u65f6\u95f4\u6233\u8f6c\u6362\u7684\u51c6\u786e\u6027\uff1f<\/strong><br \/>\u5728\u5904\u7406Unix\u65f6\u95f4\u6233\u65f6\uff0c\u65f6\u533a\u95ee\u9898\u53ef\u80fd\u4f1a\u5bfc\u81f4\u65f6\u95f4\u8f6c\u6362\u4e0d\u51c6\u786e\u3002\u53ef\u4ee5\u4f7f\u7528<code>pytz<\/code>\u5e93\u6765\u5904\u7406\u65f6\u533a\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86<code>pytz<\/code>\u5e93\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u5c06<code>datetime<\/code>\u5bf9\u8c61\u4e0e\u7279\u5b9a\u65f6\u533a\u5173\u8054\uff0c\u786e\u4fdd\u8f6c\u6362\u540e\u7684\u65f6\u95f4\u51c6\u786e\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import datetime\nimport pytz\n\ntimestamp = 1633072800\nutc_time = datetime.datetime.fromtimestamp(timestamp, pytz.utc)\nlocal_time = utc_time.astimezone(pytz.timezone(&#39;Asia\/Shangh<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>&#39;))\nprint(local_time)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u4f60\u5728\u8f6c\u6362\u65f6\u8003\u8651\u5230\u4e86\u65f6\u533a\u7684\u5f71\u54cd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8f6c\u6362Unix\u65f6\u95f4\u6233\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528datetime\u6a21\u5757\u3001time\u6a21\u5757\u3001\u4ee5\u53capandas\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1173082,"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\/1173077"}],"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=1173077"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1173077\/revisions"}],"predecessor-version":[{"id":1173084,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1173077\/revisions\/1173084"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1173082"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1173077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1173077"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1173077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}