9

I'm trying to convert a Javascript API call to Python. The working javascript code works fine, generating a timestamp like this:

var curdate = new Date();
var gmtstring = curdate.toGMTString();
var utc = Date.parse(gmtstring) / 1000;

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.

Here's some details on the different results from the 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'm clearly missing something, can anyone enlighten me on this?

Update

I had originally made a mistake in my examples, as Javascript uses 0 based dates and Python's dates are 1-based.

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.

Answer

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.

This can probably be cleaned up:

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 provide a datetime:

GenerateTimeStamp(datetime(2013, 8, 10))

or

GenerateTimeStamp(datetime.utcnow())

As a side note, if you're trying this out and want to install pytz1 using pip, you'll can get it using the pre tag2:

pip install --pre pytz

3 Answers 3

9

For JavaScript Dates, the month argument is 0 - 11. So, for August, you'll want to pass 7.

Integer value representing the month, beginning with 0 for January to 11 for December.

They also have different default timezones, with Python defaulting to UTC while JavaScript defaults to the user's "local" timezone.

You can use Date.UTC(), which returns the timestamp, for the equivalent in JavaScript.

var utc = Date.UTC(2013, 7, 10) / 1000;
// 1376092800

Side note: You can use getTime() to get the timestamp of a Date.

var curdate = new Date(2013, 7, 10);
var utc = curdate.getTime() / 1000;
Sign up to request clarification or add additional context in comments.

3 Comments

Whoops, my mistake, corrected the example date. Just looking at your UTC suggestion.
Ah that accounts for the difference in the examples, the javascript is the result I need. My timezone is GMT which explains this. Could you suggest how to get the GMT result in python?
@PaulCoghill Well, you have the timezone for that date in UTC/GMT from Python now; that actually seems to be the problem. If you want Python to create datetimes in a different timezone, you can pass an instance of a custom tzinfo subclass: datetime(2013, 8, 10, tzinfo=desiredTZ). You can also use pytz: pytz.timezone('...').localize(datetime(2013, 8, 10)).
3

new Date(2013, 8, 10) in javascript means September/10/2013.

datetime(2013, 8, 10) in Python means August/10/2013.

You're comparing two different dates.

Local time vs UTC

Results may differ (timezone for my system: Asia/Seoul).

javascript

> new Date(2013, 7, 10) / 1000
1376060400
> Date.UTC(2013, 7, 10) / 1000
1376092800

Python

>>> import calendar
>>> import datetime
>>> import time
>>> time.mktime(datetime.datetime(2013, 8, 10).timetuple())
1376060400.0
>>> calendar.timegm(datetime.datetime(2013, 8, 10).timetuple())
1376092800

See Python - calendar.timegm() vs. time.mktime()

5 Comments

Thanks, I'll read the article you recommended. Thanks for the update, though the API I need to call would expect 1376089200 for that date, as per the example in the question.
@PaulCoghill, What is your timezone?
GMT. Though currently it's BST (Brittish Summer Time), the javascript has worked in either.
@PaulCoghill, Did you tried time.mktime(datetime.datetime(2013, 8, 10).timetuple()) in your system?
time.mktime(datetime.datetime(2013, 8, 10).timetuple()) returns 1376092800.0
1

In addition to Jonathan's answer above, I might recommend this on the python side:

 d = datetime(2013, 8, 10)
 d.strftime("%s")

That will return: '1376107200'

1 Comment

For me this returns: 1376089200 locally 1376092800 on a remote server

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.