Home > Back-end >  python gives invalid argument error on OS level
python gives invalid argument error on OS level

Time:02-12

i have this piece of code:

symbol = 'PSTV'
end_point="https://api.polygon.io/v2/snapshot/locale/us/markets/stocks/tickers/" symbol "?apiKey=my_key"
            
a_json=requests.get(end_point).json()
if a_json['status'] == 'OK':
    candle_open  = a_json['ticker']['min']['o']
    candle_close = a_json['ticker']['min']['c']
    candle_high  = a_json['ticker']['min']['h']
    candle_low   = a_json['ticker']['min']['l']        
    candle_ts    = a_json['ticker']['lastQuote']['t']
    print(candle_ts/1000000)
    candle_ts = datetime.fromtimestamp(candle_ts/1000000).strftime('%Y-%m-%d %H:%M:%S')        
    print("OK")

im trying to convert timestamp to a readable format like so:

candle_ts    = a_json['ticker']['lastQuote']['t'] #get the timestamp
print(candle_ts/1000000)
candle_ts = datetime.fromtimestamp(candle_ts/1000000).strftime('%Y-%m-%d %H:%M:%S')

the print is : 1644529277457.4104

I have no clue why but the error is :

    candle_ts = datetime.fromtimestamp(candle_ts/1000000).strftime('%Y-%m-%d %H:%M:%S')        
OSError: [Errno 22] Invalid argument

Why do I get such an unusual error??

CodePudding user response:

The value for candle_ts is out of range, as you can see below sample script. The max limit is year 5138 which is around 11digits only. Your value for candle_ts is 13digits.

from datetime import datetime
candle_ts = 1644529277457.4104
try:
    candle_ts = datetime.fromtimestamp(candle_ts).strftime('%Y-%m-%d %H:%M:%S')
    print(candle_ts)
except Exception as e:
    print(e)

Result:
 year 54083 is out of range
  • Related