Skip to content

Commit

Permalink
Update code and tests to use hms instead of lubridate
Browse files Browse the repository at this point in the history
  • Loading branch information
mstackhouse committed Jan 29, 2025
1 parent e933777 commit 386a5a9
Show file tree
Hide file tree
Showing 9 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Depends: R (>= 4.0)
Imports:
yyjsonr (>= 0.1.18),
jsonvalidate (>= 1.3.1),
lubridate
hms
Suggests:
testthat (>= 2.1.0),
jsonlite (>= 1.8.0),
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export(set_study_oid)
export(set_variable_attributes)
export(validate_dataset_json)
export(write_dataset_json)
importFrom(hms,as_hms)
importFrom(jsonvalidate,json_validate)
importFrom(lubridate,hms)
importFrom(tools,file_path_sans_ext)
importFrom(utils,tail)
importFrom(yyjsonr,opts_read_json)
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ date_time_conversions <- function(d, dt, tdt){
as.POSIXct,
tz = "UTC",
tryFormats = "%Y-%m-%dT%H:%M:%S")
d[time_cols] <- lapply(d[time_cols], hms)
d[time_cols] <- lapply(d[time_cols], as_hms)
d
}

2 changes: 1 addition & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#' @importFrom yyjsonr opts_write_json opts_read_json write_json_file write_json_str read_json_str read_json_file
#' @importFrom tools file_path_sans_ext
#' @importFrom utils tail
#' @importFrom lubridate hms
#' @importFrom hms as_hms
NULL

#' @keywords internal
Expand Down
2 changes: 1 addition & 1 deletion data-raw/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ time_options <- c("12:34:56", "15:34:34", "11:12:52", "21:16:11")

adsl$VIST1TMC <- sample(time_options, 254, replace=TRUE)
adsl$VIST1DTC <-paste(format(adsl$VISIT1DT, "%Y-%m-%d"), sample(time_options, 254, replace=TRUE), sep="T")
adsl$VISIT1TM <- lubridate::hms(adsl$VIST1TMC)
adsl$VISIT1TM <- hms::as_hms(adsl$VIST1TMC)
adsl$VIST1DTM <- as.POSIXct(strptime(adsl$VIST1DTC, "%Y-%m-%dT%H:%M:%S", tz="UTC"))

new_meta <- tibble::tribble(
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-read_dataset_json.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ test_that("datetime conversions work properly",{
iris_timetest <- read_dataset_json(test_path("testdata", "iris_timetest.json"))

expect_s3_class(iris_timetest$Datetime, "POSIXct")
expect_equal(class(iris_timetest$Time), "Period", ignore_attr=TRUE)
expect_equal(class(iris_timetest$Time), c("hms", "difftime"), ignore_attr=TRUE)

expect_equal(sort(unique(iris_timetest$Datetime)),
as.POSIXct(strptime(c("2024-01-01T12:34:56", "2024-01-17T18:45:56"),
"%Y-%m-%dT%H:%M:%S", tz="UTC")))
expect_equal(sort(unique(as.numeric(iris_timetest$Time))),
as.numeric(hms(c("12:34:56", "18:45:56"))))
as.numeric(as_hms(c("12:34:56", "18:45:56"))))
})
2 changes: 1 addition & 1 deletion tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ test_that("Date, datetime and time conversions work as expected", {
expect_equal(df_converted$datetime, as.POSIXct(c("2020-01-01 12:00:00",
"2020-01-01 12:00:01",
NA), tz = "UTC"))
expect_equal(df_converted$time, hms(c("12:00:00", "12:00:01", NA)))
expect_equal(df_converted$time, as_hms(c("12:00:00", "12:00:01", NA)))
})
Binary file modified tests/testthat/testdata/adsl_time_test.Rds
Binary file not shown.
3 changes: 3 additions & 0 deletions vignettes/converting_files.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ extract_xpt_meta <- function(n, .data) {
} else if (inherits(.data[[n]],"numeric")) {
if (any(is.double(.data[[n]]))) out$dataType <- "float"
else out$dataType <- "integer"
} else if (inherits(.data[[n]],"hms")) {
out$dataType <- "time"
out$targetDataType <- "integer"
} else {
out$dataType <- "string"
out$length <- max(purrr::map_int(.data[[n]], nchar))
Expand Down

0 comments on commit 386a5a9

Please sign in to comment.