Note that there are some explanatory texts on larger screens.

plurals
  1. POPython vs Javascript DateTime
    primarykey
    data
    text
    <p>I'm trying to convert a Javascript API call to Python. The working javascript code works fine, generating a timestamp like this:</p> <pre><code>var curdate = new Date(); var gmtstring = curdate.toGMTString(); var utc = Date.parse(gmtstring) / 1000; </code></pre> <p>This result (this number of seconds since the epoch) is subsequently hashed and used in the API call, but this is the relevant section. If anyone can let me know the correct way to convert this it would be much appreciated.</p> <p>Here's some details on the different results from the different methods:</p> <h2>Javascript(Valid API Result)</h2> <pre><code>var curdate = new Date(2013, 7, 10); var gmtstring = curdate.toGMTString(); var utc = Date.parse(gmtstring) / 1000; </code></pre> <p><strong>Result: 1376089200</strong></p> <h2>Python (Invalid API Result)</h2> <pre><code>from datetime import datetime import calendar d = datetime(2013, 8, 10) calendar.timegm(d.utctimetuple()) </code></pre> <p><strong>Result: 1376092800</strong></p> <p>I'm clearly missing something, can anyone enlighten me on this?</p> <h2>Update</h2> <p>I had originally made a mistake in my examples, as Javascript uses 0 based dates and Python's dates are 1-based.</p> <p>Jonathon kindly explained the difference in the values is different due to a Python defaulting to UTC where as Javascript is defaulting to the local timezone. In my case this is GMT, which is the one required by the API. I now just need to get this result in Python.</p> <h2>Answer</h2> <p>The solution was a mismatch of the timezones being provided. Though I'm still having issues with the third party api, I am at least now getting the correct times.</p> <p>This can probably be cleaned up:</p> <pre><code>from datetime import datetime import calendar import time import pytz def GenerateTimeStamp(d): europe = pytz.timezone('Europe/London') d = europe.localize(d) tuple = d.utctimetuple() timestamp = int(time.mktime(tuple)) return timestamp </code></pre> <p>Just provide a datetime:</p> <pre><code>GenerateTimeStamp(datetime(2013, 8, 10)) </code></pre> <p>or</p> <pre><code>GenerateTimeStamp(datetime.utcnow()) </code></pre> <p>As a side note, if you're trying this out and want to install pytz<a href="http://pytz.sourceforge.net/" rel="nofollow">1</a> using pip, you'll can get it using the pre tag<a href="https://github.com/pypa/pip/issues/974" rel="nofollow">2</a>:</p> <pre><code>pip install --pre pytz </code></pre>
    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