Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have a problem one way or the other with your <code>datetime.datetime.fromtimestamp(time.mktime(time.gmtime()))</code> expression.</p> <p>(1) If all you need is the difference between two instants in seconds, the very simple <code>time.time()</code> does the job.</p> <p>(2) If you are using those timestamps for other purposes, you need to consider what you are doing, because the result has a big smell all over it:</p> <p><code>gmtime()</code> returns a time tuple in <strong>UTC</strong> but <code>mktime()</code> expects a time tuple in <strong>local time.</strong></p> <p>I'm in Melbourne, Australia where the standard TZ is UTC+10, but daylight saving is still in force until tomorrow morning so it's UTC+11. When I executed the following, it was 2011-04-02T20:31 local time here ... UTC was 2011-04-02T09:31</p> <pre><code>&gt;&gt;&gt; import time, datetime &gt;&gt;&gt; t1 = time.gmtime() &gt;&gt;&gt; t2 = time.mktime(t1) &gt;&gt;&gt; t3 = datetime.datetime.fromtimestamp(t2) &gt;&gt;&gt; print t0 1301735358.78 &gt;&gt;&gt; print t1 time.struct_time(tm_year=2011, tm_mon=4, tm_mday=2, tm_hour=9, tm_min=31, tm_sec=3, tm_wday=5, tm_yday=92, tm_isdst=0) ### this is UTC &gt;&gt;&gt; print t2 1301700663.0 &gt;&gt;&gt; print t3 2011-04-02 10:31:03 ### this is UTC+1 &gt;&gt;&gt; tt = time.time(); print tt 1301736663.88 &gt;&gt;&gt; print datetime.datetime.now() 2011-04-02 20:31:03.882000 ### UTC+11, my local time &gt;&gt;&gt; print datetime.datetime(1970,1,1) + datetime.timedelta(seconds=tt) 2011-04-02 09:31:03.880000 ### UTC &gt;&gt;&gt; print time.localtime() time.struct_time(tm_year=2011, tm_mon=4, tm_mday=2, tm_hour=20, tm_min=31, tm_sec=3, tm_wday=5, tm_yday=92, tm_isdst=1) ### UTC+11, my local time </code></pre> <p>You'll notice that t3, the result of your expression is UTC+1, which appears to be UTC + (my local DST difference) ... not very meaningful. You should consider using <code>datetime.datetime.utcnow()</code> which won't jump by an hour when DST goes on/off and may give you more precision than <code>time.time()</code></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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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