{"id":1178359,"date":"2025-01-15T18:09:56","date_gmt":"2025-01-15T10:09:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1178359.html"},"modified":"2025-01-15T18:09:59","modified_gmt":"2025-01-15T10:09:59","slug":"python-%e5%a6%82%e4%bd%95%e7%bb%9f%e8%ae%a1%e6%97%b6%e9%97%b4%e9%97%b4%e9%9a%94","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1178359.html","title":{"rendered":"python \u5982\u4f55\u7edf\u8ba1\u65f6\u95f4\u95f4\u9694"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25113027\/7591e8ad-07ce-4774-9581-f5799b8414d4.webp\" alt=\"python \u5982\u4f55\u7edf\u8ba1\u65f6\u95f4\u95f4\u9694\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528datetime\u6a21\u5757\u3001time\u6a21\u5757\u3001pandas\u5e93\u6765\u7edf\u8ba1\u65f6\u95f4\u95f4\u9694\u3002datetime\u6a21\u5757\u63d0\u4f9b\u4e86\u65f6\u95f4\u548c\u65e5\u671f\u5904\u7406\u7684\u57fa\u672c\u529f\u80fd\uff0ctime\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u5e95\u5c42\u7684\u65f6\u95f4\u5904\u7406\u529f\u80fd\uff0c\u800cpandas\u5e93\u5219\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u65f6\u95f4\u5e8f\u5217\u5904\u7406\u529f\u80fd\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u8fdb\u884c\u65f6\u95f4\u95f4\u9694\u7edf\u8ba1\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528datetime\u6a21\u5757\u7edf\u8ba1\u65f6\u95f4\u95f4\u9694<\/h3>\n<\/p>\n<p><p>datetime\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u7c7b\u548c\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528datetime\u6a21\u5757\u7edf\u8ba1\u65f6\u95f4\u95f4\u9694\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165datetime\u6a21\u5757<\/strong><\/li>\n<li><strong>\u521b\u5efadatetime\u5bf9\u8c61<\/strong><\/li>\n<li><strong>\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2adatetime\u5bf9\u8c61<\/strong><\/h2>\n<p>start_time = datetime(2023, 10, 1, 12, 0, 0)<\/p>\n<p>end_time = datetime(2023, 10, 1, 14, 30, 0)<\/p>\n<h2><strong>\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694<\/strong><\/h2>\n<p>time_interval = end_time - start_time<\/p>\n<h2><strong>\u8f93\u51fa\u65f6\u95f4\u95f4\u9694<\/strong><\/h2>\n<p>print(&quot;\u65f6\u95f4\u95f4\u9694:&quot;, time_interval)<\/p>\n<p>print(&quot;\u65f6\u95f4\u95f4\u9694\uff08\u79d2\uff09:&quot;, time_interval.total_seconds())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>datetime<\/code>\u7c7b\u521b\u5efa\u4e86\u4e24\u4e2a\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61<code>start_time<\/code>\u548c<code>end_time<\/code>\uff0c\u7136\u540e\u901a\u8fc7\u7b80\u5355\u7684\u51cf\u6cd5\u64cd\u4f5c\u8ba1\u7b97\u4e86\u5b83\u4eec\u4e4b\u95f4\u7684\u65f6\u95f4\u95f4\u9694\u3002\u7ed3\u679c\u662f\u4e00\u4e2a<code>timedelta<\/code>\u5bf9\u8c61\uff0c\u5b83\u8868\u793a\u4e24\u4e2a\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\u4e4b\u95f4\u7684\u5dee\u5f02\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>total_seconds<\/code>\u65b9\u6cd5\u5c06\u65f6\u95f4\u95f4\u9694\u8f6c\u6362\u4e3a\u79d2\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528time\u6a21\u5757\u7edf\u8ba1\u65f6\u95f4\u95f4\u9694<\/h3>\n<\/p>\n<p><p>time\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u7cfb\u7edf\u65f6\u95f4\u7684\u8bbf\u95ee\u4ee5\u53ca\u4e00\u4e9b\u65f6\u95f4\u76f8\u5173\u7684\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528time\u6a21\u5757\u7edf\u8ba1\u65f6\u95f4\u95f4\u9694\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165time\u6a21\u5757<\/strong><\/li>\n<li><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233<\/strong><\/li>\n<li><strong>\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>\u83b7\u53d6\u8d77\u59cb\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<h2><strong>\u6a21\u62df\u4e00\u4e9b\u64cd\u4f5c<\/strong><\/h2>\n<p>time.sleep(5)  # \u6682\u505c5\u79d2<\/p>\n<h2><strong>\u83b7\u53d6\u7ed3\u675f\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>end_time = time.time()<\/p>\n<h2><strong>\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694<\/strong><\/h2>\n<p>time_interval = end_time - start_time<\/p>\n<h2><strong>\u8f93\u51fa\u65f6\u95f4\u95f4\u9694<\/strong><\/h2>\n<p>print(&quot;\u65f6\u95f4\u95f4\u9694\uff08\u79d2\uff09:&quot;, time_interval)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u7684<code>time<\/code>\u51fd\u6570\u83b7\u53d6\u5f53\u524d\u7684\u65f6\u95f4\u6233\uff0c\u7136\u540e\u901a\u8fc7<code>time.sleep<\/code>\u51fd\u6570\u6a21\u62df\u4e00\u4e9b\u64cd\u4f5c\uff0c\u6700\u540e\u518d\u6b21\u83b7\u53d6\u65f6\u95f4\u6233\u5e76\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u7cbe\u5ea6\u8ba1\u65f6\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528pandas\u5e93\u7edf\u8ba1\u65f6\u95f4\u95f4\u9694<\/h3>\n<\/p>\n<p><p>pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65f6\u95f4\u5e8f\u5217\u5904\u7406\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528pandas\u5e93\u7edf\u8ba1\u65f6\u95f4\u95f4\u9694\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165pandas\u5e93<\/strong><\/li>\n<li><strong>\u521b\u5efa\u65f6\u95f4\u5e8f\u5217\u6570\u636e<\/strong><\/li>\n<li><strong>\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u65f6\u95f4\u5e8f\u5217\u6570\u636e<\/strong><\/h2>\n<p>start_time = pd.Timestamp(&#39;2023-10-01 12:00:00&#39;)<\/p>\n<p>end_time = pd.Timestamp(&#39;2023-10-01 14:30:00&#39;)<\/p>\n<h2><strong>\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694<\/strong><\/h2>\n<p>time_interval = end_time - start_time<\/p>\n<h2><strong>\u8f93\u51fa\u65f6\u95f4\u95f4\u9694<\/strong><\/h2>\n<p>print(&quot;\u65f6\u95f4\u95f4\u9694:&quot;, time_interval)<\/p>\n<p>print(&quot;\u65f6\u95f4\u95f4\u9694\uff08\u79d2\uff09:&quot;, time_interval.total_seconds())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>pandas.Timestamp<\/code>\u7c7b\u521b\u5efa\u4e86\u4e24\u4e2a\u65f6\u95f4\u6233\u5bf9\u8c61\uff0c\u7136\u540e\u901a\u8fc7\u51cf\u6cd5\u64cd\u4f5c\u8ba1\u7b97\u4e86\u5b83\u4eec\u4e4b\u95f4\u7684\u65f6\u95f4\u95f4\u9694\u3002\u7ed3\u679c\u540c\u6837\u662f\u4e00\u4e2a<code>timedelta<\/code>\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5e94\u7528\u573a\u666f\u548c\u5b9e\u8df5\u7ecf\u9a8c<\/h3>\n<\/p>\n<p><h4>1\u3001\u6570\u636e\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u65f6\u95f4\u95f4\u9694\u7684\u7edf\u8ba1\u975e\u5e38\u5e38\u89c1\u3002\u4f8b\u5982\uff0c\u5206\u6790\u7528\u6237\u5728\u7f51\u7ad9\u4e0a\u7684\u505c\u7559\u65f6\u95f4\u3001\u8ba2\u5355\u7684\u5904\u7406\u65f6\u95f4\u7b49\u3002\u4f7f\u7528pandas\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\uff0c\u5e76\u8fdb\u884c\u5404\u79cd\u7edf\u8ba1\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;timestamp&#39;: [&#39;2023-10-01 12:00:00&#39;, &#39;2023-10-01 12:05:00&#39;, &#39;2023-10-01 12:10:00&#39;],<\/p>\n<p>        &#39;value&#39;: [10, 15, 20]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5c06timestamp\u5217\u8f6c\u6362\u4e3adatetime\u7c7b\u578b<\/strong><\/h2>\n<p>df[&#39;timestamp&#39;] = pd.to_datetime(df[&#39;timestamp&#39;])<\/p>\n<h2><strong>\u8ba1\u7b97\u76f8\u90bb\u65f6\u95f4\u6233\u4e4b\u95f4\u7684\u65f6\u95f4\u95f4\u9694<\/strong><\/h2>\n<p>df[&#39;time_diff&#39;] = df[&#39;timestamp&#39;].diff().dt.total_seconds()<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u65f6\u95f4\u6233\u548c\u6570\u503c\u7684\u793a\u4f8b\u6570\u636e\u6846\uff0c\u7136\u540e\u4f7f\u7528<code>pd.to_datetime<\/code>\u51fd\u6570\u5c06\u65f6\u95f4\u6233\u5217\u8f6c\u6362\u4e3adatetime\u7c7b\u578b\uff0c\u5e76\u8ba1\u7b97\u76f8\u90bb\u65f6\u95f4\u6233\u4e4b\u95f4\u7684\u65f6\u95f4\u95f4\u9694\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6027\u80fd\u76d1\u63a7<\/h4>\n<\/p>\n<p><p>\u5728\u6027\u80fd\u76d1\u63a7\u4e2d\uff0c\u7edf\u8ba1\u65f6\u95f4\u95f4\u9694\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4e86\u89e3\u7a0b\u5e8f\u6216\u7cfb\u7edf\u7684\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u7edf\u8ba1\u51fd\u6570\u6267\u884c\u65f6\u95f4\u3001\u8bf7\u6c42\u54cd\u5e94\u65f6\u95f4\u7b49\u3002\u4f7f\u7528time\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u9ad8\u7cbe\u5ea6\u8ba1\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def some_function():<\/p>\n<p>    time.sleep(2)  # \u6a21\u62df\u51fd\u6570\u6267\u884c\u65f6\u95f4<\/p>\n<h2><strong>\u8bb0\u5f55\u8d77\u59cb\u65f6\u95f4<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<h2><strong>\u6267\u884c\u51fd\u6570<\/strong><\/h2>\n<p>some_function()<\/p>\n<h2><strong>\u8bb0\u5f55\u7ed3\u675f\u65f6\u95f4<\/strong><\/h2>\n<p>end_time = time.time()<\/p>\n<h2><strong>\u8ba1\u7b97\u51fd\u6570\u6267\u884c\u65f6\u95f4<\/strong><\/h2>\n<p>execution_time = end_time - start_time<\/p>\n<p>print(&quot;\u51fd\u6570\u6267\u884c\u65f6\u95f4\uff08\u79d2\uff09:&quot;, execution_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6a21\u62df\u51fd\u6570<code>some_function<\/code>\uff0c\u5e76\u4f7f\u7528time\u6a21\u5757\u8bb0\u5f55\u51fd\u6570\u6267\u884c\u524d\u540e\u7684\u65f6\u95f4\u6233\uff0c\u8ba1\u7b97\u51fd\u6570\u6267\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6<\/h4>\n<\/p>\n<p><p>\u5728\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\u4e2d\uff0c\u7edf\u8ba1\u65f6\u95f4\u95f4\u9694\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u786e\u5b9a\u4efb\u52a1\u7684\u6267\u884c\u65f6\u95f4\u548c\u95f4\u9694\u3002\u4f8b\u5982\uff0c\u6bcf\u9694\u4e00\u6bb5\u65f6\u95f4\u6267\u884c\u67d0\u4e2a\u4efb\u52a1\u3002\u4f7f\u7528datetime\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u8ba1\u7b97\u548c\u5904\u7406\u65f6\u95f4\u95f4\u9694\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime, timedelta<\/p>\n<p>import time<\/p>\n<p>def scheduled_task():<\/p>\n<p>    print(&quot;\u4efb\u52a1\u6267\u884c\u65f6\u95f4:&quot;, datetime.now())<\/p>\n<h2><strong>\u5b9a\u4e49\u4efb\u52a1\u6267\u884c\u95f4\u9694<\/strong><\/h2>\n<p>interval = timedelta(seconds=10)<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>next_run_time = datetime.now() + interval<\/p>\n<p>while True:<\/p>\n<p>    # \u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/p>\n<p>    current_time = datetime.now()<\/p>\n<p>    # \u68c0\u67e5\u662f\u5426\u5230\u8fbe\u4efb\u52a1\u6267\u884c\u65f6\u95f4<\/p>\n<p>    if current_time &gt;= next_run_time:<\/p>\n<p>        # \u6267\u884c\u4efb\u52a1<\/p>\n<p>        scheduled_task()<\/p>\n<p>        # \u66f4\u65b0\u4e0b\u6b21\u4efb\u52a1\u6267\u884c\u65f6\u95f4<\/p>\n<p>        next_run_time = current_time + interval<\/p>\n<p>    # \u6682\u505c\u4e00\u6bb5\u65f6\u95f4\uff0c\u907f\u514d\u9ad8\u9891\u7387\u68c0\u67e5<\/p>\n<p>    time.sleep(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5b9a\u65f6\u4efb\u52a1<code>scheduled_task<\/code>\uff0c\u5e76\u4f7f\u7528datetime\u6a21\u5757\u8ba1\u7b97\u548c\u66f4\u65b0\u4efb\u52a1\u7684\u6267\u884c\u65f6\u95f4\u95f4\u9694\u3002\u901a\u8fc7\u5faa\u73af\u548c\u68c0\u67e5\u5f53\u524d\u65f6\u95f4\u4e0e\u4efb\u52a1\u6267\u884c\u65f6\u95f4\u7684\u5bf9\u6bd4\uff0c\u5b9e\u73b0\u5b9a\u65f6\u4efb\u52a1\u8c03\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u7edf\u8ba1\u65f6\u95f4\u95f4\u9694\u6709\u591a\u79cd\u65b9\u6cd5\u548c\u5de5\u5177\u53ef\u4f9b\u9009\u62e9\uff0c\u5177\u4f53\u9009\u62e9\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002<strong>datetime\u6a21\u5757\u9002\u7528\u4e8e\u4e00\u822c\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\u3001time\u6a21\u5757\u9002\u7528\u4e8e\u9ad8\u7cbe\u5ea6\u8ba1\u65f6\u3001pandas\u5e93\u9002\u7528\u4e8e\u590d\u6742\u7684\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u5206\u6790<\/strong>\u3002\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u548c\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u5904\u7406\u548c\u5206\u6790\u65f6\u95f4\u76f8\u5173\u7684\u6570\u636e\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u8fdb\u9636\u6280\u5de7\u548c\u4f18\u5316<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u88c5\u9970\u5668\u7edf\u8ba1\u51fd\u6570\u6267\u884c\u65f6\u95f4<\/h4>\n<\/p>\n<p><p>\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u7edf\u8ba1\u591a\u4e2a\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\u3002\u4f7f\u7528\u88c5\u9970\u5668\u53ef\u4ee5\u7b80\u5316\u8fd9\u4e00\u8fc7\u7a0b\uff0c\u5e76\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def timing_decorator(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        start_time = time.time()<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        end_time = time.time()<\/p>\n<p>        execution_time = end_time - start_time<\/p>\n<p>        print(f&quot;\u51fd\u6570 {func.__name__} \u6267\u884c\u65f6\u95f4\uff08\u79d2\uff09: {execution_time}&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@timing_decorator<\/p>\n<p>def example_function():<\/p>\n<p>    time.sleep(2)  # \u6a21\u62df\u51fd\u6570\u6267\u884c\u65f6\u95f4<\/p>\n<p>example_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u88c5\u9970\u5668<code>timing_decorator<\/code>\uff0c\u7528\u4e8e\u7edf\u8ba1\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\u3002\u901a\u8fc7\u4f7f\u7528\u88c5\u9970\u5668\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u4e3a\u591a\u4e2a\u51fd\u6570\u6dfb\u52a0\u8ba1\u65f6\u529f\u80fd\uff0c\u800c\u65e0\u9700\u4fee\u6539\u51fd\u6570\u672c\u8eab\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u63d0\u9ad8\u8ba1\u65f6\u7cbe\u5ea6<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u540c\u65f6\u7edf\u8ba1\u591a\u4e2a\u4efb\u52a1\u7684\u65f6\u95f4\u95f4\u9694\u3002\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u53ef\u4ee5\u63d0\u9ad8\u8ba1\u65f6\u7684\u7cbe\u5ea6\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>from threading import Thread<\/p>\n<p>def timed_task(task_id, duration):<\/p>\n<p>    print(f&quot;\u4efb\u52a1 {task_id} \u5f00\u59cb\u6267\u884c&quot;)<\/p>\n<p>    start_time = time.time()<\/p>\n<p>    time.sleep(duration)<\/p>\n<p>    end_time = time.time()<\/p>\n<p>    execution_time = end_time - start_time<\/p>\n<p>    print(f&quot;\u4efb\u52a1 {task_id} \u6267\u884c\u65f6\u95f4\uff08\u79d2\uff09: {execution_time}&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b<\/strong><\/h2>\n<p>threads = []<\/p>\n<p>for i in range(5):<\/p>\n<p>    thread = Thread(target=timed_task, args=(i, 2))<\/p>\n<p>    threads.append(thread)<\/p>\n<p>    thread.start()<\/p>\n<h2><strong>\u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u5b8c\u6210<\/strong><\/h2>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u8ba1\u65f6\u4efb\u52a1<code>timed_task<\/code>\uff0c\u5e76\u4f7f\u7528\u591a\u7ebf\u7a0b\u540c\u65f6\u6267\u884c\u591a\u4e2a\u4efb\u52a1\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u7edf\u8ba1\u591a\u4e2a\u4efb\u52a1\u7684\u65f6\u95f4\u95f4\u9694\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u65f6\u533a\u548c\u590f\u4ee4\u65f6<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u8de8\u65f6\u533a\u7684\u65f6\u95f4\u95f4\u9694\u65f6\uff0c\u6211\u4eec\u9700\u8981\u8003\u8651\u65f6\u533a\u548c\u590f\u4ee4\u65f6\u7684\u95ee\u9898\u3002datetime\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u65f6\u533a\u548c\u590f\u4ee4\u65f6\u7684\u652f\u6301\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u6b63\u786e\u5730\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>import pytz<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u5e26\u65f6\u533a\u7684datetime\u5bf9\u8c61<\/strong><\/h2>\n<p>start_time = datetime(2023, 10, 1, 12, 0, 0, tzinfo=pytz.timezone(&#39;America\/New_York&#39;))<\/p>\n<p>end_time = datetime(2023, 10, 1, 14, 30, 0, tzinfo=pytz.timezone(&#39;Europe\/London&#39;))<\/p>\n<h2><strong>\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694<\/strong><\/h2>\n<p>time_interval = end_time - start_time<\/p>\n<h2><strong>\u8f93\u51fa\u65f6\u95f4\u95f4\u9694<\/strong><\/h2>\n<p>print(&quot;\u65f6\u95f4\u95f4\u9694:&quot;, time_interval)<\/p>\n<p>print(&quot;\u65f6\u95f4\u95f4\u9694\uff08\u79d2\uff09:&quot;, time_interval.total_seconds())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>pytz<\/code>\u5e93\u4e3adatetime\u5bf9\u8c61\u6dfb\u52a0\u4e86\u65f6\u533a\u4fe1\u606f\uff0c\u5e76\u6b63\u786e\u5730\u8ba1\u7b97\u4e86\u8de8\u65f6\u533a\u7684\u65f6\u95f4\u95f4\u9694\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5904\u7406\u6d89\u53ca\u591a\u4e2a\u65f6\u533a\u7684\u590d\u6742\u65f6\u95f4\u95f4\u9694\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5b9e\u9645\u6848\u4f8b<\/h3>\n<\/p>\n<p><h4>1\u3001\u7f51\u7ad9\u7528\u6237\u505c\u7559\u65f6\u95f4\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u7f51\u7ad9\u5206\u6790\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u7edf\u8ba1\u7528\u6237\u5728\u7f51\u7ad9\u4e0a\u7684\u505c\u7559\u65f6\u95f4\uff0c\u4ee5\u4e86\u89e3\u7528\u6237\u884c\u4e3a\u548c\u7f51\u7ad9\u7684\u5438\u5f15\u529b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528pandas\u5e93\u7edf\u8ba1\u7528\u6237\u5728\u7f51\u7ad9\u4e0a\u7684\u505c\u7559\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;user_id&#39;: [1, 1, 2, 2, 3],<\/p>\n<p>        &#39;login_time&#39;: [&#39;2023-10-01 12:00:00&#39;, &#39;2023-10-01 12:15:00&#39;, &#39;2023-10-01 12:05:00&#39;, &#39;2023-10-01 12:20:00&#39;, &#39;2023-10-01 12:10:00&#39;],<\/p>\n<p>        &#39;logout_time&#39;: [&#39;2023-10-01 12:10:00&#39;, &#39;2023-10-01 12:30:00&#39;, &#39;2023-10-01 12:15:00&#39;, &#39;2023-10-01 12:35:00&#39;, &#39;2023-10-01 12:20:00&#39;]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5c06login_time\u548clogout_time\u5217\u8f6c\u6362\u4e3adatetime\u7c7b\u578b<\/strong><\/h2>\n<p>df[&#39;login_time&#39;] = pd.to_datetime(df[&#39;login_time&#39;])<\/p>\n<p>df[&#39;logout_time&#39;] = pd.to_datetime(df[&#39;logout_time&#39;])<\/p>\n<h2><strong>\u8ba1\u7b97\u6bcf\u6b21\u767b\u5f55\u7684\u505c\u7559\u65f6\u95f4<\/strong><\/h2>\n<p>df[&#39;session_duration&#39;] = (df[&#39;logout_time&#39;] - df[&#39;login_time&#39;]).dt.total_seconds()<\/p>\n<h2><strong>\u8ba1\u7b97\u6bcf\u4e2a\u7528\u6237\u7684\u603b\u505c\u7559\u65f6\u95f4<\/strong><\/h2>\n<p>total_duration_per_user = df.groupby(&#39;user_id&#39;)[&#39;session_duration&#39;].sum()<\/p>\n<p>print(total_duration_per_user)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u7528\u6237\u767b\u5f55\u548c\u767b\u51fa\u65f6\u95f4\u7684\u793a\u4f8b\u6570\u636e\u6846\uff0c\u5e76\u8ba1\u7b97\u4e86\u6bcf\u6b21\u767b\u5f55\u7684\u505c\u7559\u65f6\u95f4\u4ee5\u53ca\u6bcf\u4e2a\u7528\u6237\u7684\u603b\u505c\u7559\u65f6\u95f4\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5206\u6790\u7528\u6237\u5728\u7f51\u7ad9\u4e0a\u7684\u505c\u7559\u65f6\u95f4\uff0c\u5e76\u8fdb\u4e00\u6b65\u4f18\u5316\u7f51\u7ad9\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8ba2\u5355\u5904\u7406\u65f6\u95f4\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u8ba2\u5355\u5904\u7406\u7cfb\u7edf\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u7edf\u8ba1\u6bcf\u4e2a\u8ba2\u5355\u7684\u5904\u7406\u65f6\u95f4\uff0c\u4ee5\u8bc4\u4f30\u7cfb\u7edf\u7684\u6548\u7387\u5e76\u53d1\u73b0\u6f5c\u5728\u7684\u74f6\u9888\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528datetime\u6a21\u5757\u7edf\u8ba1\u8ba2\u5355\u7684\u5904\u7406\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>orders = [{&#39;order_id&#39;: 1, &#39;start_time&#39;: &#39;2023-10-01 12:00:00&#39;, &#39;end_time&#39;: &#39;2023-10-01 12:10:00&#39;},<\/p>\n<p>          {&#39;order_id&#39;: 2, &#39;start_time&#39;: &#39;2023-10-01 12:05:00&#39;, &#39;end_time&#39;: &#39;2023-10-01 12:20:00&#39;},<\/p>\n<p>          {&#39;order_id&#39;: 3, &#39;start_time&#39;: &#39;2023-10-01 12:10:00&#39;, &#39;end_time&#39;: &#39;2023-10-01 12:30:00&#39;}]<\/p>\n<h2><strong>\u8ba1\u7b97\u6bcf\u4e2a\u8ba2\u5355\u7684\u5904\u7406\u65f6\u95f4<\/strong><\/h2>\n<p>for order in orders:<\/p>\n<p>    start_time = datetime.strptime(order[&#39;start_time&#39;], &#39;%Y-%m-%d %H:%M:%S&#39;)<\/p>\n<p>    end_time = datetime.strptime(order[&#39;end_time&#39;], &#39;%Y-%m-%d %H:%M:%S&#39;)<\/p>\n<p>    processing_time = end_time - start_time<\/p>\n<p>    print(f&quot;\u8ba2\u5355 {order[&#39;order_id&#39;]} \u5904\u7406\u65f6\u95f4: {processing_time.total_seconds()} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u8ba2\u5355\u5f00\u59cb\u548c\u7ed3\u675f\u65f6\u95f4\u7684\u793a\u4f8b\u6570\u636e\u5217\u8868\uff0c\u5e76\u8ba1\u7b97\u4e86\u6bcf\u4e2a\u8ba2\u5355\u7684\u5904\u7406\u65f6\u95f4\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u76d1\u63a7\u8ba2\u5355\u7684\u5904\u7406\u65f6\u95f4\uff0c\u5e76\u4f18\u5316\u8ba2\u5355\u5904\u7406\u6d41\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f18\u5316\u5efa\u8bae<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u65f6\u95f4\u6570\u636e\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6548\u7387\u3002\u4f8b\u5982\uff0c\u4f7f\u7528numpy\u6570\u7ec4\u6216pandas\u6570\u636e\u6846\u53ef\u4ee5\u5feb\u901f\u8fdb\u884c\u5411\u91cf\u5316\u64cd\u4f5c\uff0c\u4ece\u800c\u63d0\u9ad8\u8ba1\u7b97\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u4f7f\u7528numpy\u6570\u7ec4<\/strong><\/h2>\n<p>start_times = np.array([&#39;2023-10-01 12:00:00&#39;, &#39;2023-10-01 12:05:00&#39;, &#39;2023-10-01 12:10:00&#39;], dtype=&#39;datetime64&#39;)<\/p>\n<p>end_times = np.array([&#39;2023-10-01 12:10:00&#39;, &#39;2023-10-01 12:20:00&#39;, &#39;2023-10-01 12:30:00&#39;], dtype=&#39;datetime64&#39;)<\/p>\n<h2><strong>\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694<\/strong><\/h2>\n<p>time_intervals = (end_times - start_times).astype(&#39;timedelta64[s]&#39;)<\/p>\n<p>print(&quot;\u65f6\u95f4\u95f4\u9694\uff08\u79d2\uff09:&quot;, time_intervals)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528numpy\u6570\u7ec4\u5b58\u50a8\u65f6\u95f4\u6570\u636e\uff0c\u5e76\u901a\u8fc7\u5411\u91cf\u5316\u64cd\u4f5c\u5feb\u901f\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u5177\u6709\u663e\u8457\u7684\u6027\u80fd\u4f18\u52bf\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5e76\u884c\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u65f6\u95f4\u6570\u636e\u65f6\uff0c\u4f7f\u7528\u5e76\u884c\u5904\u7406\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6280\u672f\u53ef\u4ee5\u5145\u5206\u5229\u7528\u591a\u6838CPU\u7684\u8ba1\u7b97\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from multiprocessing import Pool<\/p>\n<p>import pandas as pd<\/p>\n<p>def process_time_intervals(data):<\/p>\n<p>    start_time = pd.to_datetime(data[&#39;start_time&#39;])<\/p>\n<p>    end_time = pd.to_datetime(data[&#39;end_time&#39;])<\/p>\n<p>    return (end_time - start_time).total_seconds()<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = [{&#39;start_time&#39;: &#39;2023-10-01 12:00:00&#39;, &#39;end_time&#39;: &#39;2023-10-01 12:10:00&#39;},<\/p>\n<p>        {&#39;start_time&#39;: &#39;2023-10-01 12:05:00&#39;, &#39;end_time&#39;: &#39;2023-10-01 12:20:00&#39;},<\/p>\n<p>        {&#39;start_time&#39;: &#39;2023-10-01 12:10:00&#39;, &#39;end_time&#39;: &#39;2023-10-01 12:30:00&#39;}]<\/p>\n<h2><strong>\u4f7f\u7528\u591a\u8fdb\u7a0b\u5e76\u884c\u5904\u7406<\/strong><\/h2>\n<p>with Pool() as pool:<\/p>\n<p>    results = pool.map(process_time_intervals, data)<\/p>\n<p>print(&quot;\u65f6\u95f4\u95f4\u9694\uff08\u79d2\uff09:&quot;, results)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528multiprocessing\u6a21\u5757\u7684Pool\u7c7b\u521b\u5efa\u4e86\u4e00\u4e2a\u8fdb\u7a0b\u6c60\uff0c\u5e76\u901a\u8fc7\u5e76\u884c\u5904\u7406\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u5b66\u4e60\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5728Python\u4e2d\u7edf\u8ba1\u65f6\u95f4\u95f4\u9694\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528datetime\u6a21\u5757\u3001time\u6a21\u5757\u3001pandas\u5e93\u4ee5\u53ca\u7ed3\u5408\u88c5\u9970\u5668\u3001\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u7b49\u6280\u672f\u7684\u8fdb\u9636\u65b9\u6cd5\u3002<strong>\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u548c\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u5904\u7406\u548c\u5206\u6790\u65f6\u95f4\u76f8\u5173\u7684\u6570\u636e\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u6548\u7387\u3002<\/strong>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u4f18\u5316\u6280\u5de7\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u5347\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694\u7684\u6700\u7b80\u4fbf\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u662f\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694\u7684\u6700\u7b80\u4fbf\u65b9\u6cd5\u3002\u901a\u8fc7\u521b\u5efa<code>datetime<\/code>\u5bf9\u8c61\uff0c\u53ef\u4ee5\u8f7b\u677e\u8ba1\u7b97\u4e24\u4e2a\u65f6\u95f4\u70b9\u4e4b\u95f4\u7684\u5dee\u503c\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>datetime.now()<\/code>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff0c\u5e76\u7528\u51cf\u6cd5\u64cd\u4f5c\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from datetime import datetime\n\nstart_time = datetime.now()\n# \u5047\u8bbe\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\nend_time = datetime.now()\ntime_interval = end_time - start_time\nprint(f&quot;\u65f6\u95f4\u95f4\u9694\u4e3a: {time_interval}&quot;)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u683c\u5f0f\u5316\u65f6\u95f4\u95f4\u9694\u7684\u8f93\u51fa\u7ed3\u679c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c<code>timedelta<\/code>\u5bf9\u8c61\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u83b7\u53d6\u65f6\u95f4\u95f4\u9694\u7684\u5404\u4e2a\u90e8\u5206\uff0c\u5982\u5929\u6570\u3001\u79d2\u6570\u548c\u5fae\u79d2\u3002\u82e5\u60f3\u4ee5\u66f4\u5177\u53ef\u8bfb\u6027\u7684\u683c\u5f0f\u8f93\u51fa\uff0c\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u65b9\u6cd5\u8fdb\u884c\u683c\u5f0f\u5316\uff1a<\/p>\n<pre><code class=\"language-python\">from datetime import timedelta\n\n# \u5047\u8bbe\u6709\u4e00\u4e2atimedelta\u5bf9\u8c61\ntime_interval = timedelta(days=2, hours=5, minutes=30)\n\ndays = time_interval.days\nseconds = time_interval.seconds\nhours = seconds \/\/ 3600\nminutes = (seconds % 3600) \/\/ 60\n\nprint(f&quot;\u65f6\u95f4\u95f4\u9694\u4e3a: {days}\u5929 {hours}\u5c0f\u65f6 {minutes}\u5206\u949f&quot;)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u4e0d\u540c\u65f6\u95f4\u683c\u5f0f\u4ee5\u8ba1\u7b97\u95f4\u9694\uff1f<\/strong><br \/>\u5728\u5904\u7406\u4e0d\u540c\u65f6\u95f4\u683c\u5f0f\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>strptime<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a<code>datetime<\/code>\u5bf9\u8c61\u3002\u6839\u636e\u4e0d\u540c\u7684\u65e5\u671f\u683c\u5f0f\uff0c\u53ef\u4ee5\u7075\u6d3b\u6307\u5b9a\u683c\u5f0f\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">from datetime import datetime\n\ntime_format = &quot;%Y-%m-%d %H:%M:%S&quot;\nstart_time = datetime.strptime(&quot;2023-10-01 12:00:00&quot;, time_format)\nend_time = datetime.strptime(&quot;2023-10-03 15:30:00&quot;, time_format)\n\ntime_interval = end_time - start_time\nprint(f&quot;\u65f6\u95f4\u95f4\u9694\u4e3a: {time_interval}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u786e\u4fdd\u60a8\u80fd\u591f\u5904\u7406\u591a\u79cd\u4e0d\u540c\u7684\u65f6\u95f4\u8f93\u5165\u683c\u5f0f\uff0c\u8fdb\u800c\u51c6\u786e\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528datetime\u6a21\u5757\u3001time\u6a21\u5757\u3001pandas\u5e93\u6765\u7edf\u8ba1\u65f6\u95f4\u95f4\u9694\u3002datetime\u6a21 [&hellip;]","protected":false},"author":3,"featured_media":1178367,"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\/1178359"}],"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=1178359"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1178359\/revisions"}],"predecessor-version":[{"id":1178369,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1178359\/revisions\/1178369"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1178367"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1178359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1178359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1178359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}