{"id":137,"date":"2021-07-02T13:51:46","date_gmt":"2021-07-02T05:51:46","guid":{"rendered":"https:\/\/aisumura.net\/blog\/?p=137"},"modified":"2021-07-02T13:51:46","modified_gmt":"2021-07-02T05:51:46","slug":"c%e8%aa%9e%e8%a8%80%e6%89%8b%e5%88%bbunix-timestamp","status":"publish","type":"post","link":"https:\/\/aisumura.net\/blog\/2021\/07\/02\/c%e8%aa%9e%e8%a8%80%e6%89%8b%e5%88%bbunix-timestamp\/","title":{"rendered":"C\u8a9e\u8a00\u624b\u523bunix timestamp"},"content":{"rendered":"\n<p>\u4eca\u5929\u7a81\u7136\u88abRD\u4e3b\u7ba1\u6293\u53bb\u8a0e\u8ad6MCU\u901a\u8a0a\u5982\u4f55\u6e1b\u5c11\u7121\u7528\u7a7a\u9593\u7684\u554f\u984c\uff0c<br>\u7531\u65bc\u6642\u9593\u8cc7\u6599\u662f\u7531RTC(Real-Time Clock)\u6a21\u7d44\u56de\u50b3\u7684data\uff0c<br>\u683c\u5f0f\u662fyyyy-mm-dd hh:MM:ss\uff0c<br>yyyy\u5206\u62102\u500bbytes\uff0c\u5176\u9918\u5404\u4f541\u500bbytes\uff0c\u51717 bytes\uff0c<br>\u7d93\u8a0e\u8ad6\u5f8c\u5e0c\u671b\u7528\u4e00\u500blong\u5132\u5b58unix timestamp\uff0c<br>\u572832bits MCU\u7684\u74b0\u5883\u4e0b\u5c31\u53ea\u9808\u4f54\u75284 bytes\uff0c<br>\u56e0\u70baMCU\u4e0d\u652f\u63f4time.h\u4e4b\u985e\u7684lib\uff0c<br>\u53ea\u597d\u81ea\u5df1\u624b\u523b\u4e00\u500b\u9806\u4fbf\u8a18\u9304\u4e0b\u4f86\u3002<\/p>\n\n\n\n<h4>\u601d\u8def<\/h4>\n\n\n\n<p>Unix timestamp\u7684\u5b9a\u7fa9\u70ba\u81ea1970\/01\/01 00:00:00 \u4ee5\u4f86\u7684\u79d2\u6578\uff0c<br>\u7b2c\u4e00\u500b\u53cd\u61c9\u662f\u6703\u9047\u5230\u958f\u5e74\u554f\u984c\uff0c<br>\u7136\u5f8c\u601d\u8003\u4e86\u4e00\u4e0b\uff0c\u7531\u65bc\u5b9a\u7fa9\u7684\u6642\u9593\u662f\u57281\u67081\u65e5\u76840\u66420\u79d2\uff0c<br>\u6240\u4ee5\u76f8\u6e1b\u4e0d\u9700\u8981\u505a\u4efb\u4f55\u501f\u4f4d\uff0c<br>\u53ea\u9700\u5c07\u904e\u53bb\u7684\u7e3d\u5929\u6578\u52a0\u7e3d\uff0c\u52a0\u4e0a\u7576\u5929\u7684\u6642\u3001\u5206\u3001\u79d2\uff0c<br>\u5c31\u6703\u5f97\u5230\u5b8c\u6574\u7684\u6642\u9593\u5dee\uff0c<br>\u7136\u5f8c\u7e3d\u5929\u6578\u7684\u7372\u53d6\u53c8\u5206\u70ba\u5e74\u8ddf\u6708\uff0c<br>\u91dd\u5c0d\u958f\u5e74\u7684\u90e8\u4efd\u505a\u76f8\u5c0d\u61c9\u7684\u8655\u7406(\u958f\u5e741\u5e74\u6709366\u5929\uff0c2\u6708\u670929\u65e5)\uff0c<br>\u6700\u5f8c\u5c0d\u6642\u5340\u9032\u884c\u4fee\u6b63\u5c31\u5b8c\u6210\u4e86\u3002<\/p>\n\n\n\n<h4>Code<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"c\" class=\"language-c\">#include&lt;stdio.h>\n#include&lt;stdlib.h>\n\nu_long datetime_to_timestamp(int year, int month, int day, int hour, int minute, int second){\n    u_long result;\n    int passed_days = 0;\n    int month_array[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};\n\n    \/\/\u8a72\u6708\u4ee5\u524d\u7684\u5929\u6578\n    for(int i = 1; i &lt; month; i++){\n        passed_days += month_array[i];\n        \/\/\u958f\u5e74\u7684\u4e8c\u6708\u591a\u4e00\u5929\n        if( i == 2 &amp;&amp; (year%400==0||(year%4==0 &amp;&amp; year%100!=0)) ){\n            passed_days += 1;\n        }\n    }\n\n    \/\/\u8a72\u5e74\u4ee5\u524d\u7684\u5929\u6578\n    while(--year >= 1970){\n        if( year%400==0 || ( year%4==0 &amp;&amp; year%100!=0 ) )\n            passed_days += 366;\n        else\n            passed_days += 365;\n    }\n\n    \/\/(\u8a72\u67081\u865f\u70ba\u6b62\u7684\u7e3d\u5929\u6578 + \u7576\u5929 - 1\u865f)\u8f49\u79d2 + \u6642\u8f49\u79d2 + \u5206\u8f49\u79d2 + \u79d2\n    \/\/28800\u662f\u53f0\u7063\u6642\u9593UTC+8\u4fee\u6b63\u70baUTC+0\n    result = (passed_days + day - 1)*24*3600 + hour*3600 + minute*60 + second - 28800;\n    return result;\n}\n\nint main(){\n    printf(\"%u\\n\", datetime_to_timestamp(2040,1,7,0,0,0));\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u7a81\u7136\u88abRD\u4e3b\u7ba1\u6293\u53bb\u8a0e\u8ad6MCU\u901a\u8a0a\u5982\u4f55\u6e1b\u5c11\u7121\u7528\u7a7a\u9593\u7684\u554f\u984c\uff0c\u7531\u65bc\u6642\u9593\u8cc7\u6599\u662f\u7531RTC(Real-Time Clo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12],"tags":[14],"_links":{"self":[{"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/posts\/137"}],"collection":[{"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/comments?post=137"}],"version-history":[{"count":2,"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/posts\/137\/revisions"}],"predecessor-version":[{"id":140,"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/posts\/137\/revisions\/140"}],"wp:attachment":[{"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/media?parent=137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/categories?post=137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/tags?post=137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}