I am trying to convert a Javascript API call to Python. The javascript working code works fine by creating a timestamp like this:
var curdate = new Date(); var gmtstring = curdate.toGMTString(); var utc = Date.parse(gmtstring) / 1000;
This result (this is the number of seconds since the era) is subsequently hashed and used in the API call, but this is the corresponding section. If anyone can tell me the correct way to convert this, we will be very grateful.
Here are some details about the different results of different methods:
Javascript (valid API result)
var curdate = new Date(2013, 7, 10); var gmtstring = curdate.toGMTString(); var utc = Date.parse(gmtstring) / 1000;
Result: 1376089200
Python (invalid API result)
from datetime import datetime import calendar d = datetime(2013, 8, 10) calendar.timegm(d.utctimetuple())
Result: 1376092800
I obviously missed something, can someone enlighten me on this?
Update
I initially made a mistake in my examples, since Javascript uses dates based on 0 and Python dates use 1.
Jonathon kindly explained that the difference in values ββis different because Python is set to UTC by default when Javascript uses the local time zone by default. In my case, this is GMT, which is required by the API. Now I just need to get this result in Python.
Answer
The solution was a mismatch of the time zones provided. Although I'm still having problems with a third-party api, I am at least getting the right times now.
Perhaps this can be cleared:
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
Just specify the date and time:
GenerateTimeStamp(datetime(2013, 8, 10))
or
GenerateTimeStamp(datetime.utcnow())
As a side note, if you are trying to do this and want to install pytz 1 using pip, you can get it using pre tag 2 :
pip install