{"id":80,"date":"2026-03-31T20:09:34","date_gmt":"2026-03-31T12:09:34","guid":{"rendered":"https:\/\/blog.kiloming.top\/?p=80"},"modified":"2026-04-01T22:07:45","modified_gmt":"2026-04-01T14:07:45","slug":"c%e4%b8%ad%e7%9a%84ctime%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/blog.kiloming.top\/?p=80","title":{"rendered":"C++\u4e2d\u7684&lt;ctime>\u51fd\u6570"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1. \u524d\u8a00<\/h2>\n\n\n\n<p>&lt;ctime&gt;\u51fd\u6570\u5e93\u4e2d\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6709\u5173\u65f6\u95f4\u7684\u51fd\u6570\uff0c\u4f7f\u7528\u6b64\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u65e5\u671f\uff0c\u8ba1\u65f6\u5668\uff0c\u968f\u673a\u6570\u7b49\u591a\u79cd\u529f\u80fd\u3002\uff08\u5728\u4f7f\u7528C\u8bed\u8a00\u7f16\u5199stm32\u5b9e\u65f6\u65f6\u949f\u65f6\uff0c\u4e5f\u9700\u8981\u4f7f\u7528\u6b64\u51fd\u6570\u5e93\uff0c\u53ea\u4e0d\u8fc7C\u8bed\u8a00\u7528\u7684\u662f&lt;time.h&gt;\uff09<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u5e38\u7528\u7684\u51fd\u6570\uff08\u7b80\u8ff0\uff09<\/h2>\n\n\n\n<figure class=\"wp-block-table aligncenter\"><table class=\"has-fixed-layout\"><tbody><tr><td>\u529f\u80fd<\/td><td>\u51fd\u6570\/\u7c7b\u578b<\/td><td>\u4f5c\u7528<\/td><td>\u5e38\u89c1\u5199\u6cd5<\/td><\/tr><tr><td>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/td><td><strong><em>time()<\/em><\/strong><\/td><td>\u8fd4\u56de\u5f53\u524d\u65f6\u95f4\u6233\uff08\u79d2\uff09<\/td><td>time_t t = time(NULL)<\/td><\/tr><tr><td>\u65f6\u95f4\u7c7b\u578b<\/td><td><strong><em>time_t<\/em><\/strong><\/td><td>\u5b58\u50a8\u65f6\u95f4\u6233<\/td><td>time_t t<\/td><\/tr><tr><td>\u8f6c\u5b57\u7b26\u4e32\u65f6\u95f4<\/td><td><strong><em>ctime()<\/em><\/strong><\/td><td>\u65f6\u95f4\u6233-&gt;\u5b57\u7b26\u4e32<\/td><td>cout &lt;&lt; ctime(&amp;t)<\/td><\/tr><tr><td>\u8f6c\u672c\u5730\u65f6\u95f4\u7ed3\u6784<\/td><td><strong><em>loacltime()<\/em><\/strong><\/td><td>\u65f6\u95f4\u6233-&gt;tm\u7ed3\u6784\u4f53<\/td><td>tm *lt = localtime(&amp;t)<\/td><\/tr><tr><td>\u8f6cUTC\u65f6\u95f4<\/td><td><strong><em>gmtime()<\/em><\/strong><\/td><td>\u8f6c\u4e16\u754c\u65f6\u95f4<\/td><td>tm* gt = gmtime(&amp;t)<\/td><\/tr><tr><td>\u7ed3\u6784\u4f53\u8f6c\u65f6\u95f4\u6233<\/td><td><strong><em>mktime()<\/em><\/strong><\/td><td>tm -&gt; time_t<\/td><td>time_t = mktime(&amp;tm)<\/td><\/tr><tr><td>\u65f6\u95f4\u7ed3\u6784\u4f53<\/td><td><strong><em>tm<\/em><\/strong><\/td><td>\u5b58\u50a8\u5e74\u6708\u65e5\u65f6\u5206\u79d2<\/td><td>tm t<\/td><\/tr><tr><td>\u683c\u5f0f\u5316\u8f93\u51fa<\/td><td><strong><em>strftime()<\/em><\/strong><\/td><td>\u81ea\u5b9a\u4e49\u65f6\u95f4\u683c\u5f0f<\/td><td>strftime(buf, size,&#8221;%Y-%m-%d&#8221;,lt)<\/td><\/tr><tr><td>\u968f\u673a\u79cd\u5b50<\/td><td><strong><em>srand()<\/em><\/strong><\/td><td>\u8bbe\u8ba1\u968f\u673a\u79cd\u5b50<\/td><td>srand(time(NULL))<\/td><\/tr><tr><td>\u7b80\u5355\u8ba1\u65f6<\/td><td>\u5dee\u503c<\/td><td>\u8ba1\u7b97\u8017\u65f6<\/td><td>end-start<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">3.\u5177\u4f53\u63cf\u8ff0<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1.   \u65f6\u95f4\u6233 <\/h3>\n\n\n\n<p>    \u5e38\u89c1\u7684\u65f6\u95f4\u6233\u4e3aUnix\uff0c\u5c31\u662f\u5230\u76ee\u524d\u4e00\u5171\u6709\u591a\u5c11\u79d2\uff0c\u8d77\u70b9\uff1a<strong>1970\u5e741\u67081\u65e5 00:00:00\uff08<\/strong>UTC\uff09<\/p>\n\n\n\n<p>    \u4f8b\u5982 1774956732\u5c31\u8868\u793a\u4ece\u8d77\u70b9\u52302026-03-31 19:32:12\u4e00\u5171\u7528\u4e86\u591a\u5c11\u79d2\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.UTC<\/h3>\n\n\n\n<p>GMT\u548cUTC\u7684\u65f6\u95f4\u57fa\u672c\u76f8\u7b49\uff0c\u8bef\u5dee\u4e3a1\u79d2\u5185\u3002<\/p>\n\n\n\n<p>UTC \u80cc\u540e\u662f\u539f\u5b50\u949f GMT \u662f\u5730\u7403\u8f6c\u51fa\u6765\u7684,GMT(\u683c\u6797\u5c3c\u6cbb\u65f6\u95f4);::&lt;\u5468\u6770\u4f26\u4e0d\u662f\u6709\u9996\u6b4c\u91cc\u5199\u201c\u6211\u5360\u636e \u683c\u6797\u5a01\u6cbb \u5b88\u5019\u7740\u4f60 \u5728\u65f6\u95f4 \u6807\u51c6\u8d77\u70b9 \u56de\u5fc6\u8fc7\u53bb\u201d&gt;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.time_t <\/h3>\n\n\n\n<p>\u7528\u4e8e\u65f6\u95f4\u6233\u7684\u5b9a\u4e49\uff0c \u5176\u7c7b\u578b\u4e3along long\uff0c\u6ce8\u610f\u5982\u679c\u662f\u5f88\u5927\u7684\u65e5\u671f\u5c31\u4f1a\u51fa\u9519<\/p>\n\n\n\n<p>\u26a0\ufe0f\u26a0\ufe0f\u26a0\ufe0f\u5343\u4e07\u8981\u6ce8\u610f\uff1a\u5b9a\u4e49time_t\u65f6\u4e00\u5b9a\u8981\u521d\u59cb\u5316\uff08\u8fd9\u662f\u60e8\u75db\u7684\u6559\u8bad\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>time_t t = 1774956732;\ntime_t t1 = {};    \/\/\u521d\u59cb\u5316\uff1b<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4.tm<\/h3>\n\n\n\n<p>tm \u7528\u4e8e\u5b9a\u4e49\u65f6\u95f4\u7ed3\u6784\u4f53<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct tm {\n    int tm_year; \/\/ \u5e74\uff08\u4ece1900\u5f00\u59cb\uff09\n    int tm_mon;  \/\/ \u6708\uff080~11\uff09\n    int tm_mday; \/\/ \u65e5\n    int tm_hour; \/\/ \u65f6\n    int tm_min;  \/\/ \u5206\n    int tm_sec;  \/\/ \u79d2\n};\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5.time()<\/h3>\n\n\n\n<p>\u8fd4\u56de\u5f53\u524d\u65f6\u95f4\u6233<\/p>\n\n\n\n<p>\u8bed\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>time_t time(time_t* t);<\/code><\/pre>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;ctime&gt;\nusing namespace std;\n\nint main() {\n    time_t now = time(NULL);\n    cout &lt;&lt; now &lt;&lt; endl;    \/\/\u8fd4\u56de\u5f53\u524d\u65f6\u95f4\u6233\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6.ctime()<\/h3>\n\n\n\n<p>\u65f6\u95f4\u6233\u8f6c\u5b57\u7b26\u4e32<\/p>\n\n\n\n<p>\u8bed\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>char* ctime(const time_t* t);<\/code><\/pre>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;ctime&gt;\nusing namespace std;\n\nint main() {\n    time_t now = time(NULL);\n    cout &lt;&lt; ctime(&amp;now);    \/\/\u65f6\u95f4\u6233\u8f6c\u5b57\u7b26\u4e32\uff0c\u81ea\u52a8\u6362\u884c\n}                           \/\/ Tue Mar 31 19:53:29 2026<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7.localtime()<\/h3>\n\n\n\n<p>\u8fd4\u56de\u5f53\u5730\u65f6\u95f4, \u5b9e\u73b0time_t -> tm\u7684\u8f6c\u6362\uff1b<\/p>\n\n\n\n<p>\u8bed\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tm* localtime(const time_t* t);<\/code><\/pre>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream>\n#include &lt;ctime>\nusing namespace std;\n\nint main() {\n    time_t now = time(NULL);\n    tm* lt = localtime(&amp;now);\/\/\u8fd4\u56de\u5f53\u5730\u65f6\u95f4\n    \/\/\u6b64\u5916\u8be5\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0time_t -> tm\u7684\u8f6c\u6362\n    tm s1 = {};   \/\/\u4e00\u5b9a\u8981\u521d\u59cb\u5316\uff0c\u4e0d\u7136\u4f1a\u51fa\u9519\uff08\u4e0d\u4fe1\u7684\u53ef\u4ee5\u8bd5\u8bd5>_&lt;\uff09\n    s1.tm_year = 2026-1900;\n    s1.tm_mon = 1-1;\n    s1.tm_mday = 1;\n    time_t t1 = mktime(&amp;s1);\n    tm *s2 = localtime(&amp;t1);\n    cout &lt;&lt; s2->tm_year + 1900&lt;&lt; endl;\n\n    cout &lt;&lt; lt->tm_year + 1900 &lt;&lt; endl; \/\/ 2026\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">8.mktime<\/h3>\n\n\n\n<p>\u5c06tm\u8f6c\u5316\u4e3a\u65f6\u95f4\u6233<\/p>\n\n\n\n<p>\u8bed\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>time_t mktime(tm* t);\n<\/code><\/pre>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;ctime&gt;\nusing namespace std;\n\nint main() {\n    tm t = {};\n    t.tm_year = 2026 - 1900;\n    t.tm_mon = 3 - 1;\n    t.tm_mday = 31;\n\n    time_t ts = mktime(&amp;t);\n    cout &lt;&lt; ts &lt;&lt; endl;\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">9.strftime()<\/h2>\n\n\n\n<p>\u683c\u5f0f\u5316\u8f93\u51fa\u65f6\u95f4<\/p>\n\n\n\n<p>\u8bed\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>size_t strftime(char* str, size_t maxsize, const char* format, const tm* timeptr);\n<\/code><\/pre>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;ctime&gt;\nusing namespace std;\n\nint main() {\n    time_t now = time(NULL);\n    tm* lt = localtime(&amp;now);\n\n    char buf&#91;100];\n    strftime(buf, sizeof(buf), \"%Y %m %d %H:%M:%S\", lt);\/\/2026 03 31 20:02:23\n    strftime(buf, sizeof(buf), \"%-Y %-m %-d %H:%-M:%S\", lt);\/\/2026 3 31 20:2:49     \u5728\u683c\u5f0f\u5316\u5b57\u7b26\u524d\u52a0\u4e0a - \u5c31\u53ef\u4ee5\u5c0f\u533a\u524d\u5bfc\u96f6\n    cout &lt;&lt; buf &lt;&lt; endl\uff1b\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3\uff1a<\/h2>\n\n\n\n<p>   \u8fd9\u91cc\u53ea\u4ecb\u7ecd\u4e86\u4e00\u90e8\u5206\u5e38\u7528\u7684\u51fd\u6570\uff0c\u4f46\u662f\u5982\u679c\u9762\u5bf9\u5f88\u5927\u7684\u51fd\u6570\uff0c\u90a3\u4e48&lt;ctime&gt;\u5c31\u4e0d\u9002\u7528\u4e86\uff0c\u6240\u4ee5\u6709\u4e9b\u65f6\u5019\u8fd8\u662f\u8981\u81ea\u5df1\u5199\u7684\u3002\uff08\u5982\u6709\u9519\u8bef\uff0c\u6b22\u8fce\u6307\u6b63\uff09<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u524d\u8a00 &lt;ctime&gt;\u51fd\u6570\u5e93\u4e2d\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6709\u5173\u65f6\u95f4\u7684\u51fd\u6570\uff0c\u4f7f\u7528\u6b64\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u65e5\u671f\uff0c\u8ba1\u65f6\u5668\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":81,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,8],"tags":[23],"class_list":["post-80","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c","category-text","tag-c-2"],"_links":{"self":[{"href":"https:\/\/blog.kiloming.top\/index.php?rest_route=\/wp\/v2\/posts\/80","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kiloming.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kiloming.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kiloming.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kiloming.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=80"}],"version-history":[{"count":2,"href":"https:\/\/blog.kiloming.top\/index.php?rest_route=\/wp\/v2\/posts\/80\/revisions"}],"predecessor-version":[{"id":89,"href":"https:\/\/blog.kiloming.top\/index.php?rest_route=\/wp\/v2\/posts\/80\/revisions\/89"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.kiloming.top\/index.php?rest_route=\/wp\/v2\/media\/81"}],"wp:attachment":[{"href":"https:\/\/blog.kiloming.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=80"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kiloming.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=80"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kiloming.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=80"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}