Note that there are some explanatory texts on larger screens.

plurals
  1. POgetting utc timestamp using strftime()
    primarykey
    data
    text
    <p>I am trying to encode current utc time into string using the <code>strftime</code>function:</p> <pre><code>time_t now; struct tm nowLocal; struct tm nowUtc; now = time(NULL); localtime_r(&amp;now, &amp;nowLocal); gmtime_r(&amp;now, &amp;nowUtc); </code></pre> <p>So far so good: <code>nowLocal</code> contains current time in my timezone (CET), <code>nowUtc</code> contains utc time, the difference is exactly according to the <code>tm_gmtoff</code> value:</p> <pre><code>nowLocal: {tm_sec = 28, tm_min = 27, tm_hour = 13, tm_mday = 23, tm_mon = 4, tm_year = 112, tm_wday = 3, tm_yday = 143, tm_isdst = 1, tm_gmtoff = 7200, tm_zone = 0x8127a38 "CEST"} nowUtc: {tm_sec = 28, tm_min = 27, tm_hour = 11, tm_mday = 23, tm_mon = 4, tm_year = 112, tm_wday = 3, tm_yday = 143, tm_isdst = 0, tm_gmtoff = 0, tm_zone = 0x3e9907 "GMT"} </code></pre> <p>Then I call <code>strftime()</code> with <code>"%s"</code> format to get the seconds since epoch:</p> <pre><code>char tsFromLocal[32]; char tsFromUtc[32]; strftime(tsFromLocal, sizeof(tsFromLocal), "%s", &amp;nowLocal); strftime(tsFromUtc, sizeof(tsFromUtc), "%s", &amp;nowUtc); </code></pre> <p>The result seems strange to me. I expected to get exactly the same string from both <code>strftime()</code> calls as <code>%s</code> format is described as:</p> <p>The number of seconds since the epoch, i.e., since <code>1970-01-01 00:00:00 UTC</code>. Leap seconds are not counted unless leap second support is available. </p> <p>But I got two different values:</p> <pre><code>tsFromLocal:"1337772448" tsFromUtc: "1337768848" </code></pre> <p>and moreover the difference is not <strong><em>7200</em></strong> (<code>tm_gmtoff</code>) but <strong><em>3600</em></strong>. Can anyone explain such behavior? Or is it a bug?</p> <p>The reason why I am doing this is that I need to transfer the time value over the network and compare it to the current time on the target machine that can be in different time zone. On target machine I wanted to:</p> <pre><code>struct tm restoredUtc; time_t restored; strptime(tsFromUtc, "%s", &amp;restoredUtc); restored = timegm(&amp;restoredUtc); </code></pre> <p>But I got:</p> <pre><code>restoredUtc:{tm_sec = 28, tm_min = 27, tm_hour = 12, tm_mday = 23, tm_mon = 4, tm_year = 112, tm_wday = 3, tm_yday = 143, tm_isdst = 1, tm_gmtoff = 7200, tm_zone = 0x8127a38 "CEST"} </code></pre> <p>So <code>strptime()</code> sets the <code>tm_zone</code> according to the current time zone anyway. But even if I would use <code>timelocal()</code> instead of <code>timegm()</code> I will not get correct value as it should be <strong><em>11:27:28 CEST</em></strong> and not <strong><em>12:27:28 CEST</em></strong>. Is this error related to different results of <code>strftime()</code>?</p> <p>Any comments to this later part?</p>
    singulars
    1. This table or related slice is empty.
    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