【发布时间】:2021-05-06 16:16:04
【问题描述】:
我正在尝试从一个时区(fromtz)给出日期(indate,inmonth)和时间(intime),然后使用 timezonedb api 请求另一个时区(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:
输入 - 30 01 00:00 CST GMT
输出 - 30/01 00:00 CST
正确的输出必须是 - 30/01 06:00 CST
测试 2:
输入 - 30 01 00:00 GMT CST
输出 - 29/01 23:59 CST
正确的输出必须是 - 29/01 18:00 CST
JSON 响应:{'status': 'OK', 'message': '', 'fromZoneName': 'Europe/Jersey', 'fromAbbreviation': 'GMT', 'fromTimestamp': 1611945000000, 'toZoneName': 'America/North_Dakota/Beulah', 'toAbbreviation': 'CST', 'toTimestamp': 1611944978400, 'offset': -21600}
【问题讨论】:
-
CST is ambiguous - 至少有三个...最好坚持IANA names。
标签: python datetime timezone timezonedb