diff --git a/CRAN-SUBMISSION b/CRAN-SUBMISSION deleted file mode 100644 index 4d8abd2..0000000 --- a/CRAN-SUBMISSION +++ /dev/null @@ -1,3 +0,0 @@ -Version: 2.6.23 -Date: 2023-08-17 11:47:19 UTC -SHA: 2637c4a6ad8d382709ba393643502f955e0c1f9d \ No newline at end of file diff --git a/DESCRIPTION b/DESCRIPTION index 6cb70ba..2af2051 100755 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: naryn Title: Native Access Medical Record Retriever for High Yield Analytics -Version: 2.6.25 +Version: 2.6.26 Authors@R: c( person("Misha", "Hoichman", , "misha@hoichman.com", role = "aut"), person("Aviezer", "Lifshitz", , "aviezer.lifshitz@weizmann.ac.il", role = c("aut", "cre")), diff --git a/NAMESPACE b/NAMESPACE index babfc9f..0756073 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,6 +9,7 @@ export(.naryn) export(day) export(days) export(emr_annotate) +export(emr_char2time) export(emr_cor) export(emr_date2time) export(emr_db.connect) @@ -47,14 +48,17 @@ export(emr_filters.info) export(emr_ids_coverage) export(emr_ids_vals_coverage) export(emr_monthly_iterator) +export(emr_posix2time) export(emr_quantiles) export(emr_screen) export(emr_summary) export(emr_time) +export(emr_time2char) export(emr_time2date) export(emr_time2dayofmonth) export(emr_time2hour) export(emr_time2month) +export(emr_time2posix) export(emr_time2year) export(emr_track.addto) export(emr_track.attr.export) diff --git a/NEWS.md b/NEWS.md index e9b5317..f580dbb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# naryn 2.6.26 + +* Added `emr_time2posix`, `emr_posix2time`, `emr_time2char` and `emr_char2time` functions. + # naryn 2.6.25 * Fixed clang warnings. diff --git a/R/time.R b/R/time.R index 670b147..e9f46ff 100644 --- a/R/time.R +++ b/R/time.R @@ -198,6 +198,93 @@ emr_time2date <- function(time) { ) } +#' Convert EMR time to POSIXct +#' +#' These function converts EMR time to POSIXct format. It takes the EMR time as input and returns the corresponding POSIXct object. +#' +#' @param time The EMR time to be converted. +#' @param show_hour Logical value indicating whether to include the hour in the output. Default is FALSE. +#' @param tz Time zone to be used for the output POSIXct object. Default is "UTC". +#' @param posix A POSIXct object to be converted to EMR time. +#' +#' @return A POSIXct object representing the converted time. +#' +#' @examples +#' # 30 January, 1938, 6:00 - birthday of Islam Karimov +#' t1 <- emr_date2time(30, 1, 1938, 6) +#' # September 2, 2016, 7:00 - death of Islam Karimov +#' t2 <- emr_date2time(2, 9, 2016, 7) +#' +#' emr_time2posix(c(t1, t2)) +#' emr_time2posix(c(t1, t2), show_hour = TRUE) +#' +#' emr_posix2time(emr_time2posix(c(t1, t2), show_hour = TRUE)) +#' +#' # Note that when show_hour = FALSE, the hour is set to 0 +#' # and therefore the results would be different from the original time values +#' emr_posix2time(emr_time2posix(c(t1, t2))) +#' +#' @export +emr_time2posix <- function(time, show_hour = FALSE, tz = "UTC") { + year <- emr_time2year(time) + month <- emr_time2month(time) + day <- emr_time2dayofmonth(time) + + if (show_hour) { + hour <- emr_time2hour(time) + return(as.POSIXct(paste(year, month, day, hour, sep = "-"), format = "%Y-%m-%d-%H", tz = tz)) + } + + return(as.POSIXct(paste(year, month, day, sep = "-"), format = "%Y-%m-%d", tz = tz)) +} + +#' Convert time to character format +#' +#' This function converts a given time value to a character format in the form of "%Y-%m-%d" or "%Y-%m-%d %H:%M:%S" depending on the value of the `show_hour` parameter. +#' +#' @param time The time value to be converted. +#' @param show_hour Logical value indicating whether to include the hour in the output. Default is FALSE. +#' @param char A character string to be converted to EMR time. +#' +#' @return A character string representing the converted time value. +#' +#' @examples +#' # 30 January, 1938, 6:00 - birthday of Islam Karimov +#' t1 <- emr_date2time(30, 1, 1938, 6) +#' # September 2, 2016, 7:00 - death of Islam Karimov +#' t2 <- emr_date2time(2, 9, 2016, 7) +#' +#' emr_time2char(c(t1, t2)) +#' emr_time2char(c(t1, t2), show_hour = TRUE) +#' +#' emr_char2time(emr_time2char(c(t1, t2), show_hour = TRUE)) +#' +#' # Note that when show_hour = FALSE, the hour is set to 0 +#' # and therefore the results would be different from the original time values +#' emr_char2time(emr_time2char(c(t1, t2))) +#' +#' @export +emr_time2char <- function(time, show_hour = FALSE) { + as.character(emr_time2posix(time, show_hour = show_hour)) +} + +#' @rdname emr_time2posix +#' @export +emr_posix2time <- function(posix) { + day <- as.numeric(format(posix, "%d")) + month <- as.numeric(format(posix, "%m")) + year <- as.numeric(format(posix, "%Y")) + hour <- as.numeric(format(posix, "%H")) + + return(emr_date2time(day, month, year, hour)) +} + +#' @rdname emr_time2char +#' @export +emr_char2time <- function(char) { + return(emr_posix2time(as.POSIXct(char))) +} + #' Convert time periods to internal time format #' #' Convert time periods to internal time format diff --git a/man/emr_time2char.Rd b/man/emr_time2char.Rd new file mode 100644 index 0000000..ad85a61 --- /dev/null +++ b/man/emr_time2char.Rd @@ -0,0 +1,40 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/time.R +\name{emr_time2char} +\alias{emr_time2char} +\alias{emr_char2time} +\title{Convert time to character format} +\usage{ +emr_time2char(time, show_hour = FALSE) + +emr_char2time(char) +} +\arguments{ +\item{time}{The time value to be converted.} + +\item{show_hour}{Logical value indicating whether to include the hour in the output. Default is FALSE.} + +\item{char}{A character string to be converted to EMR time.} +} +\value{ +A character string representing the converted time value. +} +\description{ +This function converts a given time value to a character format in the form of "%Y-%m-%d" or "%Y-%m-%d %H:%M:%S" depending on the value of the `show_hour` parameter. +} +\examples{ +# 30 January, 1938, 6:00 - birthday of Islam Karimov +t1 <- emr_date2time(30, 1, 1938, 6) +# September 2, 2016, 7:00 - death of Islam Karimov +t2 <- emr_date2time(2, 9, 2016, 7) + +emr_time2char(c(t1, t2)) +emr_time2char(c(t1, t2), show_hour = TRUE) + +emr_char2time(emr_time2char(c(t1, t2), show_hour = TRUE)) + +# Note that when show_hour = FALSE, the hour is set to 0 +# and therefore the results would be different from the original time values +emr_char2time(emr_time2char(c(t1, t2))) + +} diff --git a/man/emr_time2posix.Rd b/man/emr_time2posix.Rd new file mode 100644 index 0000000..6b0e823 --- /dev/null +++ b/man/emr_time2posix.Rd @@ -0,0 +1,42 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/time.R +\name{emr_time2posix} +\alias{emr_time2posix} +\alias{emr_posix2time} +\title{Convert EMR time to POSIXct} +\usage{ +emr_time2posix(time, show_hour = FALSE, tz = "UTC") + +emr_posix2time(posix) +} +\arguments{ +\item{time}{The EMR time to be converted.} + +\item{show_hour}{Logical value indicating whether to include the hour in the output. Default is FALSE.} + +\item{tz}{Time zone to be used for the output POSIXct object. Default is "UTC".} + +\item{posix}{A POSIXct object to be converted to EMR time.} +} +\value{ +A POSIXct object representing the converted time. +} +\description{ +These function converts EMR time to POSIXct format. It takes the EMR time as input and returns the corresponding POSIXct object. +} +\examples{ +# 30 January, 1938, 6:00 - birthday of Islam Karimov +t1 <- emr_date2time(30, 1, 1938, 6) +# September 2, 2016, 7:00 - death of Islam Karimov +t2 <- emr_date2time(2, 9, 2016, 7) + +emr_time2posix(c(t1, t2)) +emr_time2posix(c(t1, t2), show_hour = TRUE) + +emr_posix2time(emr_time2posix(c(t1, t2), show_hour = TRUE)) + +# Note that when show_hour = FALSE, the hour is set to 0 +# and therefore the results would be different from the original time values +emr_posix2time(emr_time2posix(c(t1, t2))) + +} diff --git a/tests/testthat/test-date2time.R b/tests/testthat/test-date2time.R index ce7c54f..d1b3bf8 100644 --- a/tests/testthat/test-date2time.R +++ b/tests/testthat/test-date2time.R @@ -211,3 +211,57 @@ test_that("periodic iterators fail when not given etime and n", { expect_error(emr_monthly_iterator(emr_date2time(1, 1, 2002))) expect_error(emr_yearly_iterator(emr_date2time(1, 1, 2002))) }) + +test_that("emr_time2posix returns correct POSIXct object without hour", { + time <- emr_date2time(30, 1, 1938, 6) + expected <- as.POSIXct("1938-01-30", format = "%Y-%m-%d", tz = "UTC") + result <- emr_time2posix(time, show_hour = FALSE) + expect_equal(result, expected) +}) + +test_that("emr_time2posix returns correct POSIXct object with hour", { + time <- emr_date2time(30, 1, 1938, 6) + expected <- as.POSIXct("1938-01-30 06:00:00", format = "%Y-%m-%d %H:%M:%S", tz = "UTC") + result <- emr_time2posix(time, show_hour = TRUE) + expect_equal(result, expected) +}) + +test_that("emr_time2char returns correct character string without hour", { + time <- emr_date2time(30, 1, 1938, 6) + expected <- "1938-01-30" + result <- emr_time2char(time, show_hour = FALSE) + expect_equal(result, expected) +}) + +test_that("emr_time2char returns correct character string with hour", { + time <- emr_date2time(30, 1, 1938, 6) + expected <- "1938-01-30 06:00:00" + result <- emr_time2char(time, show_hour = TRUE) + expect_equal(result, expected) +}) + +# Test emr_time2posix function +test_that("emr_time2posix converts EMR time to POSIXct", { + # Test case 1: EMR time with hour + time1 <- emr_date2time(30, 1, 1938, 6) + expected1 <- as.POSIXct("1938-01-30 06:00:00", tz = "UTC") + expect_equal(emr_time2posix(time1, show_hour = TRUE), expected1) + + # Test case 2: EMR time without hour + time2 <- emr_date2time(2, 9, 2016, 0) + expected2 <- as.POSIXct("2016-09-02", tz = "UTC") + expect_equal(emr_time2posix(time2, show_hour = FALSE), expected2) +}) + +# Test emr_time2char function +test_that("emr_time2char converts time to character format", { + # Test case 1: Time with hour + time1 <- emr_date2time(30, 1, 1938, 6) + expected1 <- "1938-01-30 06:00:00" + expect_equal(emr_time2char(time1, show_hour = TRUE), expected1) + + # Test case 2: Time without hour + time2 <- emr_date2time(2, 9, 2016, 0) + expected2 <- "2016-09-02" + expect_equal(emr_time2char(time2, show_hour = FALSE), expected2) +})