Skip to content

Commit

Permalink
Merge pull request #106 from trevorld/difftime
Browse files Browse the repository at this point in the history
feat: Add 'as.nanoduration.difftime()'
  • Loading branch information
eddelbuettel authored Oct 15, 2022
2 parents bcabc41 + a5c2fec commit 8bf18bd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2022-10-14 Trevor L Davis <trevor.l.davis@gmail.com>

* R/nanoduration.R (as.nanoduration.difftime): Added
* man/nanoduration.Rd: Updated
* inst/tinytest/test_nanoduration.R: Add tests

2022-10-13 Trevor L Davis <trevor.l.davis@gmail.com>

* R/nanoduration.R (nanoduration): Add default arguments equal to zero
Expand Down
13 changes: 13 additions & 0 deletions R/nanoduration.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ setMethod("as.nanoduration",

setAs("integer", "nanoduration", function(from) as.nanoduration(from))

##' @rdname nanoduration
setMethod("as.nanoduration",
"difftime",
function(x) {
x <- as.numeric(x, units = "secs")
s <- as.integer(x) # seconds
n <- as.integer(1e9 * (x - s)) # nanoseconds
nanoduration(seconds = s, nanoseconds = n)
})

setAs("difftime", "nanoduration", function(from) as.nanoduration(from))


##' @rdname nanoduration
setMethod("as.nanoduration",
"NULL",
Expand Down
8 changes: 8 additions & 0 deletions inst/tinytest/test_nanoduration.R
Original file line number Diff line number Diff line change
Expand Up @@ -613,3 +613,11 @@ expect_identical(nano_floor(as.nanotime("2010-10-10 12:23:23.123456789 UTC"), as
## rep
expect_identical(rep(as.nanoduration(1), 2), as.nanoduration(rep(1,2)))
expect_identical(rep(as.nanoduration(1:2), each=2), as.nanoduration(rep(1:2, each=2)))

## difftime
expect_identical(as.nanoduration(as.difftime(2, units = "secs")),
nanoduration(seconds = 2))
expect_identical(as.nanoduration(as.difftime(2.5, units = "secs")),
nanoduration(seconds = 2, nanoseconds = 5e8))
expect_identical(as.nanoduration(as.difftime(2, units = "hours")),
nanoduration(hours = 2))
3 changes: 3 additions & 0 deletions man/nanoduration.Rd

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

0 comments on commit 8bf18bd

Please sign in to comment.