{"id":637,"date":"2024-06-06T18:54:24","date_gmt":"2024-06-06T10:54:24","guid":{"rendered":"http:\/\/www.madbull.site\/?p=637"},"modified":"2024-12-13T22:33:40","modified_gmt":"2024-12-13T14:33:40","slug":"%e6%97%b6%e9%97%b4%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=637","title":{"rendered":"time\u65f6\u95f4\u5904\u7406"},"content":{"rendered":"\n<p><strong>linux\u73af\u5883\u4e0b\uff0c\u5bf9\u65f6\u95f4\u5904\u7406<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ gcc tm.c\n\n#include &lt;time.h&gt;\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n\n\n\/*\n * \u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u65f6\u95f4\u6233\n * \u8f93\u5165\u683c\u5f0f : 2024-06-06 18:38:20\n *\/\ntime_t string_to_timestamp(char * time_str) {\n    struct tm tm_info = {0} ;\n    if( sscanf( time_str, \"%4d-%2d-%2d %2d:%2d:%2d\", &amp;tm_info.tm_year, &amp;tm_info.tm_mon, &amp;tm_info.tm_mday, &amp;tm_info.tm_hour, &amp;tm_info.tm_min, &amp;tm_info.tm_sec ) != 6 ) {\n        return (time_t) -1;\n    }\n    tm_info.tm_year -= 1900 ;\n    tm_info.tm_mon -= 1 ;\n\n    time_t timestamp = <strong><mark style=\"background-color:#b1c5a4\" class=\"has-inline-color\">mktime<\/mark><\/strong>(&amp;tm_info) ;\n\n    return timestamp ;\n}\n\n\n\/*\n * \u65f6\u95f4\u6233\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\n * \u8f93\u51fa\u683c\u5f0f : 2024-06-06 18:38:20\n *\/\nint timestamp_to_string(char * date_s, int size, time_t itm) {\n    struct tm * timeinfo ;\n\n    \/\/ localtime \u548c localtime_r \u90fd\u53ef\u4ee5\u5904\u7406\n    \/\/ timeinfo = <strong><mark style=\"background-color:#b1c5a4\" class=\"has-inline-color\">localtime<\/mark><\/strong>(&amp;itm) ;  \/\/ \u8fd4\u56de\u5168\u5c40\u53d8\u91cf\uff0c\u5e76\u53d1\u9700\u8981\u4e0a\u9501\n    \/\/ <strong><mark style=\"background-color:#b1c5a4\" class=\"has-inline-color\">strftime<\/mark><\/strong>(date_s, size, \"%Y-%m-%d %H:%M:%S\", timeinfo) ;\n\n    struct tm tm_result = {0};\n    timeinfo = <strong><mark style=\"background-color:#b1c5a4\" class=\"has-inline-color\">localtime_r<\/mark><\/strong>(&amp;itm, &amp;tm_result);  \/\/ \u8fd4\u56de\u5168\u5c40\u53d8\u91cf\uff0c\u5e76\u53d1\u9700\u8981\u4e0a\u9501\n    <strong><mark style=\"background-color:#b1c5a4\" class=\"has-inline-color\">strftime<\/mark><\/strong>(date_s, size, \"%Y-%m-%d %H:%M:%S\", timeinfo) ;\n\n    return 0 ;\n}\n\nint main() {\n    char local_time&#91;30] ;\n\n    time_t cur_t = <strong><mark style=\"background-color:#b1c5a4\" class=\"has-inline-color\">time<\/mark><\/strong>(NULL);\n    memset(local_time, 0x00, sizeof(local_time)) ;\n\n    timestamp_to_string(local_time, sizeof(local_time), cur_t) ;\n    printf(\"now is &#91; %s ]\\n\", local_time) ;\n\n    time_t rslt_tm = string_to_timestamp(local_time) ;\n    printf(\"rslt_tm &#91; %ld ]\\n\", rslt_tm ) ;\n\n    return 0 ;\n}\n\n<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"301\" height=\"135\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/06\/123-1.png\" alt=\"\" class=\"wp-image-651\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u83b7\u53d6\u7cfb\u7edf\u5f53\u524d\u65f6\u95f4\u3001\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u65f6\u95f4\u6233\u3001\u8f93\u5165\u683c\u5f0f : 2024-06-06 18:38:20<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-with-sidebar","format":"standard","meta":{"footnotes":""},"categories":[156,154],"tags":[175,237,242,241,240,243,239,238],"class_list":["post-637","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-c","category-154","tag-c","tag-linux","tag-mktime","tag-strftime","tag-struct-tm","tag-time","tag-time_t","tag-238"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=637"}],"version-history":[{"count":9,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/637\/revisions"}],"predecessor-version":[{"id":1932,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/637\/revisions\/1932"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}