我试图从一个时区(fromtz)中给出日期(indate,inmonth)和时间(intime),然后使用timezonedbapi请求另一个时区(totz)中的时间
indate = args[0]
inmonth=args[1]
intime=args[2]
fromtz=args[3]
totz=args[4]
inyear = datetime.now().year
print(str(indate)+'/'+str(inmonth)+'/'+str(inyear)+' '+str(intime))
dt_obj = datetime.strptime(str(indate)+'/'+str(inmonth)+'/'+str(inyear)+' '+str(intime),'%d/%m/%Y %H:%M')
millisec = dt_obj.timestamp() * 1000
url="http://api.timezonedb.com/v2.1/convert-time-zone?key="+API_KEY+"&format=json&from="+fromtz+"&to="+totz+"&time="+str(int(millisec))
response = requests.get(url)
jsonoutput = json.loads(response.text)
timeoutputms = jsonoutput["toTimestamp"]
outputdt = datetime.fromtimestamp(timeoutputms/1000).strftime('%d/%m %H:%M')
测试1:
Input - 30 01 00:00 CST GMT
Output - 30/01 00:00 CST
Correct output must be - 30/01 06:00 CST
测试2:
Input - 30 01 00:00 GMT CST
Output - 29/01 23:59 CST
Correct output must be - 29/01 18:00 CST
JSON响应:{'status':'OK','message':'''fromZoneName':'Europe/Jersey','From缩写':'GMT','fromTimestamp':1611945000000,'toZoneName':'America/North_Dakota/Beulah','ToabRevision':'CST','toTimestamp':1611944978400,'offset':-21600}
如果使用正确的时区名称(如timezonedb docs中所列),您的查询原则上是有效的。明确的缩写词,如GMT或UTC也应适用
Ex:
有趣的是,对于我的测试查询,输入和输出被关闭了约18秒。我不知道这是从哪里来的,但这是离题的
相关问题 更多 >
编程相关推荐