Skip to content

Commit

Permalink
Add functions for generating time sequence character vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-MET committed Nov 29, 2023
1 parent 37e9482 commit 7e046de
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 13 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: harpCore
Title: Core functions and methods for the harp ecosystem
Version: 0.2.0.9000
Version: 0.2.0.9001
Authors@R:
person("Andrew", "Singleton", , "[email protected]", role = c("aut", "cre"))
Description: harp is a collection of packages for reading, analysing and
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,13 @@ export(q_to_rh)
export(rh_to_td)
export(scale_param)
export(select_members)
export(seq_days)
export(seq_double)
export(seq_dttm)
export(seq_hours)
export(seq_mins)
export(seq_pwr2)
export(seq_secs)
export(set_units)
export(std_dev)
export(to_seconds)
Expand Down
57 changes: 52 additions & 5 deletions R/date_time.R
Original file line number Diff line number Diff line change
Expand Up @@ -327,16 +327,23 @@ unixtime_to_str_dttm <- function(x) {

###

#' Generate a sequence of date-time strings
#' Generate a sequence of time strings
#'
#' Given a start date-time, end date-time and time resolution a regular sequence
#' of date-time strings is generated. The start and end date-times must be a
#' string or numeric of the form YYYYMMDD, YYYYMMDDhh, YYYYMMDDhhmm, or
#' YYYYMMDDhhmmss.
#' @description Given a start date-time, end date-time and time resolution
#' `seq_dttm()` generates a regular sequence of date-time strings is generated.
#' The start and end date-times must be a string or numeric of the form
#' YYYYMMDD, YYYYMMDDhh, YYYYMMDDhhmm, or YYYYMMDDhhmmss.
#'
#' The output sequence is a vector of strings. Truncation of the strings is done
#' so that the last zero values are removed.
#'
#' `seq_secs()`, `seq_mins()`, `seq_hours()` and `seq_days()` generate regular
#' sequences of numbers as character vectors with a character specifying the
#' time unit. These vectors can be used to, for example, generate sequences of
#' lead times for input to functions such as
#' \code{\link[harpIO]{read_forecast}}. \code{\link{to_seconds}} can be used to
#' convert any of the
#'
#' @param start_dttm The date-time at the start of the sequence. Must be a
#' string or numeric of the form YYYYMMDD, YYYYMMDDhh, YYYYMMDDhhmm, or
#' YYYYMMDDhhmmss.
Expand Down Expand Up @@ -376,6 +383,46 @@ seq_dttm <- function(start_dttm, end_dttm, by = "1h") {
as_str_dttm(seq(start_dttm, end_dttm, by_secs))
}

#' @inheritParams base::seq
#'
#' @rdname seq_dttm
#' @export
#'
#' @examples
#' seq_secs(0, 60, 5)
seq_secs <- function(from, to, by = 1) {
paste0(seq(from, to, by), "s")
}

#' @rdname seq_dttm
#' @export
#'
#' @examples
#' seq_mins(0, 60, 15)
seq_mins <- function(from, to, by = 1) {
paste0(seq(from, to, by), "m")
}

#' @rdname seq_dttm
#' @export
#'
#' @examples
#' seq_hours(0, 6)
#' seq_hours(0, 6, 3)
seq_hours <- function(from, to, by = 1) {
paste0(seq(from, to, by), "h")
}

#' @rdname seq_dttm
#' @export
#'
#' @examples
#' seq_days(0, 7)
#' seq_days(0, 28, 7)
seq_days <- function(from, to, by = 1) {
paste0(seq(from, to, by), "d")
}

#' Convert a time period to seconds
#'
#' Convert number of minutes, hours, days or weeks into seconds. If \code{x} is
Expand Down
42 changes: 35 additions & 7 deletions man/seq_dttm.Rd

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

0 comments on commit 7e046de

Please sign in to comment.