Skip to content

Commit

Permalink
Merge pull request #814 from DyfanJones/main
Browse files Browse the repository at this point in the history
simplify unix time
  • Loading branch information
DyfanJones authored Jul 30, 2024
2 parents 3260119 + 3b7052e commit d5331a6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions paws.common/R/time.R
Original file line number Diff line number Diff line change
@@ -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"))
}
36 changes: 36 additions & 0 deletions paws.common/tests/testthat/test_dateutil.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
})

0 comments on commit d5331a6

Please sign in to comment.