Skip to content

Commit

Permalink
Add a function for parsing time intervals output by utilities like 's…
Browse files Browse the repository at this point in the history
…queue'
  • Loading branch information
Nick-Eagles committed Oct 27, 2023
1 parent 456baa0 commit 9faf992
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ Imports:
glue,
stringr,
purrr,
utils
utils,
lubridate
Depends:
R (>= 2.10)
LazyData: true
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export(job_single)
export(partition_info)
import(dplyr)
import(glue)
import(lubridate)
import(purrr)
import(stringr)
import(utils)
46 changes: 46 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,49 @@ get_short_flags <- function(vec) {
vector_as_code <- function(vec) {
return(sprintf('c("%s")', paste(vec, collapse = '", "')))
}

#' Parse time intervals reported by SLURM commands into \code{difftime}s
#'
#' Given a character vector 'vec', return a character(1) representing the line
#' of code used to generate 'vec'
#'
#' @param tim A \code{character()} representing time intervals as reported in
#' fields like \code{Elapsed} from \code{squeue}, in [days]-[hours]:[mins]:[secs]
#'
#' @return A \code{difftime()} vector of time intervals
#'
#' @import stringr lubridate
#' @author Nicholas J. Eagles
#'
#' @examples
#'
#' slurm_times <- c("0:00", "1:04:07", "11:03:02", "1-01:39:12", "33-14:40:54")
#' parse_slurm_time(slurm_times)
#'
parse_slurm_time <- function(tim) {
# First, reformat time string 'tim' to be in format
# [days]-[hours]:[mins]:[secs], with two digits for each quantity (other
# than days, which can be arbitrarily many digits in theory)
base_time <- "0-00:00:00"
full_time <- paste0(
sapply(
tim,
function(x) substr(base_time, 1, nchar(base_time) - nchar(x))
),
tim
)

# Now, convert to 'difftime' objects. 'difftime' does not appear to
# directly handle parsing an arbitrary number of days, so we manually
# split the day and non-day components before adding together
num_days <- full_time |>
str_extract("^([0-9]+)-", group = 1) |>
days() |>
as.difftime()
num_not_days <- full_time |>
str_extract("^[0-9]+-(.*)$", group = 1) |>
as.difftime("%H:%M:%S")

# Return the difftime
return(num_days + num_not_days)
}
28 changes: 28 additions & 0 deletions man/parse_slurm_time.Rd

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

0 comments on commit 9faf992

Please sign in to comment.