{"id":4760,"date":"2022-02-21T22:07:30","date_gmt":"2022-02-21T13:07:30","guid":{"rendered":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4760"},"modified":"2025-04-01T16:03:14","modified_gmt":"2025-04-01T07:03:14","slug":"c-time-difftime-localtime","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4760","title":{"rendered":"C &#8211; time, difftime, localtime, gmtime, gettimeofday"},"content":{"rendered":"<h1>C &#8211; time, difftime, localtime, gmtime, gettimeofday<\/h1>\n<p>. . . . . . . . . . . . . . . . . . . . . . . . . . .<br \/>\n. . . . . . . . . . . . . . . . . . . . . . . . . . .<br \/>\n. . . . . . . . . . . . . . . . . . . . . . . . . . .<\/p>\n<h2>time<\/h2>\n<p><code>time()<\/code> \ud568\uc218\ub294 \ud604\uc7ac \uc2dc\uac04\uc744 <code>1970-01-01 00:00:00 UTC<\/code> \ub85c\ubd80\ud130 \uacbd\uacfc\ub41c \ucd08(second) \ub97c \ubc18\ud658\ud55c\ub2e4.<\/p>\n<p>\ub9ac\ud134\uac12\uc73c\ub85c\ub3c4 \ubc18\ud658\ud558\uace0, \ub3d9\uc2dc\uc5d0 \ucc38\uc870\ub85c \uc804\ub2ec\ud55c \ud30c\ub77c\ubbf8\ud130\uc5d0\ub3c4 \uc785\ub825\ud574 \uc900\ub2e4.<\/p>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;time.h&gt;\n\nint main() {\n    time_t seconds;\n\n    printf(&quot;since 1970-01-01 00:00:00 : %ld (sec)\\n&quot;, time(&amp;seconds));\n    printf(&quot;seconds: %ld\\n&quot;, seconds);\n\n    return 0;\n}<\/code><\/pre>\n<h2>difftime<\/h2>\n<p>\ub450 \uc2dc\uac04\uc758 \ucc28\uc774\uac12\uc744 \ubc18\ud658\ud55c\ub2e4.(seconds)<br \/>\nwindow \uc5d0\ub294 Sleep() \ud568\uc218\ub97c \uc368\uc57c\ud55c\ub2e4.<\/p>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;time.h&gt;\n#include &lt;unistd.h&gt;\n\nint main() {\n    time_t start, end;\n\n    time(&amp;start);\n    sleep(3);\n    time(&amp;end);\n\n    printf(&quot;time diff%.2lf (sec)\\n&quot;, difftime(end, start));\n\n    return 0;\n}<\/code><\/pre>\n<h2>localtime, gmtime, localtime_r<\/h2>\n<pre><code class=\"language-c\">struct tm\n{\n    int tm_sec;   \/\/ seconds after the minute - [0, 60] including leap second\n    int tm_min;   \/\/ minutes after the hour - [0, 59]\n    int tm_hour;  \/\/ hours since midnight - [0, 23]\n    int tm_mday;  \/\/ day of the month - [1, 31]\n    int tm_mon;   \/\/ months since January - [0, 11]\n    int tm_year;  \/\/ years since 1900\n    int tm_wday;  \/\/ days since Sunday - [0, 6]\n    int tm_yday;  \/\/ days since January 1 - [0, 365]\n    int tm_isdst; \/\/ daylight savings time flag\n};<\/code><\/pre>\n<p>\ud568\uc218\uba85 <code>localtime()<\/code> \uc5d0 \ub9de\uac8c UTC \uac00 \uc544\ub2cc \uc9c0\uc5ed\uc2dc\uac04(KST) \ub85c \uc2dc\uac04\uc744 \ubc18\ud658\ud55c\ub2e4.<br \/>\nUTC \uac00 \ud544\uc694\ud55c \uacbd\uc6b0 <code>gmtime()<\/code> \uc744 \uc0ac\uc6a9\ud55c\ub2e4.<\/p>\n<p>\uc5f0\ub3c4\ub294 1900\ub144\ub3c4\ubd80\ud130 \uacbd\uacfc\ub41c \ub144\ub3c4\ub97c \ubc18\ud658\ud558\ubbc0\ub85c 1900 \uc744 \ub354\ud574 \uc8fc\uc5b4\uc57c \ud55c\ub2e4.<br \/>\n\uc6d4 \ub610\ud55c 0\uc6d4\ubd80\ud130 \uc2dc\uc791\ud558\ubbc0\ub85c 1\uc744 \ub354\ud574 \uc8fc\uc5b4\uc57c \ud55c\ub2e4.<\/p>\n<p>\uba40\ud2f0\uc2a4\ub808\ub4dc \ud658\uacbd\uc5d0\uc11c\ub294 localtime_r() \uc744 \uc0ac\uc6a9\ud55c\ub2e4.<\/p>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;time.h&gt;\n\nint main() {\n    time_t now;\n    struct tm *t_now, t_now_multithread;\n\n    time(&amp;now);\n    t_now = (struct tm *) localtime(&amp;now);\n\n    printf(&quot;year : %d, month : %d, day : %d, hour : %d\\n&quot;, (t_now-&gt;tm_year + 1900), (t_now-&gt;tm_mon + 1), t_now-&gt;tm_mday, t_now-&gt;tm_hour);\n\n    localtime_r(&amp;now, &amp;t_now_multithread);\n    printf(&quot;year : %d, month : %d, day : %d, hour : %d\\n&quot;, (t_now_multithread.tm_year + 1900), (t_now_multithread.tm_mon + 1), t_now_multithread.tm_mday, t_now_multithread.tm_hour);\n\n    return 0;\n}<\/code><\/pre>\n<h2>localtime \uc624\ub958<\/h2>\n<p>\uba40\ud2f0\uc2a4\ub808\ub4dc \ud658\uacbd\uc774 \uc544\ub2c8\ub354\ub77c\ub3c4, \ubcc0\uc218\ub97c \ub450\uac1c \uc774\uc0c1 \ub3d9\uc2dc\uc5d0 \uc0ac\uc6a9\ud558\ub824\uba74 localtime_r() \uc744 \uc0ac\uc6a9\ud574\uc57c \ud55c\ub2e4.<\/p>\n<pre><code class=\"language-c\">char start_dt_str[30];\nchar end_dt_str[30];\nstruct tm *start_dt_tm_info = localtime(&amp;client_list[i].start_dt);\nstruct tm *end_dt_tm_info = localtime(&amp;client_list[i].end_dt);\nstrftime(start_dt_str, sizeof(start_dt_str), &quot;%Y-%m-%d %H:%M:%S&quot;, start_dt_tm_info);\nstrftime(end_dt_str, sizeof(end_dt_str), &quot;%Y-%m-%d %H:%M:%S&quot;, end_dt_tm_info);\nprintf(&quot;%s ~ %s\\n&quot;, start_dt_str, end_dt_str);<\/code><\/pre>\n<p>localtime \uc740 \ud558\ub098\uc758 \ubcc0\uc218\ub85c \uac12\uc744 \ubc18\ud658\ud558\ubbc0\ub85c \ub450\uac1c\uc758 \ud3ec\uc778\ud130\ub294 \ub3d9\uc77c\ud55c \ub370\uc774\ud0c0\ub97c \ubc14\ub77c\ubcf4\uac8c \ub41c\ub2e4.<\/p>\n<pre><code class=\"language-bash\">2025-04-01 15:28:13 ~ 2025-04-01 15:28:13<\/code><\/pre>\n<pre><code class=\"language-bash\">char start_dt_str[30];\nchar end_dt_str[30];\nstruct tm start_dt_tm_info;\nstruct tm end_dt_tm_info;\n\n\/\/ localtime_r \uc0ac\uc6a9 (\uc2a4\ub808\ub4dc \uc548\uc804 \ubc84\uc804)\nlocaltime_r(&amp;client_list[i].start_dt, &amp;start_dt_tm_info);\nlocaltime_r(&amp;client_list[i].end_dt, &amp;end_dt_tm_info);\n\nstrftime(start_dt_str, sizeof(start_dt_str), &quot;%Y-%m-%d %H:%M:%S&quot;, &amp;start_dt_tm_info);\nstrftime(end_dt_str, sizeof(end_dt_str), &quot;%Y-%m-%d %H:%M:%S&quot;, &amp;end_dt_tm_info);\nprintf(&quot;%s ~ %s\\n&quot;, start_dt_str, end_dt_str);<\/code><\/pre>\n<h2>gettimeofday (milisecond \uad6c\ud558\uae30)<\/h2>\n<p>window \uc5d0\ub294 gettimeofday() \ud568\uc218\uac00 \uc5c6\ub2e4.<\/p>\n<p>gettimeofday() \uc758 \ub450\ubc88\uc9f8 \ud30c\ub77c\ubbf8\ud130\ub294 \ubb34\uc2dc\ub41c\ub2e4.<br \/>\n\uc989, NULL \uc774\uc678\uc758 \ub2e4\ub978 \uac12\uc774 \uc785\ub825\ub418\uc5b4\ub3c4 \uc791\ub3d9\ud558\uc9c0 \uc54a\ub294\ub2e4.<\/p>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;time.h&gt;\n#include &lt;sys\/time.h&gt;\n\nint main() {\n    struct timeval now;\n    struct tm *t_now;\n\n    gettimeofday(&amp;now, NULL);\n    t_now = localtime(&amp;now.tv_sec);\n    float miliseconds = t_now-&gt;tm_sec + now.tv_usec \/ 1000000.0;\n\n    printf(&quot;miliseconds : %0.3f\\n&quot;, miliseconds);\n\n    return 0;\n}<\/code><\/pre>\n<h2>strptime (\ubb38\uc790\uc5f4\uc5d0\uc11c \uc2dc\uac04 \uad6c\ud558\uae30)<\/h2>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;time.h&gt;\n#include &lt;sys\/time.h&gt;\n\nint main() {\n    struct tm api_tm;\n    strptime(&quot;2022-03-25 14:42:24&quot;, &quot;%Y-%m-%d %H:%M:%S&quot;, &amp;api_tm);\n\n    return 0;\n}<\/code><\/pre>\n<h2>struct tm to time_t<\/h2>\n<p><strong><font color=red>\ucd08\uae30\ud654\ub97c \uc548\ud574\uc8fc\uba74 \uc624\uc791\ub3d9\ud560 \uc218 \uc788\ub2e4.<br \/>\n\uac12\uc744 \uc124\uc815\ud558\uc9c0 \uc54a\uc740 \ubaa8\ub4e0 \ud544\ub4dc\ub294 \ub79c\ub364\uac12\uc774 \ud560\ub2f9\ub418\uace0,<br \/>\n\uadf8\ub85c\uc778\ud574 mktime() \uc774 -1 \uc744 return \ud560 \uc218 \uc788\ub2e4.<\/font><\/strong><\/p>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;time.h&gt;\n#include &lt;sys\/time.h&gt;\n\nint main() {\n    struct tm api_tm = {0}; \/\/ \ucd08\uac00\ud654\ud558\ub294 \uac83\uc774 \uc911\uc694\n    strptime(&quot;2022-03-25 14:42:24&quot;, &quot;%Y-%m-%d %H:%M:%S&quot;, &amp;api_tm);\n\n    \/\/ localtime \uc744 \uae30\uc900\uc73c\ub85c \ubcc0\ud658\ud569\ub2c8\ub2e4.\n    time_t api_t = mktime(&amp;api_tm);\n\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C &#8211; time, difftime, localtime, gmtime, gettimeofday . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4760\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-4760","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4760","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4760"}],"version-history":[{"count":22,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4760\/revisions"}],"predecessor-version":[{"id":10311,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4760\/revisions\/10311"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}