<story><title>Dealing with Timezones in Python </title><url>http://lucumr.pocoo.org/2011/7/15/eppur-si-muove/</url></story><parent_chain></parent_chain><comment><author>nikcub</author><text>a few more gotcha&apos;s:&lt;p&gt;* when storing a UTC timestamp and you need to get a datetime object back in order to add dates or do some calcualtions, make sure you use datetime.datetime.utcfromtimestamp(), eg.&lt;p&gt;&lt;pre&gt;&lt;code&gt;    &amp;#62;&amp;#62;&amp;#62; datetime.datetime.utcfromtimestamp(1310727758.472)
    datetime.datetime(2011, 7, 15, 11, 2, 38, 472000)
&lt;/code&gt;&lt;/pre&gt;
* python timestamps are longs and represent time in seconds. in Javascript it is the number of milliseconds since epoch, so you need to divide/multiple by 1000 depending on which way you are going. for eg.&lt;p&gt;&lt;pre&gt;&lt;code&gt;    &amp;#62; new Date().valueOf()
    1310728084389

    &amp;#62;&amp;#62;&amp;#62; datetime.datetime.utcfromtimestamp(1310728084389)
    Traceback (most recent call last):
      File &quot;&amp;#60;stdin&amp;#62;&quot;, line 1, in &amp;#60;module&amp;#62;
    ValueError: year is out of range
    &amp;#62;&amp;#62;&amp;#62; datetime.datetime.utcfromtimestamp(1310728084389 / 1000)
    datetime.datetime(2011, 7, 15, 11, 8, 4)
&lt;/code&gt;&lt;/pre&gt;
* always set your server to UTC, always use the utc* functions (eg. utctimetuple, utcfromtimestamp, utctime etc.), store as a long, and when passing back to the client send the current UTC timestamp as well rather than relying on the client system clock, which is frequently wrong. You can also use:&lt;p&gt;&lt;a href=&quot;http://json-time.appspot.com/time.json?tz=UTC&quot; rel=&quot;nofollow&quot;&gt;http://json-time.appspot.com/time.json?tz=UTC&lt;/a&gt;&lt;p&gt;or implement something similar. I resorted to always outputting the current UTC time into a javascript variable and using that to calibrate the local computer time (again, never trusting client time)&lt;p&gt;* never use time.time() to get a timestamp back because it will munge the value and re-add tz info&lt;p&gt;* in Javascript, to go from your server-provided UTC timestamp to a local date object that you can then work with, use:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    &amp;#62; new Date(1310728084 * 1000).toUTCString()
    &quot;Fri, 15 Jul 2011 11:08:04 GMT&quot;
&lt;/code&gt;&lt;/pre&gt;
* If you are providing an API or a feed, please include a UTC timestamp in the output - parsing all the different date formats and timestamp info and attempting to work out when something happen (such as when a post was published) is a real pain in the ass. store the timezone in a sep field called &apos;utc_offset&apos; or similar</text></comment>
<story><title>Dealing with Timezones in Python </title><url>http://lucumr.pocoo.org/2011/7/15/eppur-si-muove/</url></story><parent_chain></parent_chain><comment><author>grovulent</author><text>So I&apos;ve been coding for about a year and a half now and had to learn all this the hard way.  Which isn&apos;t a bad thing at all.&lt;p&gt;It&apos;s little stuff like this that really makes it true that you should try to build an app or two like a blog-n-whatnot so that one&apos;s ignorance of these issues can be exposed.</text></comment>