如何在Alpha Vantage API中更改时区

时间:2019-11-01 04:11:05

标签: timezone alphavantage

我可以在Alpha Vantage API的结果中更改时区吗?这是输出示例。目前在美国东部标准时间。我想在IST中获得它。

'Meta Data': {
    '1. Information': 'Intraday (1min) open, high, low, close prices and volume',
    '2. Symbol': 'BSE:------',
    '3. Last Refreshed': '2019-11-01',
    '4. Output Size': 'Compact',
    '5. Time Zone': 'US/Eastern
}

'Time Series (1min)': {
    '2019-11-01 00:08:59': {
      '1. open': '70.7500',
      '2. high': '70.7500',
      '3. low': '70.7500',
      '4. close': '70.7500',
      '5. volume': '0'
    },

2 个答案:

答案 0 :(得分:1)

欢迎使用StackOverflow!

现在,返回的时区是您将从API收到的唯一时区。但是,如果要使用python脚本提取数据。您始终可以将其转换为您选择的时区。

from alpha_vantage.timeseries import TimeSeries
from datetime import datetime
import pytz

ts = TimeSeries()

data, meta_data = ts.get_daily('TSLA')
format = '%Y-%m-%d %H:%M:%S'

datetime = datetime.strptime(meta_data['3. Last Refreshed'], format)
old_timezone = pytz.timezone(meta_data['5. Time Zone'])
new_timezone = pytz.timezone("Asia/Calcutta")

# returns datetime in the new timezone
my_timestamp_in_new_timezone = old_timezone.localize(datetime).astimezone(new_timezone) 
print(my_timestamp_in_new_timezone.strftime(format))

您可以运行一个方法,该方法将在提取数据时将所有时间都转换为所需的时区

答案 1 :(得分:0)

ts.get_daily('TSLA')应该更改为ts.get_intraday('TSLA')