{"id":318,"date":"2025-12-12T00:23:56","date_gmt":"2025-12-11T16:23:56","guid":{"rendered":"https:\/\/www.xxiaoteng.cn\/?p=318"},"modified":"2026-05-11T21:27:00","modified_gmt":"2026-05-11T13:27:00","slug":"datetime%e6%97%b6%e9%97%b4%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/www.xxiaoteng.cn\/?p=318","title":{"rendered":"datetime\u65f6\u95f4\u6a21\u5757"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">strptime: \"parse\" (\u89e3\u6790) \u2192 \u4ece\u5b57\u7b26\u4e32\u89e3\u6790\u51fa\u65f6\u95f4\uff0c<strong>\u5b57\u7b26\u4e32 \u2192 \u65f6\u95f4\u5bf9\u8c61<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">strftime: \"format\" (\u683c\u5f0f\u5316) \u2192 \u5c06\u65f6\u95f4\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\uff0c<strong>\u65f6\u95f4\u5bf9\u8c61 \u2192 \u5b57\u7b26\u4e32<\/strong>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc-0\">1\u3001\u65f6\u95f4\u89e3\u6790\u4e0e\u683c\u5f0f\u5316<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u65e5\u5fd7\u5206\u6790\u4e2d\uff0c\u65f6\u95f4\u5b57\u6bb5\u901a\u5e38\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u683c\u5f0f\uff08\u5982 <code class=\"prettyprint\" >22\/Nov\/2024:10:00:00<\/code>\uff09\u3002\u4e3a\u4e86\u5bf9\u65f6\u95f4\u5b57\u6bb5\u8fdb\u884c\u64cd\u4f5c\uff0c\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a <code class=\"prettyprint\" >datetime<\/code> \u5bf9\u8c61\u3002Python \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684 <code class=\"prettyprint\" >datetime<\/code> \u6a21\u5757\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b8c\u6210\u65f6\u95f4\u7684\u89e3\u6790\u4e0e\u683c\u5f0f\u5316\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e73\u5e38\u6bd4\u8f83\u719f\u6089\u7684\u65f6\u95f4\u683c\u5f0f\uff1a\u5e74-\u6708-\u65e5 \u5c0f\u65f6:\u5206\u949f:\u79d2<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc-1\">2\u3001\u65f6\u95f4\u89e3\u6790<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code class=\"prettyprint\" >datetime.strptime()<\/code> \u65b9\u6cd5\u89e3\u6790\u65f6\u95f4\u5b57\u7b26\u4e32\u3002<\/li>\n\n\n\n<li>\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7528\u4e8e\u5b9a\u4e49\u65f6\u95f4\u5b57\u6bb5\u7684\u683c\u5f0f\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u6848\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >from datetime import datetime\n\n# \u793a\u4f8b\u65f6\u95f4\u5b57\u7b26\u4e32\ntime_str = &quot;22\/Nov\/2024:10:00:00&quot;\n\n# \u89e3\u6790\u65f6\u95f4\u5b57\u7b26\u4e32\u4e3a datetime \u5bf9\u8c61\nlog_time = datetime.strptime(time_str, &quot;%d\/%b\/%Y:%H:%M:%S&quot;)\n\n# \u8f93\u51fa\u89e3\u6790\u540e\u7684 datetime \u5bf9\u8c61\nprint(&quot;\u89e3\u6790\u540e\u7684\u65f6\u95f4:&quot;, log_time)\n\n\u8bf4\u660e\uff1a\nfrom datetime import datetime\nfrom \u6a21\u5757 import \u51fd\u6570\nfrom \u6a21\u5757 import class\u7c7b\uff08\u91cc\u9762\u6709\u5f88\u591a\u51fd\u6570\uff0c\u8c03\u7528\u51fd\u6570\uff0c\u9700\u8981\u901a\u8fc7\u8fd9\u4e2a\u7c7b\uff09<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u89e3\u6790\u540e\u7684\u65f6\u95f4: <code class=\"prettyprint\" >2024-11-22 10:00:00<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc-2\">3\u3001\u65f6\u95f4\u683c\u5f0f\u5316<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code class=\"prettyprint\" >datetime.strftime()<\/code> \u65b9\u6cd5\u5c06\u65f6\u95f4\u5bf9\u8c61\u8f6c\u6362\u4e3a\u7279\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n\n\n\n<li>\u53ef\u7528\u4e8e\u751f\u6210\u81ea\u5b9a\u4e49\u7684\u65f6\u95f4\u8f93\u51fa\u683c\u5f0f\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u6848\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" ># \u5c06 datetime \u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\nformatted_time = log_time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)\nprint(&quot;\u683c\u5f0f\u5316\u540e\u7684\u65f6\u95f4:&quot;, formatted_time)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u683c\u5f0f\u5316\u540e\u7684\u65f6\u95f4: <code class=\"prettyprint\" >2024-11-22 10:00:00<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc-3\">4\u3001\u65f6\u95f4\u8303\u56f4\u6bd4\u8f83\uff08\u91cd\u70b9\uff09<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u65e5\u5fd7\u5206\u6790\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u7b5b\u9009\u67d0\u4e2a\u65f6\u95f4\u8303\u56f4\u5185\u7684\u65e5\u5fd7\u8bb0\u5f55\u3002\u901a\u8fc7\u6bd4\u8f83\u4e24\u4e2a <code class=\"prettyprint\" >datetime<\/code> \u5bf9\u8c61\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u65f6\u95f4\u8303\u56f4\u8fc7\u6ee4\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"toc-4\">\u2606 \u65f6\u95f4\u8303\u56f4\u6bd4\u8f83<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 >=<code class=\"prettyprint\" >\u548c<\/code>&lt;= \u8fd0\u7b97\u7b26\u6bd4\u8f83\u65f6\u95f4\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li>\u7b5b\u9009\u51fa\u5728\u6307\u5b9a\u65f6\u95f4\u8303\u56f4\u5185\u7684\u65e5\u5fd7\u8bb0\u5f55\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u6848\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" ># \u5b9a\u4e49\u65f6\u95f4\u8303\u56f4\nstart_time = datetime.strptime(&quot;22\/Nov\/2024:10:00:00&quot;, &quot;%d\/%b\/%Y:%H:%M:%S&quot;)\nend_time = datetime.strptime(&quot;22\/Nov\/2024:10:10:00&quot;, &quot;%d\/%b\/%Y:%H:%M:%S&quot;)\n\n# \u793a\u4f8b\u65e5\u5fd7\u65f6\u95f4\nlog_time = datetime.strptime(&quot;22\/Nov\/2024:10:05:00&quot;, &quot;%d\/%b\/%Y:%H:%M:%S&quot;)\n\n# \u68c0\u67e5\u65e5\u5fd7\u65f6\u95f4\u662f\u5426\u5728\u8303\u56f4\u5185\nif start_time &lt;= log_time &lt;= end_time:\n    print(&quot;\u65e5\u5fd7\u65f6\u95f4\u5728\u8303\u56f4\u5185&quot;)\nelse:\n    print(&quot;\u65e5\u5fd7\u65f6\u95f4\u4e0d\u5728\u8303\u56f4\u5185&quot;)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ce8\u610f\uff1aPython 2.6\u53ca\u4ee5\u540e\u7684\u7248\u672c\u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u65e5\u671f\u65f6\u95f4\u5904\u7406\u80fd\u529b\uff08\u5982\u5b57\u7b26\u4e32\u65f6\u95f4\u683c\u5f0f\u6bd4\u8f83\uff0c\u4f46\u662f\u8981\u6c42\u683c\u5f0f\u5fc5\u987b\u5b8c\u5168\u4e00\u81f4\uff0c\u5426\u5219\u65e0\u6cd5\u6bd4\u8f83\uff09\uff0c\u4f46\u5bf9\u4e8e\u590d\u6742\u7684\u65e5\u671f\u548c\u65f6\u95f4\u64cd\u4f5c\uff0c\u4f7f\u7528<code class=\"prettyprint\" >datetime<\/code>\u6a21\u5757\u4f1a\u66f4\u4e3a\u65b9\u4fbf\u548c\u5f3a\u5927\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"toc-5\">\u2606 \u65f6\u95f4\u8303\u56f4\u8fc7\u6ee4Nginx\u65e5\u5fd7\u529f\u80fd\uff08\u91cd\u70b9\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6848\u4f8b\uff1a2025-01-15 02:00:00 ~ 06:00:00\u53d1\u73b0Nginx\u670d\u52a1\u5f02\u5e38\uff0c\u5148\u9700\u8981\u6211\u4eec\u4eceNginx\u7684access.log\u8bbf\u95ee\u65e5\u5fd7\u4e2d\u63d0\u53d6\u8fd9\u6bb5\u65f6\u95f4\u7684\u65e5\u5fd7\u4fe1\u606f\uff0c\u4f7f\u7528Python\u5b9e\u73b0\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9e\u73b0\u601d\u8def\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.xxiaoteng.cn\/wp-content\/uploads\/2026\/05\/codeNWY0MWEyOTViYTI3YjU1NGIwMzY4M2M1NmRjMGEwNGJfaVlmcWVuUnhWSWxhdURhUWhKcHlDdjA1Sk84ZWl3dkpfVG9rZW46UEhkWGJWVUx4b0x4Y3p4a05aOWNybjJWbmloXzE3Nzg1MDIyMjI6MTc3ODUwNTgyMl9WNA.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5177\u4f53\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >from datetime import datetime\n\n# \u8bfb\u53d6\u65e5\u5fd7\u6587\u4ef6\u5e76\u6309\u65f6\u95f4\u8303\u56f4\u7b5b\u9009\ndef filter_logs_by_time(log_file, start_time, end_time):\n    results = &#091;]\n    with open(log_file, &quot;r&quot;) as file:\n        for line in file:\n            parts = line.split()\n            # \u63d0\u53d6\u65e5\u5fd7\u65f6\u95f4\u5b57\u6bb5\u5e76\u89e3\u6790\u4e3a datetime \u5bf9\u8c61\n            log_time = datetime.strptime(parts&#091;3]&#091;1:], &quot;%d\/%b\/%Y:%H:%M:%S&quot;)\n            # \u68c0\u67e5\u65f6\u95f4\u662f\u5426\u5728\u8303\u56f4\u5185\n            if start_time &lt;= log_time &lt;= end_time:\n                results.append(line.strip())\n    return results\n\n# \u793a\u4f8b\u4f7f\u7528\nif __name__ == &quot;__main__&quot;:\n    # \u5b9a\u4e49\u65f6\u95f4\u8303\u56f4\n    start_time = datetime.strptime(&quot;22\/Nov\/2024:10:00:00&quot;, &quot;%d\/%b\/%Y:%H:%M:%S&quot;)\n    end_time = datetime.strptime(&quot;22\/Nov\/2024:10:10:00&quot;, &quot;%d\/%b\/%Y:%H:%M:%S&quot;)\n    \n    # \u65e5\u5fd7\u6587\u4ef6\u8def\u5f84\n    log_file = &quot;nginx_access.log&quot;\n    \n    # \u7b5b\u9009\u65e5\u5fd7\n    filtered_logs = filter_logs_by_time(log_file, start_time, end_time)\n    print(&quot;\u6307\u5b9a\u65f6\u95f4\u8303\u56f4\u5185\u7684\u65e5\u5fd7\u8bb0\u5f55:&quot;)\n    for log in filtered_logs:\n        print(log)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>strptime: &#8220;parse&#8221; (\u89e3\u6790) \u2192 \u4ece\u5b57\u7b26\u4e32\u89e3\u6790\u51fa\u65f6\u95f4\uff0c\u5b57\u7b26\u4e32 \u2192 \u65f6\u95f4\u5bf9\u8c61\u3002 strftime [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[],"class_list":["post-318","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=\/wp\/v2\/posts\/318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=318"}],"version-history":[{"count":1,"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=\/wp\/v2\/posts\/318\/revisions"}],"predecessor-version":[{"id":320,"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=\/wp\/v2\/posts\/318\/revisions\/320"}],"wp:attachment":[{"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xxiaoteng.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}