Skip to content

Commit

Permalink
Added emr_time2posix, emr_posix2time, emr_time2char and `emr_ch…
Browse files Browse the repository at this point in the history
…ar2time` functions
  • Loading branch information
aviezerl committed Dec 18, 2023
1 parent 483001c commit 5c3cb09
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 4 deletions.
6 changes: 3 additions & 3 deletions CRAN-SUBMISSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Version: 2.6.23
Date: 2023-08-17 11:47:19 UTC
SHA: 2637c4a6ad8d382709ba393643502f955e0c1f9d
Version: 2.6.25
Date: 2023-11-29 12:35:55 UTC
SHA: 318f25a5427d986616851182ff78ae943bb0dbf3
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = "aut"),
person("Aviezer", "Lifshitz", , "[email protected]", role = c("aut", "cre")),
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
85 changes: 85 additions & 0 deletions R/time.R
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,91 @@ 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".
#'
#' @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.
#'
#' @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, tz = "UTC") {
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, tz = "UTC") {
return(emr_posix2time(as.POSIXct(char, tz = tz)))
}

#' Convert time periods to internal time format
#'
#' Convert time periods to internal time format
Expand Down
38 changes: 38 additions & 0 deletions man/emr_time2char.Rd

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

40 changes: 40 additions & 0 deletions man/emr_time2posix.Rd

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

54 changes: 54 additions & 0 deletions tests/testthat/test-date2time.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

0 comments on commit 5c3cb09

Please sign in to comment.