From 0f97314252ddaed46243ca30bdec29493ae05061 Mon Sep 17 00:00:00 2001 From: Joshua Ulrich Date: Thu, 27 Jul 2023 15:45:54 -0500 Subject: [PATCH] Handle missing regularMarketTime getQuote() would error if the user did not request any metrics that have a value for regularMarketTime. Set the value to NA in these cases so the output remains the same regardless of whether the endpoint returns a regularMarketTime or not. Thanks to @mehdiMBH for the report! Fixes #255. --- R/getQuote.R | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/R/getQuote.R b/R/getQuote.R index 1209d704..d192179d 100644 --- a/R/getQuote.R +++ b/R/getQuote.R @@ -103,7 +103,7 @@ function(Symbols,what=standardQuote(),session=NULL,...) { QF.names <- NULL } # JSON API currently returns the following fields with every request: - # language, quoteType, regularMarketTime, marketState, exchangeDataDelayedBy, + # language, quoteType, marketState, exchangeDataDelayedBy, # exchange, fullExchangeName, market, sourceInterval, exchangeTimezoneName, # exchangeTimezoneShortName, gmtOffSetMilliseconds, tradeable, symbol QFc <- paste0(QF,collapse=',') @@ -117,15 +117,21 @@ function(Symbols,what=standardQuote(),session=NULL,...) { } else { stop(response$quoteResponse$error) } - # Always return symbol and time + + # Always return symbol and time (if regularMarketTime is provided) # Use exchange TZ, if possible. POSIXct must have only one TZ, so times # from different timezones will be converted to a common TZ - tz <- sq[, "exchangeTimezoneName"] - if (length(unique(tz)) == 1L) { - Qposix <- .POSIXct(sq[,"regularMarketTime"], tz=tz[1L]) + regularMktTime <- sq$regularMarketTime + if (is.null(regularMktTime)) { + Qposix <- .POSIXct(NA) } else { - warning("symbols have different timezones; converting to local time") - Qposix <- .POSIXct(sq$regularMarketTime, tz = NULL) # force local timezone + tz <- sq[, "exchangeTimezoneName"] + if (length(unique(tz)) == 1L) { + Qposix <- .POSIXct(regularMktTime, tz = tz[1L]) + } else { + warning("symbols have different timezones; converting to local time") + Qposix <- .POSIXct(regularMktTime, tz = NULL) # force local timezone + } } # Extract user-requested columns. Convert to list to avoid