Skip to content

Commit

Permalink
Work with SI formatted numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
billdenney committed Nov 1, 2023
1 parent dd8c94a commit 5776ed4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
13 changes: 9 additions & 4 deletions R/excel_time_to_numeric.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#' Convert a time that may be inconsistently or inconveniently formatted from
#' Microsoft Excel to a numeric number of seconds within a day.
#'
#'
#' @details
#'
#'
#' \code{time_value} may be one of the following formats:
#' \itemize{
#' \item{numeric}{The input must be a value from 0 to 1 (exclusive of 1); this value is returned as-is.}
Expand Down Expand Up @@ -80,6 +80,9 @@ excel_time_to_numeric.character <- function(time_value, round_seconds=TRUE) {
patterns <-
list(
number="^0(\\.[0-9]*)?$",
# SI numbers have to have the form [number]E-[number] becasue the number
# has to be between 0 and 1 and can't be bigger than 1.
si_number="^[1-9](\\.[0-9]*)?E-[0-9]+$",
"12hr"="^([0]?[1-9]|1[0-2]):([0-5][0-9])(?::([0-5][0-9]))? ?([AP]M)$",
"24hr"="^([0-1]?[0-9]|2[0-3]):([0-5][0-9])(?::([0-5][0-9]))?$",
# The ".*?" at the end of POSIX is to allow for a time zone, but it allows
Expand All @@ -89,7 +92,9 @@ excel_time_to_numeric.character <- function(time_value, round_seconds=TRUE) {
POSIX="1899-12-31 (?:([0-1]?[0-9]|2[0-3]):([0-5][0-9])(?::([0-5][0-9]))?)?.*?$"
)
mask_na <- is.na(time_value)
mask_number <- grepl(pattern=patterns$number, x=time_value)
mask_number <-
grepl(pattern=patterns$number, x=time_value) |
grepl(pattern=patterns$si_number, x=time_value)
mask_POSIX <- grepl(pattern=patterns[["POSIX"]], x=time_value)
mask_12hr <- grepl(pattern=patterns[["12hr"]], x=time_value, ignore.case=TRUE)
mask_24hr <- grepl(pattern=patterns[["24hr"]], x=time_value)
Expand Down Expand Up @@ -149,7 +154,7 @@ excel_time_to_numeric.character <- function(time_value, round_seconds=TRUE) {
hours[hours %in% ""] <- "0"
minutes[minutes %in% ""] <- "0"
seconds[seconds %in% ""] <- "0"

ret[mask_clock] <-
as.numeric(hours[mask_clock]) * 3600 +
as.numeric(minutes[mask_clock]) * 60 +
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-excel_time_to_numeric.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ test_that("excel_time_to_numeric, character strings of numbers work as expected"
excel_time_to_numeric("0.00001", round_seconds=FALSE),
0.00001*86400
)
# Confirm scientific notation values
expect_equal(
excel_time_to_numeric("2.9166666666666664E-2", round_seconds=TRUE),
2520
)
})

test_that("excel_time_to_numeric, am/pm times work", {
Expand Down

0 comments on commit 5776ed4

Please sign in to comment.