diff --git a/paws.common/R/time.R b/paws.common/R/time.R index f9b42039e..b23c60c12 100644 --- a/paws.common/R/time.R +++ b/paws.common/R/time.R @@ -1,5 +1,5 @@ # Returns a date value, given the number of seconds since Jan 1, 1970. unix_time <- function(sec, nsec = 0) { - time <- ISOdatetime(1970, 1, 1, 0, 0, 0, tz = "GMT") + as.numeric(sec) - return(time) + # origin: 1970-01-01 00:00:00 + return(.POSIXct(as.numeric(sec), tz = "GMT")) } diff --git a/paws.common/tests/testthat/test_dateutil.R b/paws.common/tests/testthat/test_dateutil.R index 526893b6d..85cd2ed01 100644 --- a/paws.common/tests/testthat/test_dateutil.R +++ b/paws.common/tests/testthat/test_dateutil.R @@ -21,3 +21,39 @@ test_that("as_timestamp empty input", { exp <- as.POSIXct("", tz = "GMT", format = "foo") expect_equal(out, exp) }) + +test_that("unix_time check default value", { + actual <- unix_time(0) + expect <- as.POSIXct("1970-01-01 00:00:00", tz = "GMT") + expect_equal( + actual, + expect + ) +}) + +test_that("unix_time check numeric value", { + actual <- unix_time(1704067200) + expect <- as.POSIXct("2024-01-01 00:00:00", tz = "GMT") + expect_equal( + actual, + expect + ) +}) + +test_that("unix_time check string value", { + actual <- unix_time("") + expect <- as.POSIXct(NA, tz = "GMT") + expect_equal( + actual, + expect + ) +}) + +test_that("unix_time check null value", { + actual <- unix_time(NULL) + expect <- as.POSIXct(NULL, tz = "GMT") + expect_equal( + actual, + expect + ) +})