{"id":87523,"date":"2024-01-26T05:51:14","date_gmt":"2024-01-25T21:51:14","guid":{"rendered":"http:\/\/gulass.cn\/?p=87523"},"modified":"2024-01-26T05:51:14","modified_gmt":"2024-01-25T21:51:14","slug":"python-time-submit","status":"publish","type":"post","link":"https:\/\/gulass.cn\/python-time-submit.html","title":{"rendered":"\u65f6\u95f4\u5904\u7406\u5927\u603b\u7ed3\u4e4bPython"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\npython\u4e2d\u5904\u7406\u65f6\u95f4\u7684\u6a21\u5757\u6709\u4e09\u4e2a\uff0cdatetime, time,calendar\uff0c\u878d\u6c47\u8d2f\u901a\u4e09\u4e2a\u6a21\u5757\uff0c\u624d\u80fd\u968f\u5fc3\u6240\u6b32\u5730\u7528python\u5904\u7406\u65f6\u95f4\u3002\u672c\u6587\u5c31\u662f\u4e3a\u6b64\u800c\u5199\uff0c\u6587\u7ae0\u7740\u91cd\u70b9\u5728\u4e8e\u68b3\u7406\u51fa\u4e09\u4e2a\u6a21\u5757\u7684\u8bbe\u8ba1\u8109\u7edc\uff0c\u4fbf\u4e8e\u5927\u5bb6\u8bb0\u5fc6\u91cc\u9762\u7684api\u3002\u5728\u9700\u8981\u7684\u65f6\u5019\u80fd\u591f\u53bb\u67e5\u627e\u76f8\u5e94\u7684\u65b9\u6cd5\u3002\u4f46\u7531\u4e8ecalendar\u6a21\u5757\u4f7f\u7528\u4e0d\u591a\uff0c\u9650\u4e8e\u7bc7\u5e45\uff0c\u672c\u6587\u6ca1\u6709\u6d89\u53ca\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

1.\u6982\u8ff0<\/strong><\/div>\n

datetime\u6a21\u5757\u4e3b\u8981\u662f\u7528\u6765\u8868\u793a\u65e5\u671f\u7684\uff0c\u5c31\u662f\u6211\u4eec\u5e38\u8bf4\u7684\u5e74\u6708\u65e5\u65f6\u5206\u79d2\uff0ccalendar\u6a21\u5757\u4e3b\u8981\u662f\u7528\u6765\u8868\u793a\u5e74\u6708\u65e5\uff0c\u662f\u661f\u671f\u51e0\u4e4b\u7c7b\u7684\u4fe1\u606f\uff0ctime\u6a21\u5757\u4e3b\u8981\u4fa7\u91cd\u70b9\u5728\u65f6\u5206\u79d2\uff0c\u7c97\u7565\u4ece\u529f\u80fd\u6765\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba4\u4e3a\u4e09\u8005\u662f\u4e00\u4e2a\u4e92\u8865\u7684\u5173\u7cfb\uff0c\u5404\u81ea\u4e13\u6ce8\u4e00\u5757\u3002\u65b9\u4fbf\u7528\u6237\u4f9d\u636e\u4e0d\u540c\u7684\u4f7f\u7528\u76ee\u7684\u9009\u7528\u8d81\u624b\u7684\u6a21\u5757\u3002<\/p>\n

2.\u4ecetime\u6a21\u5757\u8bf4\u8d77<\/strong><\/div>\n

\u4e3a\u4e86\u5b66\u4e60time\u6a21\u5757\uff0c\u6211\u4eec\u9700\u8981\u5148\u77e5\u9053\u51e0\u4e2a\u4e0e\u65f6\u95f4\u76f8\u5173\u7684\u6982\u5ff5\uff1a<\/p>\n

\uff081\uff09epoch<\/strong><\/span><\/div>\n

\u5047\u8bbe\u6211\u4eec\u8981\u5c06\u65f6\u95f4\u8868\u793a\u6210\u6beb\u79d2\u6570\uff0c\u6bd4\u65b9\u8bf41000000\u6beb\u79d2\uff0c\u90a3\u6709\u4e00\u4e2a\u95ee\u9898\u5fc5\u987b\u89e3\u51b3\uff0c\u8fd9\u4e2a1000000\u6beb\u79d2\u7684\u8d77\u70b9\u662f\u4ec0\u4e48\u65f6\u95f4\uff0c\u4e5f\u5c31\u662f\u6211\u4eec\u7684\u65f6\u95f4\u57fa\u51c6\u70b9\u662f\u4ec0\u4e48\u65f6\u95f4\uff1f\u597d\u6bd4\u6211\u8bf4\u4f60\u8eab\u9ad81.8\u7c73\uff0c\u90a3\u8fd9\u4e2a\u8eab\u9ad8\u662f\u6307\u76f8\u5bf9\u4e8e\u4f60\u7ad9\u7acb\u7684\u5730\u9762\u8bf4\u7684\u3002\u8fd9\u4e2a\u65f6\u95f4\u57fa\u51c6\u70b9\u5c31\u662fepoch\uff0c\u5728Unix\u7cfb\u7edf\u4e2d\uff0c\u8fd9\u4e2a\u57fa\u51c6\u70b9\u5c31\u662f1970\u5e741\u67081\u65e50\u70b9\u6574\u90a3\u4e2a\u65f6\u95f4\u70b9\u3002<\/p>\n

\uff082\uff09GMT, UTC<\/strong><\/span><\/div>\n

\u4e0a\u9762\u6211\u4eec\u8bf4epoch\u8868\u793a1970\u5e74\u7684\u8d77\u59cb\u70b9\uff0c\u90a3\u8fd9\u4e2a1970\u5e74\u53c8\u662f\u76f8\u5bf9\u4e8e\u54ea\u4e2a\u57fa\u51c6\u65f6\u95f4\u5462\uff1f\u4e00\u822c\u6765\u8bf4\uff0c\u5c31\u662f\u76f8\u5bf9\u4e8e\u683c\u6797\u5c3c\u6cbb\u65f6\u95f4\uff0c\u4e5f\u53eb\u505aGMT(Greenwich Mean Time)\u65f6\u95f4\uff0c\u8fd8\u53eb\u505aUTC(Coordinated Universal Time)\uff0c\u4e3a\u5565\u4e00\u4e2a\u65f6\u95f4\u57fa\u51c6\u6709\u4e24\u4e2a\u540d\u5b57\uff1f\u5386\u53f2\u4e0a\uff0c\u5148\u6709\u7684GMT,\u540e\u6709\u7684UTC.<\/p>\n

UTC\u662f\u6211\u4eec\u73b0\u5728\u7528\u7684\u65f6\u95f4\u6807\u51c6\uff0cGMT\u662f\u8001\u7684\u65f6\u95f4\u8ba1\u91cf\u6807\u51c6\u3002UTC\u662f\u6839\u636e\u539f\u5b50\u949f\u6765\u8ba1\u7b97\u65f6\u95f4\uff0c\u800cGMT\u662f\u6839\u636e\u5730\u7403\u7684\u81ea\u8f6c\u548c\u516c\u8f6c\u6765\u8ba1\u7b97\u65f6\u95f4\u3002<\/p>\n

\u6240\u4ee5\uff0c\u53ef\u4ee5\u8ba4\u4e3aUTC\u662f\u771f\u6b63\u7684\u57fa\u51c6\u65f6\u95f4\uff0cGMT\u76f8\u5bf9UTC\u7684\u504f\u5dee\u4e3a0\u3002<\/p>\n

\u5728\u5b9e\u9645\u4e2d\uff0c\u6211\u4eec\u7684\u8ba1\u7b97\u673a\u4e2d\u6709\u4e00\u4e2a\u786c\u4ef6\u6a21\u5757RCT\uff0c\u91cc\u9762\u4f1a\u5b9e\u65f6\u8bb0\u5f55UTC \u65f6\u95f4\uff0c\u8be5\u6a21\u5757\u6709\u5355\u72ec\u7684\u7535\u6c60\u4f9b\u7535\uff0c\u5373\u4f7f\u5173\u673a\u4e5f\u4e0d\u5f71\u54cd\u3002<\/p>\n

\u6709\u4e86epoch\u8fd9\u4e2a\u65f6\u95f4\u57fa\u51c6\uff0c\u53c8\u6709\u4e86UTC\u8fd9\u4e2a\u57fa\u51c6\u7684\u57fa\u51c6\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u7cbe\u786e\u5730\u8868\u793a\u4e00\u4e2a\u65f6\u95f4\u4e86\u3002<\/p>\n

\uff083\uff09DST, tzone<\/strong><\/span><\/div>\n

\u5c3d\u7ba1\u6211\u4eec\u5df2\u7ecf\u53ef\u4ee5\u7cbe\u786e\u5730\u8868\u793a\u4e00\u4e2a\u65f6\u95f4\uff0c\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u8fd8\u662f\u8981\u6839\u636e\u5730\u533a\u5b9e\u9645\u60c5\u51b5\u5bf9\u65f6\u95f4\u8fdb\u884c\u4e00\u4e2a\u8c03\u6574\uff0c\u6700\u5e38\u89c1\u7684\u5c31\u662f\u65f6\u533a\uff0ctzone\uff0c\u76f8\u4fe1\u5927\u5bb6\u90fd\u6bd4\u8f83\u719f\u6089\u3002<\/p>\n

\u6b64\u65f6\uff0c\u5f53\u6211\u4eec\u8bf45\u70b95\u5206\u8fd9\u4e2a\u65f6\u95f4\u65f6\uff0c\u8fd8\u9700\u52a0\u4e0a\u662f\u54ea\u4e2a\u65f6\u533a\u76845\u70b95\u5206\u624d\u80fd\u7cbe\u786e\u8bf4\u660e\u4e00\u4e2a\u65f6\u95f4\u3002<\/p>\n

\u53e6\u5916\u4e00\u4e2a\u5bf9\u65f6\u95f4\u505a\u51fa\u8c03\u6574\u7684\u5c31\u662fDST.<\/p>\n

DST \u5168\u79f0\u662fDaylight Saving Time\uff0c\u662f\u8bf4\uff0c\u4e3a\u4e86\u5145\u5206\u5229\u7528\u65e5\u5149\uff0c\u51cf\u5c11\u7528\u7535\uff0c\u4eba\u4e3a\u5730\u5bf9\u65f6\u95f4\u505a\u51fa\u4e00\u4e2a\u8c03\u6574\uff0c\u8fd9\u53d6\u51b3\u4e8e\u4e0d\u540c\u56fd\u5bb6\u548c\u5730\u533a\u7684\u653f\u7b56\u6cd5\u89c4\u3002\u6bd4\u5982\u8bf4\uff0c\u5047\u8bbe\u4f60\u51ac\u59297\u70b9\u5929\u4eae\u8d77\u5e8a\uff0c\u4f46\u590f\u59296\u70b9\u5929\u4eae\uff0c\u90a3\u4e48\u5728\u590f\u5929\u5230\u6765\u65f6\u4eba\u4e3a\u5c06\u65f6\u95f4\u52a01\u4e2a\u5c0f\u65f6\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u8ba9\u4f60\u8fd8\u662f\u89c9\u5f977\u70b9\u8d77\u5e8a\uff0c\u4f46\u5b9e\u9645\u4e0a\u662f\u63d0\u524d\u4e00\u4e2a\u5c0f\u65f6\u4e86\u3002<\/p>\n

\u90a3\u4e48\uff0c\u597d\u5947\u7684\u6211\u4eec\uff0c\u4e00\u5b9a\u8981\u95ee\u4e00\u95ee\uff0cpython\u662f\u5982\u4f55\u77e5\u9053tzone\u548cDST\u8fd9\u4e24\u4e2a\u7684\u503c\u5462\uff1f\u7b54\u6848\u662f\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u3002<\/p>\n

\u8fd9\u91cc\u6211\u4eec\u53ea\u4ee5linux\u4e3a\u4f8b\u6765\u8bf4\u660e\u4e00\u4e0b\u3002<\/p>\n

\u5728linux\u4e2d\u6709TZ\u73af\u5883\u53d8\u91cf\uff0c\u5176\u503c\u7c7b\u4f3c\u8fd9\u6837:<\/p>\n

CST+08EDT,M4.1.0,M10.5.0\uff0c\u8fd9\u4e2a\u5b57\u7b26\u4e32\u53ef\u4ee5\u505a\u5982\u4e0b\u89e3\u8bfb\uff0c\u7528\u7a7a\u683c\u5206\u5f00\u4ed6\u4eec\uff0c\u5206\u6210\u4e09\u90e8\u5206<\/p>\n

CST+08 EDT, M4.1.0,M10.5.0<\/p>\n

\u7b2c\u4e00\u90e8\u5206\u4e2d\u7684CST\u8868\u793a\u65f6\u533a\u7684\u540d\u5b57\uff0c\u5373China Standard Time\uff0c\u4e5f\u5c31\u662f\u6211\u4eec\u8bf4\u7684\u5317\u4eac\u65f6\u95f4\uff0c+8\u8868\u793a\u5317\u4eac\u65f6\u95f4\u52a0\u4e0a8\u5c0f\u65f6\u5c31\u662fUTC\u65f6\u95f4<\/p>\n

\u7b2c\u4e8c\u90e8\u5206EDT\u8868\u793aDST\u7684\u540d\u5b57\uff0c\u6211\u4eec\u8bf4DST\u662f\u56e0\u5404\u4e2a\u56fd\u5bb6\u5730\u533a\u7684\u653f\u7b56\u6cd5\u89c4\u4e0d\u540c\u800c\u4e0d\u540c\u7684\uff0cEDT\u540e\u9762\u4e5f\u53ef\u4ee5\u50cfCST\u540e\u9762\u4e00\u6837\u52a0\u4e00\u4e2a\u65f6\u95f4\u8c03\u6574\u503c\uff0c\u4f46\u7531\u4e8e\u6211\u4eec\u56fd\u5185\u53ea\u572886\u5e74\u523092\u5e74\u5b9e\u884c\u8fc7\u4e00\u6bb5\u65f6\u95f4DST\uff0c\u73b0\u5728\u5df2\u7ecf\u5e9f\u6b62\uff0c\u6240\u4ee5\u540e\u9762\u4e0d\u7528\u52a0\u8c03\u6574\u65f6\u95f4\u3002<\/p>\n

\u7b2c\u4e09\u90e8\u5206\u8868\u793a\u7684\u662f\u5b9e\u884cDST\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u65f6\u95f4\uff0c\u6211\u4eec\u5c31\u4e0d\u7ec6\u89e3\u8bfb\u4e86\u3002<\/p>\n

\uff084\uff09\u65f6\u95f4\u7684\u8868\u793a\uff0c\u83b7\u53d6\uff0c\u8f6c\u6362<\/strong><\/span><\/div>\n

time\u6a21\u5757\u4e2d\u83b7\u53d6\u65f6\u95f4\u7684\u57fa\u672c\u65b9\u6cd5\u662f<\/p>\n

t = time.time()<\/pre>\n

\u5b83\u8fd4\u56de\u7684\u662f\u4eceepoch\u5230\u73b0\u5728\u7684\u79d2\u6570(\u7528\u6d6e\u70b9\u6570\u8868\u793a)\uff0c\u7528\u7684\u662fUTC\u65f6\u95f4\u3002<\/p>\n

\u6211\u4eec\u81ea\u7136\u800c\u7136\u5730\u60f3\u628a\u8fd9\u4e2a\u79d2\u6570\u8f6c\u4e3a\u5e74\u6708\u65e5\u65f6\u5206\u79d2\u7684\u5f62\u5f0f\uff0c\u800c\u8fd9\u79cd\u8f6c\u6362\u53c8\u5206\u4e24\u79cd\uff0c\u4e00\u79cd\u8fd8\u662f\u7528UTC\u65f6\u95f4\uff0c\u4e00\u79cd\u7528\u6211\u4eec\u6240\u5728\u65f6\u533a\u8fdb\u884c\u8c03\u6574\u540e\u7684\u65f6\u95f4\u3002<\/p>\n

time\u6a21\u5757\u7ed9\u6211\u4eec\u63d0\u4f9b\u4e86\u4e24\u4e2a\u65b9\u6cd5\uff0c<\/p>\n

time. gmtime(t)\r\ntime.localtime(t)\r\n<\/pre>\n

\u4e8c\u8005\u90fd\u8fd4\u56de\u4e00\u4e2a\u7c7bstruct_time\u7684\u5b9e\u4f8b\uff0c\u8be5\u5b9e\u4f8b\u5177\u6709\u5982\u4e0b\u5c5e\u6027:<\/p>\n

\"\"<\/p>\n

\u76f8\u6bd4\u7528\u79d2\u6570\u8868\u793a\u7684\u65f6\u95f4\uff0c\u8fd9\u6837\u7684\u8868\u793a\u66f4\u9002\u5408\u6211\u4eec\u7406\u89e3\u3002<\/p>\n

\u8fd9\u4e24\u4e2a\u51fd\u6570\u5982\u679c\u8c03\u7528\u65f6\u4e0d\u4f20\u53c2\u6570\uff0c\u5b83\u4eec\u5185\u90e8\u4f1a\u8c03\u7528time.time()\uff0c\u5e76\u7528\u8fd4\u56de\u7684\u79d2\u6570\u505a\u8f6c\u6362\u3002<\/p>\n

\u76f8\u53cd\u7684\uff0cpython\u540c\u6837\u63d0\u4f9b\u4e86\u5c06\u8fd9\u4e24\u79cdstruct_time\u8f6c\u4e3a\u79d2\u6570\u7684\u65b9\u6cd5\u3002<\/p>\n

calendar.timegm()\u65b9\u6cd5\u7528\u6765\u628aUTC\u7684struct_time(gmtime\u7684\u8fd4\u56de\u5bf9\u8c61)\u8f6c\u4e3a\u4eceepoch\u5f00\u59cb\u7684\u79d2\u6570<\/p>\n

time.mktime()\u7528\u6765\u628a\u7528\u65f6\u533a\u8c03\u6574\u8fc7\u7684struct_time(\u5373localtime\u7684\u8fd4\u56de\u5bf9\u8c61)\u5bf9\u8c61\u8f6c\u4e3a\u4eceepoch\u5f00\u59cb\u7684\u79d2\u6570<\/p>\n

\u4e5f\u5c31\u662f\u8bf4mktime\u65b9\u6cd5\u4f1a\u5148\u627e\u5230\u7cfb\u7edf\u4e2d\u7684\u65f6\u533a\u548cDST\u4fe1\u606f\uff0c\u5e76\u5229\u7528\u8fd9\u4e2a\u4fe1\u606f\u5bf9struct_time\u8fdb\u884c\u8c03\u6574\u540e\u518d\u6362\u7b97\u6210\u79d2\u6570\u3002<\/p>\n

\u53e6\u4e00\u79cd\u5e38\u89c1\u7684\u9700\u6c42\u662f\u5728\u65f6\u95f4\u548c\u8868\u793a\u65f6\u95f4\u7684\u5b57\u7b26\u4e32\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n

time\u6a21\u5757\u4e2d\u7684strftime\u548cstrptime\u5c31\u662f\u505a\u8fd9\u4e2a\u7528\u7684\u3002<\/p>\n

\u770b\u540d\u5b57\u5927\u5bb6\u5c31\u5e94\u8be5\u77e5\u9053\u5b83\u4eec\u7684\u542b\u4e49\uff0c<\/p>\n

strftime \u5373 string format time\uff0c\u7528\u6765\u5c06\u65f6\u95f4\u683c\u5f0f\u5316\u6210\u5b57\u7b26\u4e32<\/p>\n

strptime \u5373string parse time\uff0c\u7528\u6765\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u6210\u65f6\u95f4\u3002<\/p>\n

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u91cc\u7684\u65f6\u95f4\u90fd\u662fstruct_time\u5bf9\u8c61\u3002<\/p>\n

\u5173\u4e8e\u600e\u4e48\u683c\u5f0f\u5316\u65f6\u95f4\uff0c\u662f\u5f88\u7b80\u5355\u7684\u77e5\u8bc6\uff0c\u8fd9\u91cc\u5c31\u501f\u7528\u5b98\u7f51\u6587\u6863\u7684\u5185\u5bb9\u4e86\u3002<\/p>\n

\"\"<\/p>\n

\u9664\u4e86\u8fd9\u4e24\u4e2a\u51fd\u6570\uff0ctime\u6a21\u5757\u4e2d\u8fd8\u63d0\u4f9b\u4e86\u4e24\u4e2a\u7b80\u4fbf\u65b9\u6cd5\uff0c\u6765\u5e2e\u52a9\u5c06\u65f6\u95f4\u8f6c\u4e3a\u5b57\u7b26\u4e32<\/p>\n

asctime\u7528\u6765\u5c06\u4e00\u4e2astruct_time\u5bf9\u8c61\u8f6c\u4e3a\u6807\u51c624\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\uff0c\u5982\u4e0b\u6240\u793a:<\/p>\n

Sun Jun 20 23:21:05 1993<\/p>\n

ctime\u65b9\u6cd5\u4e0easctime\u4f5c\u7528\u76f8\u540c\uff0c\u53ea\u4e0d\u8fc7\u5b83\u63a5\u6536\u7684\u662f\u79d2\u6570\uff0c\u5728\u5185\u90e8\uff0c\u4f1a\u5148\u628a\u79d2\u6570\u901a\u8fc7localtime\u8f6c\u4e3astruct_time\uff0c\u518d\u5f80\u540e\u5c31\u4e0easctime\u4e00\u6837\u4e86\u3002<\/p>\n

\u4ee5\u4e0a\u5c31\u662ftime\u6a21\u5757\u7684\u6838\u5fc3\u5185\u5bb9\uff0c\u6211\u5c1d\u8bd5\u7528\u4e00\u4e2a\u53e3\u8bc0\u5e2e\u52a9\u8bb0\u5fc6\u8fd9\u4e9bAPI<\/p>\n

time\u70b9time\u5f97\u79d2\u6570\r\n\r\n\u4f20\u5165gm, local time\u5f97struct_time\r\n\r\n\u8981\u60f3\u53d8\u56de\u539f\u79d2\u6570\r\n\r\n\u4f60\u5f97\u4f20\u56decalendar.timegm\u548ctime. mktime\r\n\r\nstring f\u548cstring p\r\n\r\n\u683c\u5f0f\u5316\u65f6\u95f4\u9760\u54e5\u4fe9\r\n\r\n\u4f60\u8981\u8fd8\u662f\u5acc\u8d39\u4e8b\r\n\r\nasctime ,ctime\u6765\u52a9\u529b\r\n\r\n\u4e13\u95e8\u5e2e\u4f60\u8f6c\u5b57\u7b26\u4e32\r\n\r\n\u524d\u8005\u63a5\u6536struct_time\r\n\r\n\u540e\u8005\u4e13\u95e8\u5904\u7406\u79d2\u6570\r\n\r\n\u5206\u5de5\u5408\u4f5c\u4e0d\u8d39\u529b\r\n\r\n\u5b66\u597dtime\u6a21\u5757\u57fa\u672c\u529f\r\n\r\n\u505a\u4e2a\u65f6\u95f4\u7684\u660e\u767d\u4eba\uff01<\/pre>\n

\u4e0b\u9762\uff0c\u6211\u4eec\u8981\u5f00\u59cb\u5b66\u4e60datetime\u6a21\u5757\u3002<\/p>\n

3.datetime\u6a21\u5757<\/strong><\/div>\n
\uff081\uff09\u6982\u89c8<\/strong><\/span><\/div>\n

time\u6a21\u5757\u89e3\u51b3\u4e86\u65f6\u95f4\u7684\u83b7\u53d6\u548c\u8868\u793a\uff0cdatetime\u6a21\u5757\u5219\u8fdb\u4e00\u6b65\u89e3\u51b3\u4e86\u5feb\u901f\u83b7\u53d6\u5e76\u64cd\u4f5c\u65f6\u95f4\u4e2d\u7684\u5e74\u6708\u65e5\u65f6\u5206\u79d2\u4fe1\u606f\u7684\u80fd\u529b\u3002<\/p>\n

\u7b80\u5355\u8bf4\uff0c\u8be5\u6a21\u5757\u6838\u5fc3\u7684\u7c7b\u5c31\u4e09\u4e2a\uff0cdate\u7c7b\u8868\u793a\u5e74\u6708\u65e5\uff0ctime\u7c7b\u8868\u793a\u65f6\u5206\u79d2\u6beb\u79d2\uff0c\u8fd9\u91cc\u4e0d\u8981\u548ctime\u6a21\u5757\u641e\u6df7\u6dc6\u4e86\u3002\u4e00\u53e5\u987a\u53e3\u6e9c\u53ef\u4ee5\u5e2e\u52a9\u8bb0\u6e05\u8fd9\u4e2a\u60c5\u51b5:<\/p>\n

time\u91cc\u9762\u6ca1time<\/p>\n

\u85cf\u5728datetime\u91cc<\/p>\n

\u7f16\u7684\u662f\u4e0d\u662f\u4e0d\u548b\u5730\uff1f\u55ef\uff0c\u6211\u4e5f\u8fd9\u4e48\u89c9\u5f97\u3002<\/p>\n

datetime\u7c7b\u5c31\u662fdate\u548ctime\u7684\u7ec4\u5408\u3002<\/p>\n

\u6709\u4e00\u70b9\u9700\u8981\u63d0\u524d\u8bf4\u660e\u4e00\u4e0b\uff0ctime\u7c7b\u548cdatetime\u7c7b\u90fd\u6709\u4e00\u4e2a\u5c5e\u6027\uff0c\u5b83\u7684\u503c\u662f\u4e00\u4e2atzinfo\u5bf9\u8c61\uff0c\u91cc\u9762\u5305\u542b\u4e86\u8be5time\u6216\u8005datetime\u7684\u65f6\u533a\u4fe1\u606f\uff0c\u4e00\u822c\u79f0\u8fd9\u4e2atime\u6216\u8005datetime\u5bf9\u8c61\u662faware\u7684\uff0c\u5b83\u80fd\u591f\u51c6\u786e\u6362\u7b97\u6210\u81eaepoch\u5f00\u59cb\u7684\u79d2\u6570\u3002<\/p>\n

\u5982\u679c\u8be5\u5c5e\u6027\u8bbe\u7f6e\u4e3aNone\uff0c\u90a3\u4e48\uff0c\u8fd9\u65f6\u7684time\u5bf9\u8c61\u6216\u8005datetime\u5bf9\u8c61\u5c31\u6ca1\u6709\u65f6\u533a\u4fe1\u606f\uff0c\u5177\u4f53\u5b83\u8868\u793a\u7684\u662flocal time\u8fd8\u662futc time\uff0c\u9700\u8981\u6211\u4eec\u81ea\u5df1\u5728\u7a0b\u5e8f\u4e2d\u53bb\u51b3\u5b9a\u3002<\/p>\n

\u8fd9\u91cc\u6211\u4eec\u6240\u8bf4\u7684local time\u662f\u6307\u6211\u4eec\u6240\u5728\u65f6\u533a\u7684\u65f6\u95f4\uff0c utc time\u6307\u7684\u5c31\u662f\u56fd\u9645\u6807\u51c6\u65f6\u95f4\uff0c\u4e5f\u5c31\u662f\u683c\u6797\u5c3c\u6cbb\u65f6\u95f4\u3002\u4e0b\u6587\u540c\u3002<\/p>\n

\u8bf7\u8bb0\u4f4f\u4e00\u70b9\uff0cdate\u4e2d\u662f\u6ca1\u6709\u65f6\u533a\u4fe1\u606f\u7684\u3002<\/p>\n

\uff082\uff09\u4ece\u521b\u5efadatetime\u5f00\u59cb<\/strong><\/span><\/div>\n

\u521b\u5efadatetime\u5bf9\u8c61\uff0c\u6211\u6700\u5e38\u7528\u7684\u529e\u6cd5\u5982\u4e0b<\/p>\n

dt=datetime.datetime.fromtimestamp(time.time())<\/p>\n

\u4ee5\u4e0a\uff0ctime.time()\u83b7\u5f97\u81eaepoch\u5f00\u59cb\u7684\u79d2\u6570\uff0cfromtimestamp\u65b9\u6cd5\u4f1a\u5c06\u8fd9\u4e2a\u79d2\u6570\u8f6c\u53d8\u6210\u4e00\u4e2adatetime\u5bf9\u8c61\u3002<\/p>\n

\u8fd9\u91cc\u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u8fd9\u4e2adatetime\u5bf9\u8c61\u7a76\u7adf\u662futc\u7684\u8fd8\u662flocal\u7684\uff1f<\/p>\n

\u7b54\u6848\u662flocal\u7684\uff0c\u8fd9\u662f\u8be5\u65b9\u6cd5\u7684\u9ed8\u8ba4\u884c\u4e3a\u3002\u5982\u679c\u4f60\u5728fromtimestamp\u65b9\u6cd5\u4e2d\u4f20\u5165\u4e00\u4e2a\u8868\u793a\u65f6\u533a\u7684\u53c2\u6570\uff0c\u5373tzinfo\u5bf9\u8c61\uff0c\u5c31\u4f1a\u6309\u4f20\u5165\u7684\u65f6\u533a\u4fe1\u606f\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n

\u83b7\u5f97\u8868\u793a\u5f53\u524dlocal\u65f6\u95f4\u7684datetime\u5bf9\u8c61\uff0c\u8fd8\u6709\u4e24\u4e2a\u7b80\u4fbf\u65b9\u6cd5<\/p>\n

datetime. datetime. now()\r\ndatetime. datetime. today()\r\n<\/pre>\n

\u4ee5\u4e0a\u6211\u4eec\u5f97\u5230\u7684\u90fd\u662flocal\u7684datetime\u5bf9\u8c61\uff0c\u5982\u4f55\u83b7\u5f97utc\u7684datetime\u5bf9\u8c61\u5462\uff1f\u6709\u4e24\u4e2a\u529e\u6cd5<\/p>\n

datetime. datetime. utcfromtimestamp()\r\ndatetime. datetime. utcnow()\r\n<\/pre>\n

\u6211\u4eec\u8fd8\u53ef\u4ee5\u4ece\u5b57\u7b26\u4e32\u4e2d\u521b\u5efadatetime\u5bf9\u8c61\uff0c<\/p>\n

\u5176\u5185\u90e8\u8fd8\u662f\u5148\u8c03\u7528\u7684time\u6a21\u5757\u4e2d\u7684striptime\u65b9\u6cd5\uff0c\u83b7\u53d6struct_time\u5bf9\u8c61\uff0c\u518d\u5229\u7528struct_time\u5bf9\u8c61\u4e2d\u7684\u5e74\u6708\u65e5\u65f6\u5206\u79d2\u4fe1\u606f\u6784\u5efadatetime\u5bf9\u8c61\u3002<\/p>\n

\u540c\u6837\u7684\uff0cdatetime\u7c7b\u4e5f\u63d0\u4f9b\u4e86strftime()\uff0casctime()\uff0cctime()\u65b9\u6cd5\uff0c\u76f8\u4fe1\u4e0d\u8bf4\u4f60\u4e5f\u77e5\u9053\u662f\u505a\u4ec0\u4e48\u7684\u4e86\u3002<\/p>\n

datetime\u7c7b\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e2acombine\u65b9\u6cd5\uff0c\u7528\u6765\u5c06\u4e00\u4e2adate\u5bf9\u8c61\u548c\u4e00\u4e2atime\u5bf9\u8c61\u7ec4\u5408\u6210\u4e00\u4e2adatetime\u5bf9\u8c61\u3002<\/p>\n

\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cdatetime\u6a21\u5757\u4e2d\u51fa\u73b0timestamp\u65f6\uff0c\u4e00\u822c\u53ef\u5c06\u5176\u7406\u89e3\u6210time.time()\u8fd4\u56de\u7684\u79d2\u6570<\/p>\n

\uff083\uff09date\u548ctime\u7684\u521b\u5efa<\/strong><\/span><\/div>\n

date\u5bf9\u8c61\u7684\u521b\u5efa\u548cdatetime\u975e\u5e38\u76f8\u4f3c\uff0c<\/p>\n

datetime. date. today()<\/p>\n

datetime.date.fromtimestamp()\u90fd\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2adate\u5bf9\u8c61\u3002<\/p>\n

\u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u901a\u8fc7\u6784\u9020\u65b9\u6cd5\u4f20\u5165\u5e74\u6708\u65e5\u6765\u521b\u5efadate\u5bf9\u8c61\u3002<\/p>\n

\u76f8\u6bd4\u4e4b\u4e0b\uff0ctime\u5bf9\u8c61\u7684\u521b\u5efa\u5c31\u5f88\u6709\u9650\uff0c\u53ea\u80fd\u901a\u8fc7<\/p>\n

datetime.time([hour[, minute[, second[, microsecond[, tzinfo]]]]])<\/p>\n

\u8fd9\u4e2a\u65b9\u6cd5\u521b\u5efa\u3002<\/p>\n

\uff084\uff09\u4ee5\u4e0a\u4e09\u4e2a\u5bf9\u8c61\u7684\u64cd\u4f5c\u548ctimedelta\u7c7b<\/strong><\/span><\/div>\n

\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u6211\u4eec\u6709\u4e00\u5927\u5757\u9700\u6c42\u5c31\u662f\u5bf9\u65e5\u671f\u8fdb\u884c\u6bd4\u8f83\u548c\u52a0\u51cf\u8fd0\u7b97\u3002\u5f97\u76ca\u4e8epython\u7684\u64cd\u4f5c\u7b26\u91cd\u8f7d\u80fd\u529b\uff0cpython\u4e2d\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9<\/p>\n

date\u5bf9\u8c61\u4e4b\u95f4\uff0c\u6216\u8005datetime\u5bf9\u8c61\u4e4b\u95f4\u8fdb\u884c\u5c0f\u4e8e(<)\u6bd4\u8f83\u548c\u51cf\u6cd5(-)\u64cd\u4f5c\u3002<\/p>\n

\u6ce8\u610f\uff0c\u8fd9\u91cc\u4ec5\u9650\u4e8e\u540c\u7c7b\u5bf9\u8c61\u4e4b\u95f4\uff0c\u800c\u4e14\uff0c\u4e0d\u5305\u62ectime\u5bf9\u8c61\u4e4b\u95f4\u3002<\/p>\n

\u4e24\u4e2adate\u5bf9\u8c61\u4f5c\u51cf\uff0c\u6216\u8005\u4e24\u4e2adatetime\u5bf9\u8c61\u4e4b\u95f4\u4f5c\u51cf\uff0c\u5dee\u503c\u7528\u4e00\u4e2atimedelta\u5bf9\u8c61\u8868\u793a\u3002<\/p>\n

\u540c\u7406\uff0c\u4e00\u4e2adate \u5bf9\u8c61\u6216\u8005datetime\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u52a0\u6216\u8005\u51cf\u4e00\u4e2atimedelta\u5bf9\u8c61\u3002<\/p>\n

\u4e00\u4e2atimedelta\u5bf9\u8c61\u542b\u6709\u4e09\u4e2a\u5c5e\u6027:days,seconds, microseconds,days\u5c5e\u6027\u53ef\u4ee5\u53d6\u8d1f\u503c\uff0c\u53e6\u5916\u4e24\u4e2a\u5c5e\u6027\u90fd\u53ea\u80fd\u662f\u6b63\u503c\u3002<\/p>\n

\u4f60\u53ef\u4ee5\u7528total_seconds()\u65b9\u6cd5\u83b7\u5f97\u4e00\u4e2atimedelta\u5bf9\u8c61\u7684\u79d2\u6570\u8868\u793a\u3002<\/p>\n

\u4e24\u4e2atimedelta\u5bf9\u8c61\u4e4b\u95f4\u53ef\u52a0\uff0c\u53ef\u51cf\uff0c\u4f46\u4e0d\u80fd\u505a\u5927\u5c0f\u6bd4\u8f83\uff0c\u56e0\u4e3a\u8fd9\u6837\u6ca1\u4ec0\u4e48\u610f\u4e49\u3002<\/p>\n

\u4e00\u4e2atimedelta\u5bf9\u8c61\u8fd8\u53ef\u4ee5\u4e0e\u6574\u6570\u76f8\u4e58\uff0c\u6216\u901a\u8fc7\/\/\u64cd\u4f5c\u4e0e\u4e00\u4e2a\u6574\u6570\u76f8\u9664\u3002<\/p>\n

\u8fd8\u53ef\u4ee5\u53d6\u53cd\uff0c\u6216\u8005\u7528abs\u51fd\u6570\u83b7\u5f97\u7edd\u5bf9\u503c<\/p>\n

4.\u65e0\u603b\u7ed3\uff0c\u4e0d\u8fdb\u6b65<\/strong><\/div>\n

\u672c\u6587\u7684\u76ee\u7684\u4e0d\u5728\u4e8e\u8be6\u7ec6\u8bf4\u660epython\u5904\u7406\u65f6\u95f4\u65e5\u671f\u7684api\u5982\u4f55\u4f7f\u7528\uff0c\u800c\u662f\u60f3\u901a\u8fc7\u4e00\u4e2a\u6982\u89c8\u7684\u5f62\u5f0f\uff0c\u8ba9\u5927\u5bb6\u6293\u4f4ftime\u548cdatetime\u6a21\u5757\u7684\u8bbe\u8ba1\u7ed3\u6784\uff0c\u4ece\u800c\u80fd\u591f\u6e05\u695a\u8fd9\u4e9b\u6a21\u5757\u63d0\u4f9b\u4e86\u54ea\u4e9b\u80fd\u529b\uff0c\u5728\u9700\u8981\u7684\u65f6\u5019\u80fd\u591f\u60f3\u8d77\u6765\u53bb\u7528\uff0c\u81f3\u4e8e\u67e5\u8be6\u7ec6\u7684api\uff0c\u5e94\u8be5\u662f\u53ef\u4ee5\u8f7b\u677e\u89e3\u51b3\u7684\u3002<\/p>\n

\n

\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.yunweipai.com\/archives\/23602.html<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1ahttp:\/\/gulass.cn\/python-time-submit.html<\/a>\u7f16\u8f91\uff1a\u674e\u5e05\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\u5bfc\u8bfb python\u4e2d\u5904\u7406\u65f6\u95f4\u7684\u6a21\u5757\u6709\u4e09\u4e2a\uff0cdatetime, time,calendar\uff0c\u878d\u6c47\u8d2f\u901a\u4e09\u4e2a\u6a21\u5757\uff0c\u624d […]<\/p>\n","protected":false},"author":63,"featured_media":87541,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-87523","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/87523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=87523"}],"version-history":[{"count":11,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/87523\/revisions"}],"predecessor-version":[{"id":87615,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/87523\/revisions\/87615"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/87541"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=87523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=87523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=87523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}