R/monitor_timeRange.R

Defines functions monitor_timeRange

Documented in monitor_timeRange

#' @export
#' @title Get the time range for a monitor
#'
#' @description This function is a wrapper for \code{range(monitor$data$datetime)}
#' and is convenient for use in data pipelines.
#'
#' Dates will be returned in the timezone associated with
#' \code{monitor$data$datetime} which is typically "UTC" unless
#' \code{timezone} is specified.
#'
#' @param monitor \emph{mts_monitor} object.
#' @param timezone Olson timezone for the returned dates.
#'
#' @return A vector containing the minimum and maximum times of a
#' \emph{mts_monitor} object.
#'
#' @examples
#' Carmel_Valley %>%
#'   monitor_timeRange(timezone = "America/Los_Angeles")


monitor_timeRange <- function(
  monitor = NULL,
  timezone = NULL
) {

  # ----- Validate parameters --------------------------------------------------

  MazamaCoreUtils::stopIfNull(monitor)

  if ( !is.null(timezone) ) {
    if ( !timezone %in% OlsonNames() )
      stop(sprintf("timezone '%s' is not a valid OlsonNames() timezone", timezone))
  }

  # ----- Return ---------------------------------------------------------------

  timeRange <- range(monitor$data$datetime)

  if ( !is.null(timezone) )
    timeRange <- lubridate::with_tz(timeRange, tzone = timezone)

  return(timeRange)

}

Try the AirMonitor package in your browser

Any scripts or data that you put into this service are public.

AirMonitor documentation built on Aug. 26, 2023, 1:08 a.m.