Skip to content

Commit

Permalink
Merge branch 'feat@time-to-posix-and-character' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
aviezerl committed Dec 18, 2023
2 parents 214b5f3 + 51b3bc8 commit d86f7cb
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 4 deletions.
3 changes: 0 additions & 3 deletions CRAN-SUBMISSION

This file was deleted.

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
87 changes: 87 additions & 0 deletions R/time.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions man/emr_time2char.Rd

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

42 changes: 42 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 d86f7cb

Please sign in to comment.