Note that there are some explanatory texts on larger screens.

plurals
  1. POmktime shows inconsistent output
    primarykey
    data
    text
    <p>While trying to write code which returns 24 hours less than a given time, <code>mktime()</code>shows inconsistent output. I calculate it similar to this: <code>current_time(GMT) - 86400</code> which should return the right value. All we need to do is calculate based on the input time; we used <code>mktime()</code> to change the time and get the GMT time and then do the regular calculation. I included my code below.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;time.h&gt; int main() { time_t currentTime, tempTime; struct tm *localTime; time(&amp;currentTime); //localTime = localtime(&amp;currentTime); localTime = gmtime(&amp;currentTime); //get the time in GMT as we are in PDT printf("Time %2d:%02d\n", (localTime-&gt;tm_hour)%24, localTime-&gt;tm_min); localTime-&gt;tm_hour = 19; // Set the time to 19:00 GMT localTime-&gt;tm_min = 0; localTime-&gt;tm_sec = 0; tempTime = mktime(localTime); //tempTime = mktime(localTime) - timezone; printf("Current time is %ld and day before time is %ld\n", currentTime, (currentTime - 86400)); printf("Current timezone is %ld \n", timezone); printf("New time is %ld and day before time is %ld\n",tempTime, (tempTime - 86400)); } </code></pre> <p>But when we check the output it is coming back incorrect after the call to call <code>mktime()</code>. Below is the output of above program.</p> <pre><code>$ ./a.out Time 11:51 Current time is 1341229916 and day before time is 1341143516 New time is 1341284400 and day before time is 1341198000 $ ./print_gmt 1341229916 Mon Jul 2 11:51:56 2012 $ ./print_gmt 1341143516 Sun Jul 1 11:51:56 2012 $ ./print_gmt 1341284400 Tue Jul 3 03:00:00 2012 $ ./print_gmt 1341198000 Mon Jul 2 03:00:00 2012 $ date Mon Jul 2 04:52:46 PDT 2012 </code></pre> <p>Now if we un-comment the line which subtracts the timezone (present in time.h), then the output is as expected. Below is the value for timezone in above program</p> <pre><code>$ ./a.out . . . Current timezone is 28800 . . . </code></pre> <p>So why is there such inconsistent behavior for <code>mktime()</code> although the man pages do not mention such adjustment of the timezone. Is there something we are missing while doing such conversions?</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload