{"id":953753,"date":"2024-12-27T01:47:17","date_gmt":"2024-12-26T17:47:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/953753.html"},"modified":"2024-12-27T01:47:19","modified_gmt":"2024-12-26T17:47:19","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e5%bd%93%e5%89%8d%e5%b0%8f%e6%97%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/953753.html","title":{"rendered":"python\u5982\u4f55\u83b7\u53d6\u5f53\u524d\u5c0f\u65f6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094115\/243908f6-8525-4630-9dba-4e8396f1a5aa.webp\" alt=\"python\u5982\u4f55\u83b7\u53d6\u5f53\u524d\u5c0f\u65f6\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u83b7\u53d6\u5f53\u524d\u5c0f\u65f6\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\uff0c\u901a\u8fc7\u8c03\u7528<code>datetime.datetime.now().hour<\/code>\u6765\u5b9e\u73b0\u3001\u53ef\u4ee5\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u7684<code>time.localtime().tm_hour<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528<code>pandas<\/code>\u5e93\u4e2d\u7684<code>Timestamp.now().hour<\/code>\u65b9\u6cd5\u3002<\/strong>\u5176\u4e2d\uff0c<code>datetime<\/code>\u6a21\u5757\u662f\u6700\u5e38\u7528\u7684\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u5c0f\u65f6\uff0c\u8fd8\u53ef\u4ee5\u83b7\u53d6\u7cbe\u786e\u5230\u79d2\u7684\u5f53\u524d\u65f6\u95f4\u3002\u63a5\u4e0b\u6765\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u83b7\u53d6\u5f53\u524d\u5c0f\u65f6\u3002<\/p>\n<\/p>\n<p><p><code>datetime<\/code>\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u6807\u51c6\u6a21\u5757\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u800c\u6709\u6548\u7684\u65b9\u5f0f\u6765\u64cd\u4f5c\u65e5\u671f\u548c\u65f6\u95f4\u3002\u5728\u8fd9\u4e2a\u6a21\u5757\u4e2d\uff0c<code>datetime.now()<\/code>\u65b9\u6cd5\u8fd4\u56de\u5f53\u524d\u7684\u672c\u5730\u65e5\u671f\u548c\u65f6\u95f4\u3002\u901a\u8fc7<code>.hour<\/code>\u5c5e\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u8fd4\u56de\u7684<code>datetime<\/code>\u5bf9\u8c61\u4e2d\u63d0\u53d6\u5f53\u524d\u5c0f\u65f6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>current_hour = datetime.now().hour<\/p>\n<p>print(&quot;\u5f53\u524d\u5c0f\u65f6\u662f\uff1a&quot;, current_hour)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528DATETIME\u6a21\u5757\u83b7\u53d6\u5f53\u524d\u5c0f\u65f6<\/h3>\n<\/p>\n<p><p><code>datetime<\/code>\u6a21\u5757\u662fPython\u4e2d\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u6807\u51c6\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u7c7b\u6765\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u4fe1\u606f\uff0c\u5305\u62ec\u5e74\u3001\u6708\u3001\u65e5\u3001\u65f6\u3001\u5206\u3001\u79d2\u3001\u5fae\u79d2\u7b49\u3002\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u83b7\u53d6\u5f53\u524d\u5c0f\u65f6\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528DATETIME.NOW()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>datetime.now()<\/code>\u8fd4\u56de\u5f53\u524d\u7684\u672c\u5730\u65f6\u95f4\uff0c\u5305\u542b\u5b8c\u6574\u7684\u65e5\u671f\u548c\u65f6\u95f4\u4fe1\u606f\u3002\u901a\u8fc7\u8bbf\u95ee<code>.hour<\/code>\u5c5e\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u83b7\u53d6\u5f53\u524d\u7684\u5c0f\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>def get_current_hour():<\/p>\n<p>    current_time = datetime.now()<\/p>\n<p>    current_hour = current_time.hour<\/p>\n<p>    return current_hour<\/p>\n<p>print(&quot;\u5f53\u524d\u5c0f\u65f6\u662f\uff1a&quot;, get_current_hour())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165<code>datetime<\/code>\u6a21\u5757\uff0c\u7136\u540e\u8c03\u7528<code>datetime.now()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u7684\u672c\u5730\u65e5\u671f\u548c\u65f6\u95f4\u3002\u63a5\u7740\uff0c\u901a\u8fc7<code>.hour<\/code>\u5c5e\u6027\u63d0\u53d6\u5f53\u524d\u5c0f\u65f6\uff0c\u5e76\u5c06\u5176\u8fd4\u56de\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528DATETIME\u5bf9\u8c61\u7684\u7ec4\u5408<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5904\u7406\u591a\u4e2a\u65f6\u95f4\u4fe1\u606f\uff0c\u6bd4\u5982\u83b7\u53d6\u5f53\u524d\u7684\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u3002\u6b64\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u4fe1\u606f\u7ec4\u5408\u5728\u4e00\u8d77\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>def get_time_det<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ls():<\/p>\n<p>    current_time = datetime.now()<\/p>\n<p>    current_hour = current_time.hour<\/p>\n<p>    current_minute = current_time.minute<\/p>\n<p>    current_second = current_time.second<\/p>\n<p>    return current_hour, current_minute, current_second<\/p>\n<p>hour, minute, second = get_time_details()<\/p>\n<p>print(f&quot;\u5f53\u524d\u65f6\u95f4\u662f\uff1a{hour}\u65f6{minute}\u5206{second}\u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4e0d\u4ec5\u83b7\u53d6\u4e86\u5f53\u524d\u7684\u5c0f\u65f6\uff0c\u8fd8\u540c\u65f6\u83b7\u53d6\u4e86\u5206\u949f\u548c\u79d2\uff0c\u5e76\u5c06\u5b83\u4eec\u4e00\u8d77\u8fd4\u56de\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528TIME\u6a21\u5757\u83b7\u53d6\u5f53\u524d\u5c0f\u65f6<\/h3>\n<\/p>\n<p><p>\u9664\u4e86<code>datetime<\/code>\u6a21\u5757\u5916\uff0c<code>time<\/code>\u6a21\u5757\u4e5f\u662fPython\u4e2d\u5904\u7406\u65f6\u95f4\u4fe1\u606f\u7684\u6807\u51c6\u5e93\u3002<code>time<\/code>\u6a21\u5757\u66f4\u503e\u5411\u4e8e\u5904\u7406\u65f6\u95f4\u6233\u548c\u57fa\u4e8e\u65f6\u95f4\u7684\u64cd\u4f5c\u3002\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u83b7\u53d6\u5f53\u524d\u5c0f\u65f6\u4e5f\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528TIME.LOCALTIME()\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>time.localtime()<\/code>\u8fd4\u56de\u5f53\u524d\u7684\u672c\u5730\u65f6\u95f4\u5143\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u5e74\u3001\u6708\u3001\u65e5\u3001\u65f6\u3001\u5206\u3001\u79d2\u7b49\u4fe1\u606f\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6216\u5c5e\u6027\u8bbf\u95ee\u5c0f\u65f6\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def get_local_hour():<\/p>\n<p>    current_time = time.localtime()<\/p>\n<p>    current_hour = current_time.tm_hour<\/p>\n<p>    return current_hour<\/p>\n<p>print(&quot;\u5f53\u524d\u5c0f\u65f6\u662f\uff1a&quot;, get_local_hour())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165<code>time<\/code>\u6a21\u5757\uff0c\u7136\u540e\u8c03\u7528<code>time.localtime()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u7684\u672c\u5730\u65f6\u95f4\u5143\u7ec4\u3002\u63a5\u7740\uff0c\u901a\u8fc7\u8bbf\u95ee<code>tm_hour<\/code>\u5c5e\u6027\u63d0\u53d6\u5f53\u524d\u5c0f\u65f6\uff0c\u5e76\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528TIME\u6a21\u5757\u7684\u5176\u4ed6\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>time<\/code>\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5176\u4ed6\u65b9\u6cd5\uff0c\u5982<code>time.gmtime()<\/code>\uff0c\u53ef\u4ee5\u83b7\u53d6UTC\u65f6\u95f4\u4fe1\u606f\u3002\u8fd9\u5728\u9700\u8981\u5904\u7406\u8de8\u65f6\u533a\u65f6\u95f4\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def get_utc_hour():<\/p>\n<p>    current_time = time.gmtime()<\/p>\n<p>    current_hour = current_time.tm_hour<\/p>\n<p>    return current_hour<\/p>\n<p>print(&quot;\u5f53\u524dUTC\u5c0f\u65f6\u662f\uff1a&quot;, get_utc_hour())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>time.gmtime()<\/code>\u65b9\u6cd5\u83b7\u53d6UTC\u65f6\u95f4\uff0c\u5e76\u901a\u8fc7\u8bbf\u95ee<code>tm_hour<\/code>\u5c5e\u6027\u63d0\u53d6\u5f53\u524d\u7684UTC\u5c0f\u65f6\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528PANDAS\u5e93\u83b7\u53d6\u5f53\u524d\u5c0f\u65f6<\/h3>\n<\/p>\n<p><p><code>pandas<\/code>\u662fPython\u4e2d\u7684\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u901a\u5e38\u7528\u4e8e\u5904\u7406\u6570\u636e\u6846\u548c\u65f6\u95f4\u5e8f\u5217\u3002\u867d\u7136\u5b83\u4e0d\u662f\u4e13\u95e8\u4e3a\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u800c\u8bbe\u8ba1\u7684\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u8fd9\u4e2a\u76ee\u7684\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528PANDAS.TIMESTAMP\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>pandas<\/code>\u63d0\u4f9b\u4e86<code>Timestamp<\/code>\u5bf9\u8c61\uff0c\u5b83\u7c7b\u4f3c\u4e8e<code>datetime<\/code>\u5bf9\u8c61\uff0c\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def get_pandas_hour():<\/p>\n<p>    current_time = pd.Timestamp.now()<\/p>\n<p>    current_hour = current_time.hour<\/p>\n<p>    return current_hour<\/p>\n<p>print(&quot;\u5f53\u524d\u5c0f\u65f6\u662f\uff1a&quot;, get_pandas_hour())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165<code>pandas<\/code>\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>pd.Timestamp.now()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u3002\u63a5\u7740\uff0c\u901a\u8fc7\u8bbf\u95ee<code>.hour<\/code>\u5c5e\u6027\u63d0\u53d6\u5f53\u524d\u5c0f\u65f6\uff0c\u5e76\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528PANDAS\u5904\u7406\u65f6\u95f4\u5e8f\u5217<\/h4>\n<\/p>\n<p><p><code>pandas<\/code>\u5e93\u901a\u5e38\u7528\u4e8e\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5206\u6790\u548c\u64cd\u4f5c\u5305\u542b\u65f6\u95f4\u4fe1\u606f\u7684\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def analyze_time_series():<\/p>\n<p>    dates = pd.date_range(start=&#39;2023-01-01&#39;, periods=10, freq=&#39;H&#39;)<\/p>\n<p>    data = pd.Series(range(10), index=dates)<\/p>\n<p>    current_hour = data.index.hour<\/p>\n<p>    return current_hour<\/p>\n<p>print(&quot;\u65f6\u95f4\u5e8f\u5217\u4e2d\u7684\u5c0f\u65f6\u4fe1\u606f\uff1a&quot;, analyze_time_series())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>pd.date_range()<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u65f6\u95f4\u5e8f\u5217\u7684\u6570\u636e\u96c6\uff0c\u5e76\u901a\u8fc7\u8bbf\u95ee<code>index.hour<\/code>\u63d0\u53d6\u5c0f\u65f6\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u83b7\u53d6\u5f53\u524d\u5c0f\u65f6<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Python\u7684\u5185\u7f6e\u6a21\u5757\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u4fe1\u606f\u3002\u5176\u4e2d\uff0c<code>arrow<\/code>\u548c<code>pendulum<\/code>\u662f\u6bd4\u8f83\u5e38\u7528\u7684\u5e93\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528ARROW\u5e93<\/h4>\n<\/p>\n<p><p><code>arrow<\/code>\u662f\u4e00\u4e2a\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u76f4\u89c2\u7684API\u6765\u64cd\u4f5c\u65f6\u95f4\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import arrow<\/p>\n<p>def get_arrow_hour():<\/p>\n<p>    current_time = arrow.now()<\/p>\n<p>    current_hour = current_time.hour<\/p>\n<p>    return current_hour<\/p>\n<p>print(&quot;\u5f53\u524d\u5c0f\u65f6\u662f\uff1a&quot;, get_arrow_hour())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b89\u88c5\u5e76\u5bfc\u5165<code>arrow<\/code>\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>arrow.now()<\/code>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u3002\u63a5\u7740\uff0c\u901a\u8fc7\u8bbf\u95ee<code>.hour<\/code>\u5c5e\u6027\u63d0\u53d6\u5f53\u524d\u5c0f\u65f6\uff0c\u5e76\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528PENDULUM\u5e93<\/h4>\n<\/p>\n<p><p><code>pendulum<\/code>\u662f\u53e6\u4e00\u4e2a\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u65f6\u533a\u652f\u6301\u548c\u66f4\u7075\u6d3b\u7684\u65f6\u95f4\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pendulum<\/p>\n<p>def get_pendulum_hour():<\/p>\n<p>    current_time = pendulum.now()<\/p>\n<p>    current_hour = current_time.hour<\/p>\n<p>    return current_hour<\/p>\n<p>print(&quot;\u5f53\u524d\u5c0f\u65f6\u662f\uff1a&quot;, get_pendulum_hour())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b89\u88c5\u5e76\u5bfc\u5165<code>pendulum<\/code>\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>pendulum.now()<\/code>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u3002\u63a5\u7740\uff0c\u901a\u8fc7\u8bbf\u95ee<code>.hour<\/code>\u5c5e\u6027\u63d0\u53d6\u5f53\u524d\u5c0f\u65f6\uff0c\u5e76\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u65f6\u95f4\u64cd\u4f5c\u7684\u5b9e\u7528\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u83b7\u53d6\u5f53\u524d\u5c0f\u65f6\u5f80\u5f80\u662f\u4e3a\u4e86\u8fdb\u884c\u66f4\u590d\u6742\u7684\u65f6\u95f4\u64cd\u4f5c\u6216\u6570\u636e\u5206\u6790\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><h4>1. \u57fa\u4e8e\u5c0f\u65f6\u7684\u81ea\u52a8\u5316\u4efb\u52a1<\/h4>\n<\/p>\n<p><p>\u5728\u81ea\u52a8\u5316\u4efb\u52a1\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u6839\u636e\u5f53\u524d\u5c0f\u65f6\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u5b9a\u65f6\u53d1\u9001\u63d0\u9192\u6216\u62a5\u544a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def scheduled_task():<\/p>\n<p>    current_hour = datetime.now().hour<\/p>\n<p>    if current_hour &gt;= 9 and current_hour &lt;= 17:<\/p>\n<p>        print(&quot;\u5de5\u4f5c\u65f6\u95f4\uff0c\u6267\u884c\u4efb\u52a1&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;\u975e\u5de5\u4f5c\u65f6\u95f4\uff0c\u6682\u505c\u4efb\u52a1&quot;)<\/p>\n<p>scheduled_task()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6839\u636e\u5f53\u524d\u5c0f\u65f6\u5224\u65ad\u662f\u5426\u5904\u4e8e\u5de5\u4f5c\u65f6\u95f4\uff0c\u5e76\u51b3\u5b9a\u662f\u5426\u6267\u884c\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>2. \u6570\u636e\u5206\u6790\u4e2d\u7684\u65f6\u95f4\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u65f6\u95f4\u4fe1\u606f\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u90e8\u5206\u3002\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5c0f\u65f6\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def analyze_data_by_hour(data):<\/p>\n<p>    data[&#39;Hour&#39;] = data[&#39;Timestamp&#39;].dt.hour<\/p>\n<p>    hourly_data = data.groupby(&#39;Hour&#39;).sum()<\/p>\n<p>    return hourly_data<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = pd.DataFrame({<\/p>\n<p>    &#39;Timestamp&#39;: pd.date_range(start=&#39;2023-01-01&#39;, periods=100, freq=&#39;H&#39;),<\/p>\n<p>    &#39;Value&#39;: range(100)<\/p>\n<p>})<\/p>\n<p>result = analyze_data_by_hour(data)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u65f6\u95f4\u6233\u548c\u6570\u503c\u7684\u6570\u636e\u96c6\uff0c\u7136\u540e\u6839\u636e\u5c0f\u65f6\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u548c\u6c47\u603b\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u83b7\u53d6\u5f53\u524d\u5c0f\u65f6\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4f9b\u9009\u62e9\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u6a21\u5757\u5982<code>datetime<\/code>\u548c<code>time<\/code>\uff0c\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u5982<code>pandas<\/code>\u3001<code>arrow<\/code>\u548c<code>pendulum<\/code>\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f18\u5148\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757<\/strong>\uff1a\u5bf9\u4e8e\u5927\u591a\u6570\u57fa\u672c\u7684\u65f6\u95f4\u64cd\u4f5c\uff0c<code>datetime<\/code>\u6a21\u5757\u662f\u6700\u7b80\u5355\u548c\u76f4\u63a5\u7684\u9009\u62e9\u3002<\/li>\n<li><strong>\u5229\u7528<code>time<\/code>\u6a21\u5757\u8fdb\u884c\u4f4e\u7ea7\u65f6\u95f4\u64cd\u4f5c<\/strong>\uff1a\u5982\u679c\u9700\u8981\u5904\u7406\u65f6\u95f4\u6233\u6216\u8fdb\u884c\u9ad8\u6027\u80fd\u7684\u65f6\u95f4\u8ba1\u7b97\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u3002<\/li>\n<li><strong>\u4f7f\u7528<code>pandas<\/code>\u8fdb\u884c\u6570\u636e\u5206\u6790<\/strong>\uff1a\u5982\u679c\u6d89\u53ca\u5230\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u548c\u65f6\u95f4\u5e8f\u5217\u64cd\u4f5c\uff0c<code>pandas<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u3002<\/li>\n<li><strong>\u8003\u8651\u7b2c\u4e09\u65b9\u5e93\u7684\u4f18\u52bf<\/strong>\uff1a\u5bf9\u4e8e\u9700\u8981\u66f4\u9ad8\u7ea7\u529f\u80fd\u7684\u5e94\u7528\uff0c\u5982\u65f6\u533a\u5904\u7406\u6216\u66f4\u76f4\u89c2\u7684API\uff0c\u7b2c\u4e09\u65b9\u5e93\u5982<code>arrow<\/code>\u548c<code>pendulum<\/code>\u53ef\u4ee5\u63d0\u4f9b\u66f4\u597d\u7684\u652f\u6301\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u4e86\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u9ad8\u6548\u5730\u8fdb\u884c\u65f6\u95f4\u64cd\u4f5c\u548c\u6570\u636e\u5206\u6790\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\u65f6\u95f4\u7684\u5c0f\u65f6\u90e8\u5206\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u6765\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u5c0f\u65f6\u90e8\u5206\u3002\u9996\u5148\uff0c\u5bfc\u5165<code>datetime<\/code>\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>datetime.now()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff0c\u518d\u901a\u8fc7<code>.hour<\/code>\u5c5e\u6027\u63d0\u53d6\u5c0f\u65f6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">from datetime import datetime\ncurrent_hour = datetime.now().hour\nprint(current_hour)\n<\/code><\/pre>\n<p><strong>\u83b7\u53d6\u5f53\u524d\u5c0f\u65f6\u662f\u5426\u8003\u8651\u65f6\u533a\uff1f<\/strong><br \/>\u83b7\u53d6\u5f53\u524d\u5c0f\u65f6\u65f6\uff0c<code>datetime.now()<\/code>\u4f7f\u7528\u7684\u662f\u7cfb\u7edf\u7684\u672c\u5730\u65f6\u95f4\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u7279\u5b9a\u65f6\u533a\u7684\u65f6\u95f4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pytz<\/code>\u5e93\u6765\u786e\u4fdd\u83b7\u53d6\u7684\u5c0f\u65f6\u662f\u6309\u7167\u6240\u9700\u65f6\u533a\u7684\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import pytz\nfrom datetime import datetime\n\ntimezone = pytz.timezone(&#39;Asia\/Shanghai&#39;)\ncurrent_hour = datetime.now(timezone).hour\nprint(current_hour)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5c06\u83b7\u53d6\u7684\u5f53\u524d\u5c0f\u65f6\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u5c06\u5f53\u524d\u5c0f\u65f6\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528<code>strftime<\/code>\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u5141\u8bb8\u60a8\u5b9a\u4e49\u8f93\u51fa\u7684\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5c06\u5c0f\u65f6\u683c\u5f0f\u5316\u4e3a\u4e24\u4f4d\u6570\u7684\u5b57\u7b26\u4e32\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">from datetime import datetime\n\ncurrent_hour_str = datetime.now().strftime(&quot;%H&quot;)\nprint(current_hour_str)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u83b7\u53d6\u5f53\u524d\u5c0f\u65f6\u53ef\u4ee5\u4f7f\u7528datetime\u6a21\u5757\uff0c\u901a\u8fc7\u8c03\u7528datetime.datetime.now( [&hellip;]","protected":false},"author":3,"featured_media":953757,"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\/953753"}],"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=953753"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/953753\/revisions"}],"predecessor-version":[{"id":953760,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/953753\/revisions\/953760"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/953757"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=953753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=953753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=953753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}