Skip to content

Commit

Permalink
Merge pull request #161 from ropensci/query
Browse files Browse the repository at this point in the history
expose and store properly query time. Closes #160
  • Loading branch information
boshek committed Sep 7, 2021
2 parents efb5d9c + 5b078ca commit d10ce22
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions NEWS.md
@@ -1,6 +1,7 @@
# tidyhydat 0.5.3.9000
- When add a local timezone column, use the most common timezone in the data rather than the first one. This just seems more likely to be useful to users
- Add more documentation to `realtime_add_local_datetime` to make how timezones are dealt with clearer (#157)
- Expose the query time for realtime functions as an attribute (#160)

# tidyhydat 0.5.3
- Allow pkg to loaded without internet and rather just issue an message when it is not. (#149)
Expand Down
6 changes: 5 additions & 1 deletion R/realtime-classes.R
Expand Up @@ -13,12 +13,16 @@
## Add "realtime" class
as.realtime <- function(x) {
class(x) <- c("realtime", setdiff(class(x), "realtime"))
t <- Sys.time()
attr(t, "tzone") <- "UTC"
attr(x, "query_time") <- t

x
}

#' @export
print.realtime <- function(x, ...){
cat(paste(" Queried on:", format(Sys.time(), tz = "UTC"), "(UTC)\n"))
cat(paste(" Queried on:", attributes(x)$query_time, "(UTC)\n"))
if(c("Date") %in% names(x) && !all(is.na(x$Date))){
date_range = paste0(range(as.Date(x$Date), na.rm = TRUE), collapse = " to ")
cat(paste0(" Date range: ", date_range, " \n"))
Expand Down
10 changes: 7 additions & 3 deletions R/realtime.R
Expand Up @@ -20,7 +20,8 @@
#'
#' @inheritParams hy_stations
#'
#' @return A tibble of water flow and level values.
#' @return A tibble of water flow and level values. The date and time of the query (in UTC) is also
#' stored as an attribute.
#'
#' @format A tibble with 8 variables:
#' \describe{
Expand All @@ -41,8 +42,11 @@
#' ## Download from multiple provinces
#' realtime_dd(station_number=c("01CD005","08MF005"))
#'
#' # To download all stations in Prince Edward Island:
#' realtime_dd(prov_terr_state_loc = "PE")
#' ## To download all stations in Prince Edward Island:
#' pei <- realtime_dd(prov_terr_state_loc = "PE")
#'
#' ## Access the time of query
#' attributes(pei)$query_time
#' }
#'
#' @family realtime functions
Expand Down
10 changes: 7 additions & 3 deletions man/realtime_dd.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tests/testthat/test_download_realtime.R
Expand Up @@ -9,14 +9,15 @@ context("Testing realtime functions")
)
})

test_that("realtime_dd can download stations from a whole province using prov_terr_state_loc", {
test_that("realtime_dd can download stations from a whole province using prov_terr_state_loc and stores query time", {
#skip_on_travis()
skip_on_cran()
expected_columns <- c("STATION_NUMBER", "PROV_TERR_STATE_LOC", "Date", "Parameter",
"Value", "Grade", "Symbol", "Code")
rldf <- realtime_dd(prov_terr_state_loc = "PE")

expect_true(identical(expected_columns,colnames(rldf)))
expect_is(attributes(rldf)$query_time, "POSIXct")
})


Expand Down

0 comments on commit d10ce22

Please sign in to comment.