{"id":52,"date":"2017-07-29T23:11:52","date_gmt":"2017-07-29T17:11:52","guid":{"rendered":"https:\/\/qtvz.com\/?p=52"},"modified":"2017-09-11T21:30:51","modified_gmt":"2017-09-11T15:30:51","slug":"c%e6%97%b6%e9%97%b4%e6%a0%87%e5%87%86%e5%ba%93%e6%97%b6%e9%97%b4time%e5%92%8c%e7%b3%bb%e7%bb%9f%e6%97%b6%e9%97%b4%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/qtvz.com\/52.html","title":{"rendered":"C++\u65f6\u95f4\u6807\u51c6\u5e93\u65f6\u95f4time\u548c\u7cfb\u7edf\u65f6\u95f4\u7684\u4f7f\u7528"},"content":{"rendered":"<!--wp-compress-html--><!--wp-compress-html no compression--><p>1. C++\u6807\u51c6\u5e93\u4e2d\u7684\u65f6\u95f4\u9700\u8981\u5f15\u7528time.h\uff0c\u53ef\u4ee5\u53d6\u7684\u672c\u5730\u65f6\u95f4\u6216\u8005\u683c\u6797\u5a01\u6cbb\u65f6\u95f4\uff0c\u53ea\u80fd\u7cbe\u786e\u5230\u79d2<\/p>\n<pre class=\"lang:c++ decode:true\">#include &lt;iostream&gt;\r\n \r\n\/*\u5305\u542btime\u5934\u6587\u4ef6*\/\r\n#include &lt;time.h&gt;\r\n \r\n \r\nusing namespace std;\r\n \r\nint main()\r\n{\r\n    \/\/time_t\u662flong\u7c7b\u578b\uff0c\u7cbe\u786e\u5230\u79d2\uff0c\u662f\u5f53\u524d\u65f6\u95f4\u548c1970\u5e741\u67081\u65e5\u96f6\u70b9\u65f6\u95f4\u7684\u5dee\r\n    const time_t t = time(NULL);\r\n \r\n    cout&lt;&lt;\"current time is \"&lt;&lt;t&lt;&lt;endl;\r\n \r\n    \/*\u672c\u5730\u65f6\u95f4\uff1a\u65e5\u671f\uff0c\u65f6\u95f4 \u5e74\u6708\u65e5\uff0c\u661f\u671f\uff0c\u65f6\u5206\u79d2*\/\r\n    struct tm* current_time = localtime(&amp;t);\r\n    printf(\"current year is %d;current month is %d;current date of month is %d\\r\\n\",\r\n        1900 + current_time-&gt;tm_year,\r\n        1 + current_time-&gt;tm_mon\/*\u6b64month\u7684\u8303\u56f4\u4e3a0-11*\/,\r\n        current_time-&gt;tm_mday);\r\n \r\n    printf(\"current day of year is %d;current day in week is %d\\r\\n\",\r\n        current_time-&gt;tm_yday,\/*\u5f53\u524d\u65e5\u671f\u662f\u4eca\u5e74\u7684\u7b2c\u591a\u5c11\u5929[0,365] *\/\r\n        current_time-&gt;tm_wday\/*days since Sunday - [0,6] *\/);\r\n \r\n    printf(\"time part %d:%d:%d \\r\\n\",\r\n        current_time-&gt;tm_hour,\r\n        current_time-&gt;tm_min,\r\n        current_time-&gt;tm_sec);\r\n \r\n    printf(\"\\t\u672c\u5730\u65f6\u95f4\uff1a%d-%d-%d %d:%d:%d\\r\\n\",\r\n        current_time-&gt;tm_year + 1900,\r\n        current_time-&gt;tm_mon + 1,\r\n        current_time-&gt;tm_mday,\r\n        current_time-&gt;tm_hour,\r\n        current_time-&gt;tm_min,\r\n        current_time-&gt;tm_sec);\r\n \r\n    \/*\u683c\u6797\u5a01\u6cbb\u65f6\u95f4*\/\r\n    struct tm* current_gmtime = gmtime(&amp;t);\r\n \r\n    printf(\"\u683c\u6797\u5a01\u6cbb\u65f6\u95f4\uff1a%d-%d-%d %d:%d:%d\\r\\n\",\r\n        current_gmtime-&gt;tm_year + 1900,\r\n        current_gmtime-&gt;tm_mon + 1,\r\n        current_gmtime-&gt;tm_mday,\r\n        current_gmtime-&gt;tm_hour,\r\n        current_gmtime-&gt;tm_min,\r\n        current_gmtime-&gt;tm_sec);\r\n \r\n \r\n    system(\"pause\");\r\n    return 0;\r\n}\r\n2. \u7cfb\u7edf\u65f6\u95f4 SYSTEMTIME \u4f7f\u7528\u65f6\u8981\u5f15\u7528windows.h\uff0c\u53ef\u4ee5\u7cbe\u786e\u5230\u6beb\u79d2\u7ea7\u522b\r\n\r\n#include &lt;iostream&gt;\r\n#include &lt;Windows.h&gt;\r\n \r\n \r\nint main(){\r\n    \/\/\u58f0\u660e\u53d8\u91cf\r\n    SYSTEMTIME sys_time;\r\n \r\n    \/\/\u5c06\u53d8\u91cf\u503c\u8bbe\u7f6e\u4e3a\u672c\u5730\u65f6\u95f4\r\n    GetLocalTime( &amp;sys_time );\r\n \r\n    \/\/\u8f93\u51fa\u65f6\u95f4\r\n    printf( \"%4d\/%02d\/%02d %02d:%02d:%02d.%03d \u661f\u671f%1d\\n\",sys_time.wYear,\r\n        sys_time.wMonth,\r\n        sys_time.wDay,\r\n        sys_time.wHour,\r\n        sys_time.wMinute, \r\n        sys_time.wSecond,\r\n        sys_time.wMilliseconds,\r\n        sys_time.wDayOfWeek);\r\n \r\n    system(\"time\");\r\n \r\n    system(\"pause\");\r\n    return 0;\r\n}<\/pre>\n<p>\u539f\u6587\uff1a<a href=\"https:\/\/qtvz.com\/redirect\/aHR0cDovL3d3dy5jbmJsb2dzLmNvbS95dWthaXpoYW8vYXJjaGl2ZS8yMDExLzA0LzI5L2NwcF90aW1lX3N5c3RlbV90aW1lLmh0bWw=\" target=\"_blank\">http:\/\/www.cnblogs.com\/yukaizhao\/archive\/2011\/04\/29\/cpp_time_system_time.html<\/a><\/p>\n<!--wp-compress-html no compression--><!--wp-compress-html-->","protected":false},"excerpt":{"rendered":"1. C++\u6807\u51c6\u5e93\u4e2d\u7684\u65f6\u95f4\u9700\u8981\u5f15\u7528time.h\uff0c\u53ef\u4ee5\u53d6\u7684\u672c\u5730\u65f6\u95f4\u6216\u8005\u683c\u6797\u5a01\u6cbb\u65f6\u95f4\uff0c\u53ea\u80fd\u7cbe\u786e\u5230\u79d2 #include &lt;iostream&gt; \/*\u5305\u542btime\u5934\u6587\u4ef6*\/ #include &#038;lt \u00b7\u00b7\u00b7","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12],"tags":[23],"views":8819,"_links":{"self":[{"href":"https:\/\/qtvz.com\/api\/wp\/v2\/posts\/52"}],"collection":[{"href":"https:\/\/qtvz.com\/api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qtvz.com\/api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/comments?post=52"}],"version-history":[{"count":2,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/posts\/52\/revisions"}],"predecessor-version":[{"id":54,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/posts\/52\/revisions\/54"}],"wp:attachment":[{"href":"https:\/\/qtvz.com\/api\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}