8

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
4

3 回答 3

8

对于 JavaScript 日期,month参数是 0 - 11。所以,对于 8 月,你会想要通过7.

表示月份的整数值,从 0 开始表示一月到 11 表示十二月。

它们也有不同的默认时区,Python 默认为 UTC,而 JavaScript 默认为用户的“本地”时区。

您可以使用Date.UTC()返回时间戳的 ,用于 JavaScript 中的等价物。

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

旁注:您可以使用getTime()来获取Date.

var curdate = new Date(2013, 7, 10);
var utc = curdate.getTime() / 1000;
于 2013-08-10T10:11:22.913 回答
3

new Date(2013, 8, 10)在 javascript 中表示 2013 年 9 月/10 日。

datetime(2013, 8, 10)在 Python 中表示 2013 年 8 月/10 日。

您正在比较两个不同的日期。

当地时间与 UTC

结果可能会有所不同(我的系统的时区:亚洲/首尔)。

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

请参阅Python - calendar.timegm() 与 time.mktime()

于 2013-08-10T10:11:12.037 回答
1

除了上面乔纳森的回答,我可能会在 python 方面推荐这个:

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

这将返回:'1376107200'

于 2013-08-10T10:27:16.863 回答