{"id":407,"date":"2010-11-08T14:11:00","date_gmt":"2010-11-08T06:11:00","guid":{"rendered":"http:\/\/www.prayerlaputa.com\/?p=407"},"modified":"2019-10-22T14:52:56","modified_gmt":"2019-10-22T06:52:56","slug":"c%e8%af%ad%e8%a8%80%e4%b8%adtime%e5%87%bd%e6%95%b0%e3%80%90%e8%bd%ac%e8%bd%bd%e3%80%91-2","status":"publish","type":"post","link":"http:\/\/prayerlaputa.com\/?p=407","title":{"rendered":"C\u8bed\u8a00\u4e2dtime\u51fd\u6570\u3010\u8f6c\u8f7d\u3011"},"content":{"rendered":"<p>\u8f6c\u81ea\uff1ahttp:\/\/www.examda.com\/ncre2\/C\/jichu\/20100825\/08150493.html<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\nC\/C++\u4e2d\u7684\u65e5\u671f\u548c\u65f6\u95f4<br \/>\n\u5934\u6587\u4ef6 time.h<br \/>\n\u51fd\u6570\u7528\u9014 \u51fd\u6570\u540d<br \/>\n\u5f97\u5230\u5904\u7406\u5668\u65f6\u95f4 clock<!--more--><br \/>\n\u5f97\u5230\u65f6\u95f4\u5dee difftime<br \/>\n\u8bbe\u7f6e\u65f6\u95f4 mktime<br \/>\n\u5f97\u5230\u65f6\u95f4 time<br \/>\n\u5f97\u5230\u4ee5ASCII\u7801\u8868\u793a\u7684\u65f6\u95f4 asctime<br \/>\n\u5f97\u5230\u5b57\u7b26\u4e32\u8868\u793a\u7684\u65f6\u95f4 ctime<br \/>\n\u5f97\u5230\u6307\u5b9a\u683c\u5f0f\u7684\u65f6\u95f4 strftime<br \/>\n\u6458\u8981\uff1a<br \/>\n\u672c\u6587\u4ece\u4ecb\u7ecd\u57fa\u7840\u6982\u5ff5\u5165\u624b\uff0c\u63a2\u8ba8\u4e86\u5728C\/C++\u4e2d\u5bf9\u65e5\u671f\u548c\u65f6\u95f4\u64cd\u4f5c\u6240\u7528\u5230\u7684\u6570\u636e\u7ed3\u6784\u548c\u51fd\u6570\uff0c\u5e76\u5bf9\u8ba1\u65f6\u3001\u65f6\u95f4\u7684\u83b7\u53d6\u3001\u65f6\u95f4\u7684\u8ba1\u7b97\u548c\u663e\u793a\u683c\u5f0f\u7b49\u65b9\u9762\u8fdb\u884c\u4e86\u9610\u8ff0\u3002\u672c\u6587\u8fd8\u901a\u8fc7\u5927\u91cf\u7684\u5b9e\u4f8b\u5411\u4f60\u5c55\u793a\u4e86time.h\u5934\u6587\u4ef6\u4e2d\u58f0\u660e\u7684\u5404\u79cd\u51fd\u6570\u548c\u6570\u636e\u7ed3\u6784\u7684\u8be6\u7ec6\u4f7f\u7528\u65b9\u6cd5\u3002<br \/>\n\u5173\u952e\u5b57\uff1a<br \/>\nUTC\uff08\u4e16\u754c\u6807\u51c6\u65f6\u95f4\uff09\uff0cCalendar Time\uff08\u65e5\u5386\u65f6\u95f4\uff09\uff0cepoch\uff08\u65f6\u95f4\u70b9\uff09\uff0cclock tick\uff08\u65f6\u949f\u8ba1\u65f6\u5355\u5143\uff09<br \/>\n1\uff0e\u6982\u5ff5<br \/>\n\u5728C\/C++\u4e2d\uff0c\u5bf9\u5b57\u7b26\u4e32\u7684\u64cd\u4f5c\u6709\u5f88\u591a\u503c\u5f97\u6ce8\u610f\u7684\u95ee\u9898\uff0c\u540c\u6837\uff0cC\/C++\u5bf9\u65f6\u95f4\u7684\u64cd\u4f5c\u4e5f\u6709\u8bb8\u591a\u503c\u5f97\u5927\u5bb6\u6ce8\u610f\u7684\u5730\u65b9\u3002\u6700\u8fd1\uff0c\u5728\u6280\u672f\u7fa4\u4e2d\u6709\u5f88\u591a\u7f51\u53cb \u4e5f\u591a\u6b21\u95ee\u5230 \u8fc7C++\u8bed\u8a00\u4e2d\u5bf9\u65f6\u95f4\u7684\u64cd\u4f5c\u3001\u83b7\u53d6\u548c\u663e\u793a\u7b49\u7b49\u7684\u95ee\u9898\u3002\u4e0b\u9762\uff0c\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u7b14\u8005\u5c06\u4e3b\u8981\u4ecb\u7ecd\u5728C\/C++\u4e2d\u65f6\u95f4\u548c\u65e5\u671f\u7684\u4f7f\u7528\u65b9\u6cd5.<br \/>\n\u901a\u8fc7\u5b66\u4e60\u8bb8\u591aC\/C++\u5e93\uff0c\u4f60\u53ef\u4ee5\u6709\u5f88\u591a\u64cd\u4f5c\u3001\u4f7f\u7528\u65f6\u95f4\u7684\u65b9\u6cd5\u3002\u4f46\u5728\u8fd9\u4e4b\u524d\u4f60\u9700\u8981\u4e86\u89e3\u4e00\u4e9b\u201c\u65f6\u95f4\u201d\u548c\u201c\u65e5\u671f\u201d\u7684\u6982\u5ff5\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u4e2a\uff1a<br \/>\nCoordinated Universal Time\uff08UTC\uff09\uff1a\u534f\u8c03\u4e16\u754c\u65f6\uff0c\u53c8\u79f0\u4e3a\u4e16\u754c\u6807\u51c6\u65f6\u95f4\uff0c\u4e5f\u5c31\u662f\u5927\u5bb6\u6240\u719f\u77e5\u7684\u683c\u6797\u5a01\u6cbb\u6807\u51c6\u65f6\u95f4 \uff08Greenwich Mean Time\uff0cGMT\uff09\u3002\u6bd4\u5982\uff0c\u4e2d\u56fd\u5185\u5730\u7684\u65f6\u95f4\u4e0eUTC\u7684\u65f6\u5dee\u4e3a+8\uff0c\u4e5f\u5c31\u662fUTC+8\u3002\u7f8e\u56fd\u662fUTC-5\u3002<br \/>\nCalendar Time\uff1a\u65e5\u5386\u65f6\u95f4\uff0c\u662f\u7528\u201c\u4ece\u4e00\u4e2a\u6807\u51c6\u65f6\u95f4\u70b9\u5230\u6b64\u65f6\u7684\u65f6\u95f4\u7ecf\u8fc7\u7684\u79d2\u6570\u201d\u6765\u8868\u793a\u7684\u65f6\u95f4\u3002\u8fd9\u4e2a\u6807\u51c6\u65f6\u95f4\u70b9\u5bf9\u4e0d\u540c\u7684\u7f16\u8bd1\u5668\u6765\u8bf4\u4f1a\u6709\u6240\u4e0d\u540c\uff0c\u4f46 \u5bf9\u4e00\u4e2a\u7f16\u8bd1\u7cfb\u7edf\u6765\u8bf4\uff0c\u8fd9\u4e2a\u6807\u51c6\u65f6\u95f4\u70b9\u662f\u4e0d\u53d8\u7684\uff0c\u8be5\u7f16\u8bd1\u7cfb\u7edf\u4e2d\u7684\u65f6\u95f4\u5bf9\u5e94\u7684\u65e5\u5386\u65f6\u95f4\u90fd\u901a\u8fc7\u8be5\u6807\u51c6\u65f6\u95f4\u70b9\u6765\u8861\u91cf\uff0c\u6240\u4ee5\u53ef\u4ee5\u8bf4\u65e5\u5386\u65f6\u95f4\u662f\u201c\u76f8\u5bf9\u65f6\u95f4\u201d\uff0c\u4f46\u662f\u65e0\u8bba \u4f60\u5728\u54ea\u4e00\u4e2a\u65f6\u533a\uff0c\u5728\u540c\u4e00\u65f6\u523b\u5bf9\u540c\u4e00\u4e2a\u6807\u51c6\u65f6\u95f4\u70b9\u6765\u8bf4\uff0c\u65e5\u5386\u65f6\u95f4\u90fd\u662f\u4e00\u6837\u7684\u3002<br \/>\nepoch\uff1a\u65f6\u95f4\u70b9\u3002\u65f6\u95f4\u70b9\u5728\u6807\u51c6C\/C++\u4e2d\u662f\u4e00\u4e2a\u6574\u6570\uff0c\u5b83\u7528\u6b64\u65f6\u7684\u65f6\u95f4\u548c\u6807\u51c6\u65f6\u95f4\u70b9\u76f8\u5dee\u7684\u79d2\u6570\uff08\u5373\u65e5\u5386\u65f6\u95f4\uff09\u6765\u8868\u793a\u3002<br \/>\nclock tick\uff1a\u65f6\u949f\u8ba1\u65f6\u5355\u5143\uff08\u800c\u4e0d\u628a\u5b83\u53eb\u505a\u65f6\u949f\u6ef4\u7b54\u6b21\u6570\uff09\uff0c\u4e00\u4e2a\u65f6\u949f\u8ba1\u65f6\u5355\u5143\u7684\u65f6\u95f4\u957f\u77ed\u662f\u7531CPU\u63a7\u5236\u7684\u3002\u4e00\u4e2aclock tick\u4e0d\u662fCPU\u7684\u4e00\u4e2a\u65f6\u949f\u5468\u671f\uff0c\u800c\u662fC\/C++\u7684\u4e00\u4e2a\u57fa\u672c\u8ba1\u65f6\u5355\u4f4d\u3002<br \/>\n\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528ANSI\u6807\u51c6\u5e93\u4e2d\u7684time.h\u5934\u6587\u4ef6\u3002\u8fd9\u4e2a\u5934\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\u65f6\u95f4\u548c\u65e5\u671f\u6240\u4f7f\u7528\u7684\u65b9\u6cd5\uff0c\u65e0\u8bba\u662f\u5728\u7ed3\u6784\u5b9a\u4e49\uff0c\u8fd8\u662f\u547d\u540d\uff0c\u90fd\u5177\u6709\u660e\u663e\u7684C\u8bed\u8a00\u98ce\u683c\u3002\u4e0b\u9762\uff0c\u6211\u5c06\u8bf4\u660e\u5728C\/C++\u4e2d\u600e\u6837\u4f7f\u7528\u65e5\u671f\u7684\u65f6\u95f4\u529f\u80fd\u3002<br \/>\n2\uff0e \u8ba1\u65f6<br \/>\nC\/C++\u4e2d\u7684\u8ba1\u65f6\u51fd\u6570\u662fclock()\uff0c\u800c\u4e0e\u5176\u76f8\u5173\u7684\u6570\u636e\u7c7b\u578b\u662fclock_t\u3002\u5728MSDN\u4e2d\uff0c\u67e5\u5f97\u5bf9clock\u51fd\u6570\u5b9a\u4e49\u5982\u4e0b\uff1a<br \/>\nclock_t clock( void );<br \/>\n\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u4ece\u201c\u5f00\u542f\u8fd9\u4e2a\u7a0b\u5e8f\u8fdb\u7a0b\u201d\u5230\u201c\u7a0b\u5e8f\u4e2d\u8c03\u7528clock()\u51fd\u6570\u201d\u65f6\u4e4b\u95f4\u7684CPU\u65f6\u949f\u8ba1\u65f6\u5355\u5143\uff08clock tick\uff09\u6570\uff0c\u5728MSDN\u4e2d\u79f0\u4e4b\u4e3a\u6302\u949f \u65f6\u95f4\uff08wal-clock\uff09\u3002\u5176\u4e2dclock_t\u662f\u7528\u6765\u4fdd\u5b58\u65f6\u95f4\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5728time.h\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u627e\u5230\u5bf9\u5b83\u7684\u5b9a\u4e49\uff1a<br \/>\n#ifndef _CLOCK_T_DEFINED<br \/>\ntypedef long clock_t;<br \/>\n#define _CLOCK_T_DEFINED<br \/>\n#endif<br \/>\n\u5f88\u660e\u663e\uff0cclock_t\u662f\u4e00\u4e2a\u957f\u6574\u5f62\u6570\u3002\u5728time.h\u6587\u4ef6\u4e2d\uff0c\u8fd8\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5e38\u91cfCLOCKS_PER_SEC\uff0c\u5b83\u7528\u6765\u8868\u793a\u4e00\u79d2\u949f\u4f1a\u6709\u591a\u5c11\u4e2a\u65f6\u949f\u8ba1\u65f6\u5355\u5143\uff0c\u5176\u5b9a\u4e49\u5982\u4e0b\uff1a<br \/>\n#define CLOCKS_PER_SEC ((clock_t)1000)<br \/>\n\u53ef\u4ee5\u770b\u5230\u6bcf\u8fc7\u5343\u5206\u4e4b\u4e00\u79d2\uff081\u6beb\u79d2\uff09\uff0c\u8c03\u7528clock\uff08\uff09\u51fd\u6570\u8fd4\u56de\u7684\u503c\u5c31\u52a01\u3002\u4e0b\u9762\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u516c\u5f0fclock()\/CLOCKS_PER_SEC\u6765\u8ba1\u7b97\u4e00\u4e2a\u8fdb\u7a0b\u81ea\u8eab\u7684\u8fd0\u884c\u65f6\u95f4\uff1a<br \/>\nvoid elapsed_time()<br \/>\n{<br \/>\nprintf(&#8220;Elapsed time:%u secs.n&#8221;,clock()\/CLOCKS_PER_SEC);<br \/>\n}<br \/>\n\u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u7528clock\u51fd\u6570\u6765\u8ba1\u7b97\u4f60\u7684\u673a\u5668\u8fd0\u884c\u4e00\u4e2a\u5faa\u73af\u6216\u8005\u5904\u7406\u5176\u5b83\u4e8b\u4ef6\u5230\u5e95\u82b1\u4e86\u591a\u5c11\u65f6\u95f4\uff1a<br \/>\n#include \u201cstdio.h\u201d<br \/>\n#include \u201cstdlib.h\u201d<br \/>\n#include \u201ctime.h\u201d<br \/>\nint main( void )<br \/>\n{<br \/>\nlong i = 10000000L;<br \/>\nclock_t start, finish;<br \/>\ndouble duration;<br \/>\n\/* \u6d4b\u91cf\u4e00\u4e2a\u4e8b\u4ef6\u6301\u7eed\u7684\u65f6\u95f4*\/<br \/>\nprintf( &#8220;Time to do %ld empty loops is &#8220;, i );<br \/>\nstart = clock();<br \/>\nwhile( i&#8211; )<br \/>\nfinish = clock();<br \/>\nduration = (double)(finish &#8211; start) \/ CLOCKS_PER_SEC;<br \/>\nprintf( &#8220;%f secondsn&#8221;, duration );<br \/>\nsystem(&#8220;pause&#8221;);<br \/>\n}<br \/>\n\u5728\u7b14\u8005\u7684\u673a\u5668\u4e0a\uff0c\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<br \/>\nTime to do 10000000 empty loops is 0.03000 seconds<br \/>\n\u4e0a\u9762\u6211\u4eec\u770b\u5230\u65f6\u949f\u8ba1\u65f6\u5355\u5143\u7684\u957f\u5ea6\u4e3a1\u6beb\u79d2\uff0c\u90a3\u4e48\u8ba1\u65f6\u7684\u7cbe\u5ea6\u4e5f\u4e3a1\u6beb\u79d2\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4e0d\u53ef\u4ee5\u901a\u8fc7\u6539\u53d8CLOCKS_PER_SEC\u7684\u5b9a\u4e49\uff0c\u901a\u8fc7\u628a\u5b83\u5b9a\u4e49\u7684\u5927\u4e00\u4e9b\uff0c\u4ece\u800c\u4f7f\u8ba1\u65f6\u7cbe\u5ea6\u66f4\u9ad8\u5462\uff1f\u901a\u8fc7\u5c1d\u8bd5\uff0c\u4f60\u4f1a\u53d1\u73b0\u8fd9\u6837\u662f\u4e0d\u884c\u7684\u3002\u5728\u6807\u51c6C\/C++\u4e2d\uff0c\u6700\u5c0f\u7684\u8ba1\u65f6\u5355\u4f4d\u662f\u4e00\u6beb\u79d2\u3002<br \/>\n3\uff0e\u4e0e\u65e5\u671f\u548c\u65f6\u95f4\u76f8\u5173\u7684\u6570\u636e\u7ed3\u6784<br \/>\n\u5728\u6807\u51c6C\/C++\u4e2d\uff0c\u6211\u4eec\u53ef\u901a\u8fc7tm\u7ed3\u6784\u6765\u83b7\u5f97\u65e5\u671f\u548c\u65f6\u95f4\uff0ctm\u7ed3\u6784\u5728time.h\u4e2d\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a<br \/>\n#ifndef _TM_DEFINED<br \/>\nstruct tm {<br \/>\nint tm_sec; \/* \u79d2 \u2013 \u53d6\u503c\u533a\u95f4\u4e3a[0,59] *\/<br \/>\nint tm_min; \/* \u5206 &#8211; \u53d6\u503c\u533a\u95f4\u4e3a[0,59] *\/<br \/>\nint tm_hour; \/* \u65f6 &#8211; \u53d6\u503c\u533a\u95f4\u4e3a[0,23] *\/<br \/>\nint tm_mday; \/* \u4e00\u4e2a\u6708\u4e2d\u7684\u65e5\u671f &#8211; \u53d6\u503c\u533a\u95f4\u4e3a[1,31] *\/<br \/>\nint tm_mon; \/* \u6708\u4efd\uff08\u4ece\u4e00\u6708\u5f00\u59cb\uff0c0\u4ee3\u8868\u4e00\u6708\uff09 &#8211; \u53d6\u503c\u533a\u95f4\u4e3a[0,11] *\/<br \/>\nint tm_year; \/* \u5e74\u4efd\uff0c\u5176\u503c\u7b49\u4e8e\u5b9e\u9645\u5e74\u4efd\u51cf\u53bb1900 *\/<br \/>\nint tm_wday; \/* \u661f\u671f \u2013 \u53d6\u503c\u533a\u95f4\u4e3a[0,6]\uff0c\u5176\u4e2d0\u4ee3\u8868\u661f\u671f\u5929\uff0c1\u4ee3\u8868\u661f\u671f\u4e00\uff0c\u4ee5\u6b64\u7c7b\u63a8 *\/<br \/>\nint tm_yday; \/* \u4ece\u6bcf\u5e74\u76841\u67081\u65e5\u5f00\u59cb\u7684\u5929\u6570 \u2013 \u53d6\u503c\u533a\u95f4\u4e3a[0,365]\uff0c\u5176\u4e2d0\u4ee3\u88681\u67081\u65e5\uff0c1\u4ee3\u88681\u67082\u65e5\uff0c\u4ee5\u6b64\u7c7b\u63a8 *\/<br \/>\nint tm_isdst; \/* \u590f\u4ee4\u65f6\u6807\u8bc6\u7b26\uff0c\u5b9e\u884c\u590f\u4ee4\u65f6\u7684\u65f6\u5019\uff0ctm_isdst\u4e3a\u6b63\u3002\u4e0d\u5b9e\u884c\u590f\u4ee4\u65f6\u7684\u8fdb\u5019\uff0ctm_isdst\u4e3a0\uff1b\u4e0d\u4e86\u89e3\u60c5\u51b5\u65f6\uff0ctm_isdst()\u4e3a\u8d1f\u3002*\/<br \/>\n};<br \/>\n#define _TM_DEFINED<br \/>\n#endif<br \/>\nANSI C\u6807\u51c6\u79f0\u4f7f\u7528tm\u7ed3\u6784\u7684\u8fd9\u79cd\u65f6\u95f4\u8868\u793a\u4e3a\u5206\u89e3\u65f6\u95f4(broken-down time)\u3002\u800c\u65e5\u5386\u65f6\u95f4\uff08Calendar Time\uff09\u662f\u901a\u8fc7 time_t\u6570\u636e\u7c7b\u578b\u6765\u8868\u793a\u7684\uff0c\u7528time_t\u8868\u793a\u7684\u65f6\u95f4\uff08\u65e5\u5386\u65f6\u95f4\uff09\u662f\u4ece\u4e00\u4e2a\u65f6\u95f4\u70b9\uff08\u4f8b\u5982\uff1a1970\u5e741\u67081\u65e50\u65f60\u52060\u79d2\uff09\u5230\u6b64\u65f6\u7684\u79d2\u6570\u3002\u5728 time.h\u4e2d\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u770b\u5230time_t\u662f\u4e00\u4e2a\u957f\u6574\u578b\u6570\uff1a<br \/>\n#ifndef _TIME_T_DEFINED<br \/>\ntypedef long time_t; \/* \u65f6\u95f4\u503c *\/<br \/>\n#define _TIME_T_DEFINED \/* \u907f\u514d\u91cd\u590d\u5b9a\u4e49 time_t *\/<br \/>\n#endif<br \/>\n&nbsp;<br \/>\n\u5927\u5bb6\u53ef\u80fd\u4f1a\u4ea7\u751f\u7591\u95ee\uff1a\u65e2\u7136time_t\u5b9e\u9645\u4e0a\u662f\u957f\u6574\u578b\uff0c\u5230\u672a\u6765\u7684\u67d0\u4e00\u5929\uff0c\u4ece\u4e00\u4e2a\u65f6\u95f4\u70b9\uff08\u4e00\u822c\u662f1970\u5e741\u67081\u65e50\u65f60\u52060\u79d2\uff09\u5230\u90a3\u65f6\u7684\u79d2\u6570\uff08\u5373\u65e5 \u5386\u65f6 \u95f4\uff09\u8d85\u51fa\u4e86\u957f\u6574\u5f62\u6240\u80fd\u8868\u793a\u7684\u6570\u7684\u8303\u56f4\u600e\u4e48\u529e\uff1f\u5bf9time_t\u6570\u636e\u7c7b\u578b\u7684\u503c\u6765\u8bf4\uff0c\u5b83\u6240\u8868\u793a\u7684\u65f6\u95f4\u4e0d\u80fd\u665a\u4e8e2038\u5e741\u670818\u65e519\u65f614\u520607\u79d2\u3002\u4e3a\u4e86\u80fd\u591f \u8868\u793a\u66f4\u4e45\u8fdc\u7684\u65f6\u95f4\uff0c\u4e00\u4e9b\u7f16\u8bd1\u5668\u5382\u5546\u5f15\u5165\u4e8664\u4f4d\u751a\u81f3\u66f4\u957f\u7684\u6574\u5f62\u6570\u6765\u4fdd\u5b58\u65e5\u5386\u65f6\u95f4\u3002\u6bd4\u5982\u5fae\u8f6f\u5728Visual C++\u4e2d\u91c7\u7528\u4e86__time64_t\u6570\u636e\u7c7b\u578b\u6765 \u4fdd\u5b58\u65e5\u5386\u65f6\u95f4\uff0c\u5e76\u901a\u8fc7_time64()\u51fd\u6570\u6765\u83b7\u5f97\u65e5\u5386\u65f6\u95f4\uff08\u800c\u4e0d\u662f\u901a\u8fc7\u4f7f\u752832\u4f4d\u5b57\u7684time()\u51fd\u6570\uff09\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u901a\u8fc7\u8be5\u6570\u636e\u7c7b\u578b\u4fdd\u5b583001\u5e741\u6708 1\u65e50\u65f60\u52060\u79d2\uff08\u4e0d\u5305\u62ec\u8be5\u65f6\u95f4\u70b9\uff09\u4e4b\u524d\u7684\u65f6\u95f4\u3002<br \/>\n\u5728time.h\u5934\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u770b\u5230\u4e00\u4e9b\u51fd\u6570\uff0c\u5b83\u4eec\u90fd\u662f\u4ee5time_t\u4e3a\u53c2\u6570\u7c7b\u578b\u6216\u8fd4\u56de\u503c\u7c7b\u578b\u7684\u51fd\u6570\uff1a<br \/>\ndouble difftime(time_t time1, time_t time0);<br \/>\ntime_t mktime(struct tm * timeptr);<br \/>\ntime_t time(time_t * timer);<br \/>\nchar * asctime(const struct tm * timeptr);<br \/>\nchar * ctime(const time_t *timer);<br \/>\n\u6b64\u5916\uff0ctime.h\u8fd8\u63d0\u4f9b\u4e86\u4e24\u79cd\u4e0d\u540c\u7684\u51fd\u6570\u5c06\u65e5\u5386\u65f6\u95f4\uff08\u4e00\u4e2a\u7528time_t\u8868\u793a\u7684\u6574\u6570\uff09\u8f6c\u6362\u4e3a\u6211\u4eec\u5e73\u65f6\u770b\u5230\u7684\u628a\u5e74\u6708\u65e5\u65f6\u5206\u79d2\u5206\u5f00\u663e\u793a\u7684\u65f6\u95f4\u683c\u5f0ftm\uff1a<br \/>\nstruct tm * gmtime(const time_t *timer);<br \/>\nstruct tm * localtime(const time_t * timer);<br \/>\n\u901a\u8fc7\u67e5\u9605MSDN\uff0c\u6211\u4eec\u53ef\u4ee5\u77e5\u9053Microsoft C\/C++ 7.0\u4e2d\u65f6\u95f4\u70b9\u7684\u503c\uff08time_t\u5bf9\u8c61\u7684\u503c\uff09\u662f\u4ece1899\u5e7412\u670831\u65e50\u65f60\u52060\u79d2\u5230 \u8be5\u65f6\u95f4\u70b9\u6240\u7ecf\u8fc7\u7684\u79d2\u6570\uff0c\u800c\u5176\u5b83\u5404\u79cd\u7248\u672c\u7684Microsoft C\/C++\u548c\u6240\u6709\u4e0d\u540c\u7248\u672c\u7684Visual C++\u90fd\u662f\u8ba1\u7b97\u7684\u4ece1970\u5e741\u67081\u65e50\u65f60\u52060 \u79d2\u5230\u8be5\u65f6\u95f4\u70b9\u6240\u7ecf\u8fc7\u7684\u79d2\u6570\u3002<br \/>\n4\uff0e\u4e0e\u65e5\u671f\u548c\u65f6\u95f4\u76f8\u5173\u7684\u51fd\u6570\u53ca\u5e94\u7528<br \/>\n\u5728\u672c\u8282\uff0c\u6211\u5c06\u5411\u5927\u5bb6\u5c55\u793a\u600e\u6837\u5229\u7528time.h\u4e2d\u58f0\u660e\u7684\u51fd\u6570\u5bf9\u65f6\u95f4\u8fdb\u884c\u64cd\u4f5c\u3002\u8fd9\u4e9b\u64cd\u4f5c\u5305\u62ec\u53d6\u5f53\u524d\u65f6\u95f4\u3001\u8ba1\u7b97\u65f6\u95f4\u95f4\u9694\u3001\u4ee5\u4e0d\u540c\u7684\u5f62\u5f0f\u663e\u793a\u65f6\u95f4\u7b49\u5185\u5bb9\u3002<br \/>\n4.1 \u83b7\u5f97\u65e5\u5386\u65f6\u95f4<br \/>\n\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7time()\u51fd\u6570\u6765\u83b7\u5f97\u65e5\u5386\u65f6\u95f4\uff08Calendar Time\uff09\uff0c\u5176\u539f\u578b\u4e3a\uff1atime_t time(time_t * timer);<br \/>\n\u5982\u679c\u4f60\u5df2\u7ecf\u58f0\u660e\u4e86\u53c2\u6570timer\uff0c\u4f60\u53ef\u4ee5\u4ece\u53c2\u6570timer\u8fd4\u56de\u73b0\u5728\u7684\u65e5\u5386\u65f6\u95f4\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u901a\u8fc7\u8fd4\u56de\u503c\u8fd4\u56de\u73b0\u5728\u7684\u65e5\u5386\u65f6\u95f4\uff0c\u5373\u4ece\u4e00\u4e2a\u65f6\u95f4\u70b9\uff08\u4f8b \u5982\uff1a1970 \u5e741\u67081\u65e50\u65f60\u52060\u79d2\uff09\u5230\u73b0\u5728\u6b64\u65f6\u7684\u79d2\u6570\u3002\u5982\u679c\u53c2\u6570\u4e3a\u7a7a\uff08NUL\uff09\uff0c\u51fd\u6570\u5c06\u53ea\u901a\u8fc7\u8fd4\u56de\u503c\u8fd4\u56de\u73b0\u5728\u7684\u65e5\u5386\u65f6\u95f4\uff0c\u6bd4\u5982\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u7528\u6765\u663e\u793a\u5f53\u524d\u7684\u65e5\u5386\u65f6\u95f4\uff1a<br \/>\n#include &#8220;time.h&#8221;<br \/>\n#include &#8220;stdio.h&#8221;<br \/>\nint main(void)<br \/>\n{<br \/>\nstruct tm *ptr;<br \/>\ntime_t lt;<br \/>\nlt =time(NUL);<br \/>\nprintf(&#8220;The Calendar Time now is %dn&#8221;,lt);<br \/>\nreturn 0;<br \/>\n}<br \/>\n\u8fd0\u884c\u7684\u7ed3\u679c\u4e0e\u5f53\u65f6\u7684\u65f6\u95f4\u6709\u5173\uff0c\u6211\u5f53\u65f6\u8fd0\u884c\u7684\u7ed3\u679c\u662f\uff1a<br \/>\nThe Calendar Time now is 1122707619<br \/>\n\u5176\u4e2d1122707619\u5c31\u662f\u6211\u8fd0\u884c\u7a0b\u5e8f\u65f6\u7684\u65e5\u5386\u65f6\u95f4\u3002\u5373\u4ece1970\u5e741\u67081\u65e50\u65f60\u52060\u79d2\u5230\u6b64\u65f6\u7684\u79d2\u6570\u3002<br \/>\n4.2 \u83b7\u5f97\u65e5\u671f\u548c\u65f6\u95f4<br \/>\n\u8fd9\u91cc\u8bf4\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5c31\u662f\u6211\u4eec\u5e73\u65f6\u6240\u8bf4\u7684\u5e74\u3001\u6708\u3001\u65e5\u3001\u65f6\u3001\u5206\u3001\u79d2\u7b49\u4fe1\u606f\u3002\u4ece\u7b2c2\u8282\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u8fd9\u4e9b\u4fe1\u606f\u90fd\u4fdd\u5b58\u5728\u4e00\u4e2a\u540d\u4e3atm\u7684\u7ed3\u6784\u4f53\u4e2d\uff0c\u90a3\u4e48\u5982\u4f55\u5c06\u4e00\u4e2a\u65e5\u5386\u65f6\u95f4\u4fdd\u5b58\u4e3a\u4e00\u4e2atm\u7ed3\u6784\u7684\u5bf9\u8c61\u5462\uff1f<br \/>\n\u5176\u4e2d\u53ef\u4ee5\u4f7f\u7528\u7684\u51fd\u6570\u662fgmtime()\u548clocaltime()\uff0c\u8fd9\u4e24\u4e2a\u51fd\u6570\u7684\u539f\u578b\u4e3a\uff1a<br \/>\nstruct tm * gmtime(const time_t *timer);<br \/>\nstruct tm * localtime(const time_t * timer);<br \/>\n\u5176\u4e2dgmtime()\u51fd\u6570\u662f\u5c06\u65e5\u5386\u65f6\u95f4\u8f6c\u5316\u4e3a\u4e16\u754c\u6807\u51c6\u65f6\u95f4\uff08\u5373\u683c\u6797\u5c3c\u6cbb\u65f6\u95f4\uff09\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2atm\u7ed3\u6784\u4f53\u6765\u4fdd\u5b58\u8fd9\u4e2a\u65f6\u95f4\uff0c\u800clocaltime() \u51fd\u6570\u662f\u5c06\u65e5 \u5386\u65f6\u95f4\u8f6c\u5316\u4e3a\u672c\u5730\u65f6\u95f4\u3002\u6bd4\u5982\u73b0\u5728\u7528gmtime()\u51fd\u6570\u83b7\u5f97\u7684\u4e16\u754c\u6807\u51c6\u65f6\u95f4\u662f2005\u5e747\u670830\u65e57\u70b918\u520620\u79d2\uff0c\u90a3\u4e48\u6211\u7528localtime()\u51fd\u6570 \u5728\u4e2d\u56fd\u5730\u533a\u83b7\u5f97\u7684\u672c\u5730\u65f6\u95f4\u4f1a\u6bd4\u4e16\u754c\u6807\u51c6\u65f6\u95f4\u665a8\u4e2a\u5c0f\u65f6\uff0c\u53732005\u5e747\u670830\u65e515\u70b918\u520620\u79d2\u3002\u4e0b\u9762\u662f\u4e2a\u4f8b\u5b50\uff1a<br \/>\n#include &#8220;time.h&#8221;<br \/>\n#include &#8220;stdio.h&#8221;<br \/>\nint main(void)<br \/>\n{<br \/>\nstruct tm *local;<br \/>\ntime_t t;<br \/>\nt=time(NUL);<br \/>\nlocal=localtime(&amp;t);<br \/>\nprintf(&#8220;Local hour is: %dn&#8221;,local-&gt;tm_hour);<br \/>\nlocal=gmtime(&amp;t);<br \/>\nprintf(&#8220;UTC hour is: %dn&#8221;,local-&gt;tm_hour);<br \/>\nreturn 0;<br \/>\n}<br \/>\n\u8fd0\u884c\u7ed3\u679c\u662f\uff1a<br \/>\nLocal hour is: 15<br \/>\nUTC hour is: 7<br \/>\n4.3 \u56fa\u5b9a\u7684\u65f6\u95f4\u683c\u5f0f<br \/>\n\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7asctime()\u51fd\u6570\u548cctime()\u51fd\u6570\u5c06\u65f6\u95f4\u4ee5\u56fa\u5b9a\u7684\u683c\u5f0f\u663e\u793a\u51fa\u6765\uff0c\u4e24\u8005\u7684\u8fd4\u56de\u503c\u90fd\u662fchar*\u578b\u7684\u5b57\u7b26\u4e32\u3002\u8fd4\u56de\u7684\u65f6\u95f4\u683c\u5f0f\u4e3a\uff1a<br \/>\n\u661f\u671f\u51e0 \u6708\u4efd \u65e5\u671f \u65f6:\u5206:\u79d2 \u5e74n<br \/>\n\u4f8b\u5982\uff1aWed Jan 02 02:03:55 1980n<br \/>\n\u5176\u4e2dn\u662f\u4e00\u4e2a\u6362\u884c\u7b26\uff0c\u662f\u4e00\u4e2a\u7a7a\u5b57\u7b26\uff0c\u8868\u793a\u5b57\u7b26\u4e32\u7ed3\u675f\u3002\u4e0b\u9762\u662f\u4e24\u4e2a\u51fd\u6570\u7684\u539f\u578b\uff1a<br \/>\nchar * asctime(const struct tm * timeptr);<br \/>\nchar * ctime(const time_t *timer);<br \/>\n\u5176\u4e2dasctime()\u51fd\u6570\u662f\u901a\u8fc7tm\u7ed3\u6784\u6765\u751f\u6210\u5177\u6709\u56fa\u5b9a\u683c\u5f0f\u7684\u4fdd\u5b58\u65f6\u95f4\u4fe1\u606f\u7684\u5b57\u7b26\u4e32\uff0c\u800cctime()\u662f\u901a\u8fc7\u65e5\u5386\u65f6\u95f4\u6765\u751f\u6210\u65f6\u95f4\u5b57\u7b26\u4e32\u3002\u8fd9\u6837 \u7684\u8bdd\uff0c asctime\uff08\uff09\u51fd\u6570\u53ea\u662f\u628atm\u7ed3\u6784\u5bf9\u8c61\u4e2d\u7684\u5404\u4e2a\u57df\u586b\u5230\u65f6\u95f4\u5b57\u7b26\u4e32\u7684\u76f8\u5e94\u4f4d\u7f6e\u5c31\u884c\u4e86\uff0c\u800cctime\uff08\uff09\u51fd\u6570\u9700\u8981\u5148\u53c2\u7167\u672c\u5730\u7684\u65f6\u95f4\u8bbe\u7f6e\uff0c\u628a\u65e5\u5386\u65f6\u95f4\u8f6c\u5316\u4e3a \u672c\u5730\u65f6\u95f4\uff0c\u7136\u540e\u518d\u751f\u6210\u683c\u5f0f\u5316\u540e\u7684\u5b57\u7b26\u4e32\u3002\u5728\u4e0b\u9762\uff0c\u5982\u679ct\u662f\u4e00\u4e2a\u975e\u7a7a\u7684time_t\u53d8\u91cf\u7684\u8bdd\uff0c\u90a3\u4e48\uff1a<br \/>\nprintf(ctime(&amp;t));<br \/>\n\u7b49\u4ef7\u4e8e\uff1a<br \/>\nstruct tm *ptr;<br \/>\nptr=localtime(&amp;t);<br \/>\nprintf(asctime(ptr));<br \/>\n\u90a3\u4e48\uff0c\u4e0b\u9762\u8fd9\u4e2a\u7a0b\u5e8f\u7684\u4e24\u6761printf\u8bed\u53e5\u8f93\u51fa\u7684\u7ed3\u679c\u5c31\u662f\u4e0d\u540c\u7684\u4e86\uff08\u9664\u975e\u4f60\u5c06\u672c\u5730\u65f6\u533a\u8bbe\u4e3a\u4e16\u754c\u6807\u51c6\u65f6\u95f4\u6240\u5728\u7684\u65f6\u533a\uff09\uff1a<br \/>\n#include &#8220;time.h&#8221;<br \/>\n#include &#8220;stdio.h&#8221;<br \/>\nint main(void)<br \/>\n{<br \/>\nstruct tm *ptr;<br \/>\ntime_t lt;<br \/>\nlt =time(NUL);<br \/>\nptr=gmtime(&lt;);<br \/>\nprintf(asctime(ptr));<br \/>\nprintf(ctime(&lt;));<br \/>\nreturn 0;<br \/>\n}<br \/>\n\u8fd0\u884c\u7ed3\u679c\uff1a<br \/>\nSat Jul 30 08:43:03 2005<br \/>\nSat Jul 30 16:43:03 2005<br \/>\n4.4 \u81ea\u5b9a\u4e49\u65f6\u95f4\u683c\u5f0f<br \/>\n\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528strftime\uff08\uff09\u51fd\u6570\u5c06\u65f6\u95f4\u683c\u5f0f\u5316\u4e3a\u6211\u4eec\u60f3\u8981\u7684\u683c\u5f0f\u3002\u5b83\u7684\u539f\u578b\u5982\u4e0b\uff1a<br \/>\nsize_t strftime(<br \/>\nchar *strDest,<br \/>\nsize_t maxsize,<br \/>\nconst char *format,<br \/>\nconst struct tm *timeptr<br \/>\n);<br \/>\n\u6211\u4eec\u53ef\u4ee5\u6839\u636eformat\u6307\u5411\u5b57\u7b26\u4e32\u4e2d\u683c\u5f0f\u547d\u4ee4\u628atimeptr\u4e2d\u4fdd\u5b58\u7684\u65f6\u95f4\u4fe1\u606f\u653e\u5728strDest\u6307\u5411\u7684\u5b57\u7b26\u4e32\u4e2d\uff0c\u6700\u591a\u5411strDest\u4e2d\u5b58\u653emaxsize\u4e2a\u5b57\u7b26\u3002\u8be5\u51fd\u6570\u8fd4\u56de\u5411strDest\u6307\u5411\u7684\u5b57\u7b26\u4e32\u4e2d\u653e\u7f6e\u7684\u5b57\u7b26\u6570\u3002<br \/>\n\u51fd\u6570strftime()\u7684\u64cd\u4f5c\u6709\u4e9b\u7c7b\u4f3c\u4e8esprintf()\uff1a\u8bc6\u522b\u4ee5\u767e\u5206\u53f7(%)\u5f00\u59cb\u7684\u683c\u5f0f\u547d\u4ee4\u96c6\u5408\uff0c\u683c\u5f0f\u5316\u8f93\u51fa\u7ed3\u679c\u653e\u5728\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u3002\u683c\u5f0f \u5316\u547d\u4ee4\u8bf4\u660e \u4e32strDest\u4e2d\u5404\u79cd\u65e5\u671f\u548c\u65f6\u95f4\u4fe1\u606f\u7684\u786e\u5207\u8868\u793a\u65b9\u6cd5\u3002\u683c\u5f0f\u4e32\u4e2d\u7684\u5176\u4ed6\u5b57\u7b26\u539f\u6837\u653e\u8fdb\u4e32\u4e2d\u3002\u683c\u5f0f\u547d\u4ee4\u5217\u5728\u4e0b\u9762\uff0c\u5b83\u4eec\u662f\u533a\u5206\u5927\u5c0f\u5199\u7684\u3002<br \/>\n%a \u661f\u671f\u51e0\u7684\u7b80\u5199<br \/>\n%A \u661f\u671f\u51e0\u7684\u5168\u79f0<br \/>\n%b \u6708\u5206\u7684\u7b80\u5199<br \/>\n%B \u6708\u4efd\u7684\u5168\u79f0<br \/>\n%c \u6807\u51c6\u7684\u65e5\u671f\u7684\u65f6\u95f4\u4e32<br \/>\n%C \u5e74\u4efd\u7684\u540e\u4e24\u4f4d\u6570\u5b57<br \/>\n%d \u5341\u8fdb\u5236\u8868\u793a\u7684\u6bcf\u6708\u7684\u7b2c\u51e0\u5929<br \/>\n%D \u6708\/\u5929\/\u5e74<br \/>\n%e \u5728\u4e24\u5b57\u7b26\u57df\u4e2d\uff0c\u5341\u8fdb\u5236\u8868\u793a\u7684\u6bcf\u6708\u7684\u7b2c\u51e0\u5929<br \/>\n%F \u5e74-\u6708-\u65e5<br \/>\n%g \u5e74\u4efd\u7684\u540e\u4e24\u4f4d\u6570\u5b57\uff0c\u4f7f\u7528\u57fa\u4e8e\u5468\u7684\u5e74<br \/>\n%G \u5e74\u5206\uff0c\u4f7f\u7528\u57fa\u4e8e\u5468\u7684\u5e74<br \/>\n%h \u7b80\u5199\u7684\u6708\u4efd\u540d<br \/>\n%H 24\u5c0f\u65f6\u5236\u7684\u5c0f\u65f6<br \/>\n%I 12\u5c0f\u65f6\u5236\u7684\u5c0f\u65f6<br \/>\n%j \u5341\u8fdb\u5236\u8868\u793a\u7684\u6bcf\u5e74\u7684\u7b2c\u51e0\u5929<br \/>\n%m \u5341\u8fdb\u5236\u8868\u793a\u7684\u6708\u4efd<br \/>\n%M \u5341\u65f6\u5236\u8868\u793a\u7684\u5206\u949f\u6570<br \/>\n%n \u65b0\u884c\u7b26<br \/>\n%p \u672c\u5730\u7684AM\u6216PM\u7684\u7b49\u4ef7\u663e\u793a<br \/>\n%r 12\u5c0f\u65f6\u7684\u65f6\u95f4<br \/>\n%R \u663e\u793a\u5c0f\u65f6\u548c\u5206\u949f\uff1ahh:mm<br \/>\n%S \u5341\u8fdb\u5236\u7684\u79d2\u6570<br \/>\n%t \u6c34\u5e73\u5236\u8868\u7b26<br \/>\n%T \u663e\u793a\u65f6\u5206\u79d2\uff1ahh:mm:ss<br \/>\n%u \u6bcf\u5468\u7684\u7b2c\u51e0\u5929\uff0c\u661f\u671f\u4e00\u4e3a\u7b2c\u4e00\u5929 \uff08\u503c\u4ece0\u52306\uff0c\u661f\u671f\u4e00\u4e3a0\uff09<br \/>\n%U \u7b2c\u5e74\u7684\u7b2c\u51e0\u5468\uff0c\u628a\u661f\u671f\u65e5\u505a\u4e3a\u7b2c\u4e00\u5929\uff08\u503c\u4ece0\u523053\uff09<br \/>\n%V \u6bcf\u5e74\u7684\u7b2c\u51e0\u5468\uff0c\u4f7f\u7528\u57fa\u4e8e\u5468\u7684\u5e74<br \/>\n%w \u5341\u8fdb\u5236\u8868\u793a\u7684\u661f\u671f\u51e0\uff08\u503c\u4ece0\u52306\uff0c\u661f\u671f\u5929\u4e3a0\uff09<br \/>\n%W \u6bcf\u5e74\u7684\u7b2c\u51e0\u5468\uff0c\u628a\u661f\u671f\u4e00\u505a\u4e3a\u7b2c\u4e00\u5929\uff08\u503c\u4ece0\u523053\uff09<br \/>\n%x \u6807\u51c6\u7684\u65e5\u671f\u4e32<br \/>\n%X \u6807\u51c6\u7684\u65f6\u95f4\u4e32<br \/>\n%y \u4e0d\u5e26\u4e16\u7eaa\u7684\u5341\u8fdb\u5236\u5e74\u4efd\uff08\u503c\u4ece0\u523099\uff09<br \/>\n%Y \u5e26\u4e16\u7eaa\u90e8\u5206\u7684\u5341\u8fdb\u5236\u5e74\u4efd<br \/>\n%z\uff0c%Z \u65f6\u533a\u540d\u79f0\uff0c\u5982\u679c\u4e0d\u80fd\u5f97\u5230\u65f6\u533a\u540d\u79f0\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u3002<br \/>\n%% \u767e\u5206\u53f7<br \/>\n\u5982\u679c\u60f3\u663e\u793a\u73b0\u5728\u662f\u51e0\u70b9\u4e86\uff0c\u5e76\u4ee512\u5c0f\u65f6\u5236\u663e\u793a\uff0c\u5c31\u8c61\u4e0b\u9762\u8fd9\u6bb5\u7a0b\u5e8f\uff1a<br \/>\n#include \u201ctime.h\u201d<br \/>\n#include \u201cstdio.h\u201d<br \/>\nint main(void)<br \/>\n{<br \/>\nstruct tm *ptr;<br \/>\ntime_t lt;<br \/>\nchar str[80];<br \/>\nlt=time(NUL);<br \/>\nptr=localtime(&lt;);<br \/>\nstrftime(str,100,&#8221;It is now %I %p&#8221;,ptr);<br \/>\nprintf(str);<br \/>\nreturn 0;<br \/>\n}<br \/>\n\u5176\u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a<br \/>\nIt is now 4PM<br \/>\n\u800c\u4e0b\u9762\u7684\u7a0b\u5e8f\u5219\u663e\u793a\u5f53\u524d\u7684\u5b8c\u6574\u65e5\u671f\uff1a<br \/>\n#include<br \/>\n#include<br \/>\nvoid main( void )<br \/>\n{<br \/>\nstruct tm *newtime;<br \/>\nchar tmpbuf[128];<br \/>\ntime_t lt1;<br \/>\ntime( &lt;1 );<br \/>\nnewtime=localtime(&lt;1);<br \/>\nstrftime( tmpbuf, 128, &#8220;Today is %A, day %d of %B in the year %Y.n&#8221;, newtime);<br \/>\nprintf(tmpbuf);<br \/>\n}<br \/>\n\u8fd0\u884c\u7ed3\u679c\uff1a<br \/>\nToday is Saturday, day 30 of July in the year 2005.<br \/>\n4.5 \u8ba1\u7b97\u6301\u7eed\u65f6\u95f4\u7684\u957f\u5ea6<br \/>\n\u6709\u65f6\u5019\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u8981\u8ba1\u7b97\u4e00\u4e2a\u4e8b\u4ef6\u6301\u7eed\u7684\u65f6\u95f4\u957f\u5ea6\uff0c\u6bd4\u5982\u8ba1\u7b97\u6253\u5b57\u901f\u5ea6\u3002\u5728\u7b2c1\u8282\u8ba1\u65f6\u90e8\u5206\u4e2d\uff0c\u6211\u5df2\u7ecf\u7528clock\u51fd\u6570\u4e3e\u4e86\u4e00\u4e2a\u4f8b\u5b50\u3002Clock()\u51fd\u6570\u53ef\u4ee5\u7cbe\u786e\u5230\u6beb\u79d2\u7ea7\u3002\u540c\u65f6\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528difftime()\u51fd\u6570\uff0c\u4f46\u5b83\u53ea\u80fd\u7cbe\u786e\u5230\u79d2\u3002\u8be5\u51fd\u6570\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a<br \/>\ndouble difftime(time_t time1, time_t time0);<br \/>\n\u867d\u7136\u8be5\u51fd\u6570\u8fd4\u56de\u7684\u4ee5\u79d2\u8ba1\u7b97\u7684\u65f6\u95f4\u95f4\u9694\u662fdouble\u7c7b\u578b\u7684\uff0c\u4f46\u8fd9\u5e76\u4e0d\u8bf4\u660e\u8be5\u65f6\u95f4\u5177\u6709\u540cdouble\u4e00\u6837\u7684\u7cbe\u786e\u5ea6\uff0c\u8fd9\u662f\u7531\u5b83\u7684\u53c2\u6570\u89c9\u5f97\u7684\uff08time_t\u662f\u4ee5\u79d2\u4e3a\u5355\u4f4d\u8ba1\u7b97\u7684\uff09\u3002\u6bd4\u5982\u4e0b\u9762\u4e00\u6bb5\u7a0b\u5e8f\uff1a<br \/>\n#include &#8220;time.h&#8221;<br \/>\n#include &#8220;stdio.h&#8221;<br \/>\n#include &#8220;stdlib.h&#8221;<br \/>\nint main(void)<br \/>\n{<br \/>\ntime_t start,end;<br \/>\nstart = time(NUL);<br \/>\nsystem(&#8220;pause&#8221;);<br \/>\nend = time(NUL);<br \/>\nprintf(&#8220;The pause used %f seconds.n&#8221;,difftime(end,start));\/\/&lt;-<br \/>\nsystem(&#8220;pause&#8221;);<br \/>\nreturn 0;<br \/>\n}<br \/>\n\u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a<br \/>\n\u8bf7\u6309\u4efb\u610f\u952e\u7ee7\u7eed. . .<br \/>\nThe pause used 2.000000 seconds.<br \/>\n\u8bf7\u6309\u4efb\u610f\u952e\u7ee7\u7eed. . .<br \/>\n\u53ef\u4ee5\u60f3\u50cf\uff0c\u6682\u505c\u7684\u65f6\u95f4\u5e76\u4e0d\u90a3\u4e48\u5de7\u662f\u6574\u65742\u79d2\u949f\u3002\u5176\u5b9e\uff0c\u4f60\u5c06\u4e0a\u9762\u7a0b\u5e8f\u7684\u5e26\u6709\u201c\/\/&lt;-\u201d\u6ce8\u91ca\u7684\u4e00\u884c\u7528\u4e0b\u9762\u7684\u4e00\u884c\u4ee3\u7801\u66ff\u6362\uff1a<br \/>\nprintf(&#8220;The pause used %f seconds.n&#8221;,end-start);<br \/>\n\u5176\u8fd0\u884c\u7ed3\u679c\u662f\u4e00\u6837\u7684\u3002<br \/>\n4.6 \u5206\u89e3\u65f6\u95f4\u8f6c\u5316\u4e3a\u65e5\u5386\u65f6\u95f4<br \/>\n\u8fd9\u91cc\u8bf4\u7684\u5206\u89e3\u65f6\u95f4\u5c31\u662f\u4ee5\u5e74\u3001\u6708\u3001\u65e5\u3001\u65f6\u3001\u5206\u3001\u79d2\u7b49\u5206\u91cf\u4fdd\u5b58\u7684\u65f6\u95f4\u7ed3\u6784\uff0c\u5728C\/C++\u4e2d\u662ftm\u7ed3\u6784\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528mktime\uff08\uff09\u51fd\u6570\u5c06\u7528tm\u7ed3\u6784\u8868\u793a\u7684\u65f6\u95f4\u8f6c\u5316\u4e3a\u65e5\u5386\u65f6\u95f4\u3002\u5176\u51fd\u6570\u539f\u578b\u5982\u4e0b\uff1a<br \/>\ntime_t mktime(struct tm * timeptr);<br \/>\n\u5176\u8fd4\u56de\u503c\u5c31\u662f\u8f6c\u5316\u540e\u7684\u65e5\u5386\u65f6\u95f4\u3002\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5148\u5236\u5b9a\u4e00\u4e2a\u5206\u89e3\u65f6\u95f4\uff0c\u7136\u540e\u5bf9\u8fd9\u4e2a\u65f6\u95f4\u8fdb\u884c\u64cd\u4f5c\u4e86\uff0c\u4e0b\u9762\u7684\u4f8b\u5b50\u53ef\u4ee5\u8ba1\u7b97\u51fa1997\u5e747\u67081\u65e5\u662f\u661f\u671f\u51e0\uff1a<br \/>\n#include &#8220;time.h&#8221;<br \/>\n#include &#8220;stdio.h&#8221;<br \/>\n#include &#8220;stdlib.h&#8221;<br \/>\nint main(void)<br \/>\n{<br \/>\nstruct tm t;<br \/>\ntime_t t_of_day;<br \/>\nt.tm_year=1997-1900;<br \/>\nt.tm_mon=6;<br \/>\nt.tm_mday=1;<br \/>\nt.tm_hour=0;<br \/>\nt.tm_min=0;<br \/>\nt.tm_sec=1;<br \/>\nt.tm_isdst=0;<br \/>\nt_of_day=mktime(&amp;t);<br \/>\nprintf(ctime(&amp;t_of_day));<br \/>\nreturn 0;<br \/>\n}<br \/>\n\u8fd0\u884c\u7ed3\u679c\uff1a<br \/>\nTue Jul 01 00:00:01 1997<br \/>\n\u73b0\u5728\u6ce8\u610f\u4e86\uff0c\u6709\u4e86mktime()\u51fd\u6570\uff0c\u662f\u4e0d\u662f\u6211\u4eec\u53ef\u4ee5\u64cd\u4f5c\u73b0\u5728\u4e4b\u524d\u7684\u4efb\u4f55\u65f6\u95f4\u5462\uff1f\u4f60\u53ef\u4ee5\u901a\u8fc7\u8fd9\u79cd\u529e\u6cd5\u7b97\u51fa1945\u5e748\u670815\u53f7\u662f\u661f\u671f\u51e0\u5417\uff1f\u7b54 \u6848\u662f\u5426\u5b9a\u7684\u3002\u56e0\u4e3a\u8fd9\u4e2a\u65f6\u95f4\u57281970\u5e741\u67081\u65e5\u4e4b\u524d\uff0c\u6240\u4ee5\u5728\u5927\u591a\u6570\u7f16\u8bd1\u5668\u4e2d\uff0c\u8fd9\u6837\u7684\u7a0b\u5e8f\u867d\u7136\u53ef\u4ee5\u7f16\u8bd1\u901a\u8fc7\uff0c\u4f46\u8fd0\u884c\u65f6\u4f1a\u5f02\u5e38\u7ec8\u6b62\u3002<br \/>\n5\uff0e\u603b\u7ed3<br \/>\n\u672c\u6587\u4ecb\u7ecd\u4e86\u6807\u51c6C\/C++\u4e2d\u7684\u6709\u5173\u65e5\u671f\u548c\u65f6\u95f4\u7684\u6982\u5ff5\uff0c\u5e76\u901a\u8fc7\u5404\u79cd\u5b9e\u4f8b\u8bb2\u8ff0\u4e86\u8fd9\u4e9b\u51fd\u6570\u548c\u6570\u636e\u7ed3\u6784\u7684\u4f7f\u7528\u65b9\u6cd5\u3002\u7b14\u8005\u8ba4\u4e3a\uff0c\u548c\u65f6\u95f4\u76f8\u5173\u7684\u4e00\u4e9b\u6982\u5ff5\u662f\u76f8\u5f53\u91cd\u8981\u7684\uff0c\u7406\u89e3\u8fd9\u4e9b\u6982\u5ff5\u662f\u7406\u89e3\u5404\u79cd\u65f6\u95f4\u683c\u5f0f\u7684\u8f6c\u6362\u7684\u57fa\u7840\uff0c\u66f4\u662f\u5e94\u7528\u8fd9\u4e9b\u51fd\u6570\u548c\u6570\u636e\u7ed3\u6784\u7684\u57fa\u7840\u3002<br \/>\n&nbsp;<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u81ea\uff1ahttp:\/\/www.examda.com\/ncre2\/C\/jichu\/20100825\/0815049 <a href='http:\/\/prayerlaputa.com\/?p=407' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[4],"tags":[27],"_links":{"self":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/407"}],"collection":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=407"}],"version-history":[{"count":1,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/407\/revisions"}],"predecessor-version":[{"id":685,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/407\/revisions\/685"}],"wp:attachment":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=407"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}