From 855127d599b20f26d79cb01ed899dd437ae81b1f Mon Sep 17 00:00:00 2001 From: "dyfan.jones" Date: Tue, 30 Jul 2024 10:17:46 +0100 Subject: [PATCH 1/3] simplify unix time --- paws.common/R/time.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/paws.common/R/time.R b/paws.common/R/time.R index f9b42039e..25b8b578f 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(0, tz = "GMT") + as.numeric(sec)) } From 89e58dc9722843948dcb1c3db1696500d0f3c4df Mon Sep 17 00:00:00 2001 From: "dyfan.jones" Date: Tue, 30 Jul 2024 10:25:07 +0100 Subject: [PATCH 2/3] tidy up --- paws.common/R/time.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paws.common/R/time.R b/paws.common/R/time.R index 25b8b578f..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) { # origin: 1970-01-01 00:00:00 - return(.POSIXct(0, tz = "GMT") + as.numeric(sec)) + return(.POSIXct(as.numeric(sec), tz = "GMT")) } From 3b7052e3dd0f8ab89b5d80faef8cf7c2a93e77fa Mon Sep 17 00:00:00 2001 From: "dyfan.jones" Date: Tue, 30 Jul 2024 11:21:06 +0100 Subject: [PATCH 3/3] add unit tests for unix_time --- paws.common/tests/testthat/test_dateutil.R | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) 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 + ) +})