{"id":1037,"date":"2023-03-24T09:47:07","date_gmt":"2023-03-24T01:47:07","guid":{"rendered":""},"modified":"2023-03-24T09:47:07","modified_gmt":"2023-03-24T01:47:07","slug":"Python \u65e5\u671f\u4e0e\u65f6\u95f4","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1037.html","title":{"rendered":"Python \u65e5\u671f\u4e0e\u65f6\u95f4"},"content":{"rendered":"
\n
\u641e\u61c2Python\u65e5\u671f\u4e0e\u65f6\u95f4\u7684\u683c\u5f0f\u5316\u548c\u8f6c\u6362\u7b49\u64cd\u4f5c<\/span>\n <\/div>\n %y \u4e24\u4f4d\u6570\u7684\u5e74\u4efd\u8868\u793a\uff0800-99\uff09<\/span> import<\/span> time
ticks = time.time()<\/span>
print(\"\u5f53\u524d\u65f6\u95f4\u6233\u4e3a:\", ticks<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u5f53\u524d\u65f6\u95f4\u6233\u4e3a: 1594644577.9761147<\/span><\/code><\/pre>\n<\/p><\/div>\n
\u65f6\u95f4\u5143\u7ec4<\/h2>\n
\n\n
\n \u5b57\u6bb5<\/td>\n \u503c<\/td>\n<\/tr>\n \n 4\u4f4d\u6570\u5e74<\/td>\n 2008<\/td>\n<\/tr>\n \n \u6708 <\/td>\n 1 \u5230 12<\/td>\n<\/tr>\n \n \u65e5 <\/td>\n 1\u523031<\/td>\n<\/tr>\n \n \u5c0f\u65f6 <\/td>\n 0\u523023<\/td>\n<\/tr>\n \n \u5206\u949f <\/td>\n 0\u523059<\/td>\n<\/tr>\n \n \u79d2 <\/td>\n 0\u523061 (60\u621661 \u662f\u95f0\u79d2)<\/td>\n<\/tr>\n \n \u4e00\u5468\u7684\u7b2c\u51e0\u65e5 <\/td>\n 0\u52306 (0\u662f\u5468\u4e00)<\/td>\n<\/tr>\n \n \u4e00\u5e74\u7684\u7b2c\u51e0\u65e5 <\/td>\n 1\u5230366 (\u5112\u7565\u5386)<\/td>\n<\/tr>\n \n \u590f\u4ee4\u65f6 <\/td>\n -1, 0, 1, -1\u662f\u51b3\u5b9a\u662f\u5426\u4e3a\u590f\u4ee4\u65f6\u7684\u65d7\u5e1c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n \n\n
\n \u5c5e\u6027<\/td>\n \u503c<\/td>\n<\/tr>\n \n tm_year <\/td>\n 2008<\/td>\n<\/tr>\n \n tm_mon<\/td>\n 1 \u5230 12<\/td>\n<\/tr>\n \n tm_mday <\/td>\n 1 \u5230 31<\/td>\n<\/tr>\n \n tm_hour <\/td>\n 0 \u5230 23<\/td>\n<\/tr>\n \n tm_min <\/td>\n 0 \u5230 59<\/td>\n<\/tr>\n \n tm_sec <\/td>\n 0 \u5230 61 (60\u621661 \u662f\u95f0\u79d2)<\/td>\n<\/tr>\n \n tm_wday <\/td>\n 0\u52306 (0\u662f\u5468\u4e00)<\/td>\n<\/tr>\n \n tm_yday <\/td>\n \u4e00\u5e74\u4e2d\u7684\u7b2c\u51e0\u5929\uff0c1 \u5230 366<\/td>\n<\/tr>\n \n tm_isdst <\/td>\n \u662f\u5426\u4e3a\u590f\u4ee4\u65f6\uff0c\u503c\u6709\uff1a1(\u590f\u4ee4\u65f6)\u30010(\u4e0d\u662f\u590f\u4ee4\u65f6)\u3001-1(\u672a\u77e5)\uff0c\u9ed8\u8ba4 -1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n \u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/h2>\n
import<\/span> time
localtime = time.localtime(time.time())<\/span>
print(\"\u672c\u5730\u65f6\u95f4\u4e3a :\", localtime<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u672c\u5730\u65f6\u95f4\u4e3a : time.struct_time(tm_year=2020, tm_mon=7, tm_mday=13, tm_hour=13, tm_min=0, tm_sec=37, tm_wday=0, tm_yday=195, tm_isdst=0)<\/span><\/code><\/pre>\n<\/p><\/div>\n
\u83b7\u53d6\u683c\u5f0f\u5316\u7684\u65f6\u95f4<\/h2>\n
import<\/span> time
localtime = time.asctime(time.localtime(time.time()))<\/span>
print(\"\u672c\u5730\u65f6\u95f4\u4e3a :\", localtime<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u672c\u5730\u65f6\u95f4\u4e3a : Mon Jul 13 13:02:03 2020<\/span><\/code><\/pre>\n<\/p><\/div>\n
\u683c\u5f0f\u5316\u65e5\u671f<\/h2>\n
import<\/span> time
# \u683c\u5f0f\u5316\u62102020-07-13 13:06:04\u5f62\u5f0f<\/span>
print(time.strftime(\"%Y-%m-%d %H:%M:%S\", time.localtime())<\/span>)
# \u683c\u5f0f\u5316\u6210Mon Jul 13 13:06:04 2020\u5f62\u5f0f<\/span>
print(time.strftime(\"%a %b %d %H:%M:%S %Y\", time.localtime())<\/span>)
# \u5c06\u683c\u5f0f\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233<\/span>
a = \"Mon Jul 13 13:06:04 2020\"
print(time.mktime(time.strptime(a,\"%a %b %d %H:%M:%S %Y\"))<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n2020-07-13 13:06:53
Mon Jul 13 13:06:53 2020
1594645564.0<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n %Y \u56db\u4f4d\u6570\u7684\u5e74\u4efd\u8868\u793a\uff08000-9999\uff09<\/span>
\n %m \u6708\u4efd\uff0801-12\uff09<\/span>
\n %d \u6708\u5185\u4e2d\u7684\u4e00\u5929\uff080-31\uff09<\/span>
\n %H 24\u5c0f\u65f6\u5236\u5c0f\u65f6\u6570\uff080-23\uff09<\/span>
\n %I 12\u5c0f\u65f6\u5236\u5c0f\u65f6\u6570\uff0801-12\uff09<\/span>
\n %M \u5206\u949f\u6570\uff0800=59\uff09<\/span>
\n %S \u79d2\uff0800-59\uff09<\/span>
\n %a \u672c\u5730\u7b80\u5316\u661f\u671f\u540d\u79f0<\/span>
\n %A \u672c\u5730\u5b8c\u6574\u661f\u671f\u540d\u79f0<\/span>
\n %b \u672c\u5730\u7b80\u5316\u7684\u6708\u4efd\u540d\u79f0<\/span>
\n %B \u672c\u5730\u5b8c\u6574\u7684\u6708\u4efd\u540d\u79f0<\/span>
\n %c \u672c\u5730\u76f8\u5e94\u7684\u65e5\u671f\u8868\u793a\u548c\u65f6\u95f4\u8868\u793a<\/span>
\n %j \u5e74\u5185\u7684\u4e00\u5929\uff08001-366\uff09<\/span>
\n %p \u672c\u5730A.M.\u6216P.M.\u7684\u7b49\u4ef7\u7b26<\/span>
\n %U \u4e00\u5e74\u4e2d\u7684\u661f\u671f\u6570\uff0800-53\uff09\u661f\u671f\u5929\u4e3a\u661f\u671f\u7684\u5f00\u59cb<\/span>
\n %w \u661f\u671f\uff080-6\uff09\uff0c\u661f\u671f\u5929\u4e3a\u661f\u671f\u7684\u5f00\u59cb<\/span>
\n %W \u4e00\u5e74\u4e2d\u7684\u661f\u671f\u6570\uff0800-53\uff09\u661f\u671f\u4e00\u4e3a\u661f\u671f\u7684\u5f00\u59cb<\/span>
\n %x \u672c\u5730\u76f8\u5e94\u7684\u65e5\u671f\u8868\u793a<\/span>
\n %X \u672c\u5730\u76f8\u5e94\u7684\u65f6\u95f4\u8868\u793a<\/span>
\n %Z \u5f53\u524d\u65f6\u533a\u7684\u540d\u79f0<\/span>
\n %% %\u53f7\u672c\u8eab<\/span> <\/p>\n\u83b7\u53d6\u65e5\u5386<\/h2>\n
import<\/span> calendar
cal = calendar.month(2020, 7<\/span>)
print(\"\u8f93\u51fa2020\u5e747\u6708\u4efd\u65e5\u5386\"<\/span>)
print(cal<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u8f93\u51fa2020\u5e747\u6708\u4efd\u65e5\u5386<\/span> \n
\n July 2020<\/span> \n
\nMo Tu We Th Fr Sa Su<\/span> \n
\n 1 2 3 4 5<\/span> \n
\n 6 7 8 9 10 11 12<\/span> \n
\n13 14 15 16 17 18 19<\/span> \n
\n20 21 22 23 24 25 26<\/span> \n
\n27 28 29 30 31<\/span><\/code><\/pre>\n<\/p><\/div>\nTime \u6a21\u5757<\/h2>\n
\n\n
\n \u51fd\u6570\u3001\u63cf\u8ff0\u548c\u793a\u4f8b<\/td>\n<\/tr>\n \n time.altzone<\/span>
\u8fd4\u56de\u683c\u6797\u5a01\u6cbb\u897f\u90e8\u7684\u590f\u4ee4\u65f6\u5730\u533a\u7684\u504f\u79fb\u79d2\u6570\u3002\u5982\u679c\u8be5\u5730\u533a\u5728\u683c\u6797\u5a01\u6cbb\u4e1c\u90e8\u4f1a\u8fd4\u56de\u8d1f\u503c\uff08\u5982\u897f\u6b27\uff0c\u5305\u62ec\u82f1\u56fd\uff09\u3002 <\/p>\n >>> import<\/span> time
>>> print(\"time.altzone %d \" % time.altzone<\/span>)
time.altzone 0 <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/td>\n<\/tr>\n\n time.asctime([tupletime])<\/span>
\u8fd4\u56de\u4e00\u4e2a\u5f62\u5f0f\u4e3a\"Tue Jul 14 11:39:58 2020\"\uff082020\u5e747\u670814\u65e5 \u5468\u4e8c11\u65f639\u520658\u79d2\uff09\u7684\u5b57\u7b26\u4e32\u3002 <\/p>\n >>> import<\/span> time
>>> t = time.localtime()<\/span>
>>> print(time.asctime(t)<\/span>)
Thu Apr 7 10:36:20 2016 <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/td>\n<\/tr>\n\n time.clock()<\/span>
\u7528\u4ee5\u6d6e\u70b9\u6570\u8ba1\u7b97\u7684\u79d2\u6570\u8fd4\u56de\u5f53\u524d\u7684CPU\u65f6\u95f4\u3002\u7528\u6765\u8861\u91cf\u4e0d\u540c\u7a0b\u5e8f\u7684\u8017\u65f6\uff0c\u6bd4time.time()\u66f4\u6709\u7528\u3002 <\/p>\n >>> import<\/span> time
>>> print(time.perf_counter()<\/span>)
>>> print(time.process_time()<\/span>)
22482074.841124818
0.013232696
<\/span><\/code><\/pre>\n<\/p><\/div>\n<\/td>\n<\/tr>\n\n time.ctime([secs])<\/span>
\u4f5c\u7528\u76f8\u5f53\u4e8easctime(localtime(secs))\uff0c\u672a\u7ed9\u53c2\u6570\u76f8\u5f53\u4e8easctime() <\/p>\n >>> import<\/span> time
>>> print(time.ctime()<\/span>)
Tue Jul 14 11:45:21 2020 <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/td>\n<\/tr>\n\n time.gmtime([secs])<\/span>
\u63a5\u6536\u65f6\u95f4\u6233\u5e76\u8fd4\u56de\u683c\u6797\u5a01\u6cbb\u5929\u6587\u65f6\u95f4\u4e0b\u7684\u65f6\u95f4\u5143\u7ec4t\u3002\u6ce8\uff1at.tm_isdst\u59cb\u7ec8\u4e3a0 <\/p>\n >>> import<\/span> time
>>> print(time.gmtime(1593508609.34375)<\/span>)
time.struct_time(tm_year=2020, tm_mon=6, tm_mday=30, tm_hour=9, tm_min=16, tm_sec=49, tm_wday=1, tm_yday=182, tm_isdst=0) <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/td>\n<\/tr>\n\n time.localtime([secs]<\/span>
\u63a5\u6536\u65f6\u95f4\u6233\u5e76\u8fd4\u56de\u5f53\u5730\u65f6\u95f4\u4e0b\u7684\u65f6\u95f4\u5143\u7ec4t\uff08t.tm_isdst\u53ef\u53d60\u62161\uff0c\u53d6\u51b3\u4e8e\u5f53\u5730\u5f53\u65f6\u662f\u4e0d\u662f\u590f\u4ee4\u65f6\uff09\u3002 <\/p>\n >>> import<\/span> time
>>> print(time.localtime(1593508609.34375)<\/span>)
time.struct_time(tm_year=2020, tm_mon=6, tm_mday=30, tm_hour=9, tm_min=16, tm_sec=49, tm_wday=1, tm_yday=182, tm_isdst=0) <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/td>\n<\/tr>\n\n time.mktime(tupletime)<\/span>
\u63a5\u53d7\u65f6\u95f4\u5143\u7ec4\u5e76\u8fd4\u56de\u65f6\u95f4\u6233\uff081970\u7eaa\u5143\u540e\u7ecf\u8fc7\u7684\u6d6e\u70b9\u79d2\u6570\uff09\u3002 <\/p>\n >>> import<\/span> time
>>> t = (2016, 2, 17, 17, 3, 38, 1, 48, 0)<\/span>
>>> secs = time.mktime(t)<\/span>
>>> print(secs<\/span>)
>>> print(time.asctime(time.localtime(secs))<\/span>)
1455728618.0
Wed Feb 17 17:03:38 2016 <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/td>\n<\/tr>\n\n time.sleep(secs)<\/span>
\u63a8\u8fdf\u8c03\u7528\u7ebf\u7a0b\u7684\u8fd0\u884c\uff0csecs\u6307\u79d2\u6570\u3002 <\/p>\n >>> import<\/span> time
>>> print(time.ctime()<\/span>)
>>> time.sleep(3) >>> print(time.ctime()<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n<\/td>\n<\/tr>\n\n time.strftime(fmt[,tupletime])<\/span>
\u63a5\u6536\u4ee5\u65f6\u95f4\u5143\u7ec4\uff0c\u5e76\u8fd4\u56de\u4ee5\u53ef\u8bfb\u5b57\u7b26\u4e32\u8868\u793a\u7684\u5f53\u5730\u65f6\u95f4\uff0c\u683c\u5f0f\u7531fmt\u51b3\u5b9a\u3002 <\/p>\n >>> import<\/span> time
>>> print(time.strftime(\"%Y-%m-%d %H:%M:%S\", time.localtime())<\/span>)
2020-07-14 11:57:27 <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/td>\n<\/tr>\n\n time.strptime(str,fmt='%a %b %d %H:%M:%S %Y')<\/span>
\u6839\u636efmt\u7684\u683c\u5f0f\u628a\u4e00\u4e2a\u65f6\u95f4\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u65f6\u95f4\u5143\u7ec4\u3002 <\/p>\n >>> import<\/span> time
>>> struct_time = time.strptime(\"30 Nov 00\", \"%d %b %y\")<\/span>
>>> print(struct_time<\/span>)
time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1) <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/td>\n<\/tr>\n\n time.time()<\/span>
\u8fd4\u56de\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233\uff081970\u7eaa\u5143\u540e\u7ecf\u8fc7\u7684\u6d6e\u70b9\u79d2\u6570\uff09\u3002 <\/p>\n >>> import<\/span> time
>>> print(time.time()<\/span>)
1594728225.9022148 <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/td>\n<\/tr>\n\n time.tzset()<\/span>
\u6839\u636e\u73af\u5883\u53d8\u91cfTZ\u91cd\u65b0\u521d\u59cb\u5316\u65f6\u95f4\u76f8\u5173\u8bbe\u7f6e\u3002 <\/p>\n >>> import<\/span> time
>>> import<\/span> os
>>> os.environ['TZ'] = 'EST+05EDT,M4.1.0,M10.5.0'<\/span>
>>> time.tzset()
>>> print(time.strftime('%X %x %Z')<\/span>)
>>> os.environ['TZ'] = 'AEST-10AEDT-11,M10.5.0,M3.5.0'<\/span>
>>> time.tzset()
>>> print(time.strftime('%X %x %Z')<\/span>)
08:08:47 07\/14\/20 EDT
22:08:47 07\/14\/20 AEST <\/span><\/code><\/pre>\n<\/p><\/div>\n<\/td>\n<\/tr>\n\n time.perf_counter()<\/span>
\u8fd4\u56de\u8ba1\u65f6\u5668\u7684\u7cbe\u51c6\u65f6\u95f4\uff0c\u7531\u4e8e\u8fd4\u56de\u503c\u7684\u57fa\u51c6\u70b9\u662f\u672a\u5b9a\u4e49\u7684\uff0c\u6240\u4ee5\uff0c\u53ea\u6709\u8fde\u7eed\u8c03\u7528\u7684\u7ed3\u679c\u4e4b\u95f4\u7684\u5dee\u624d\u662f\u6709\u6548\u7684\u3002 <\/p>\n >>> import<\/span> time
>>> time.sleep(1)<\/span>
>>> a = time.perf_counter()<\/span>
>>> print(a<\/span>)