Skip to content

Commit

Permalink
Merge pull request #119 from eddelbuettel/118-asdate-does-not-handle-…
Browse files Browse the repository at this point in the history
…the-case-where-tz-is-a-vector

'as.Date' forces use of 'tz' and can handle 'tz' as a vector; closes …
  • Loading branch information
lsilvest authored Oct 2, 2023
2 parents 3c07620 + d0e4876 commit e21145e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 4 deletions.
17 changes: 17 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
2023-09-26 Leonardo Silvestri <lsilvestri@ztsdb.org>

* R/nanotime.R: Further refinement for default UTC timezone
* inst/tinytest/test_nanotime.R: Idem

2023-09-22 Dirk Eddelbuettel <edd@debian.org>

* R/nanotime.R: Provide default UTC timezone in 'as.Date()'
* tests/simpleTests.R: Adjist a test accordingly
* inst/tinytest/test_nanotime.R: Idem

2023-09-21 Leonardo Silvestri <lsilvestri@ztsdb.org>

* DESCRIPTION (Version, Date): Roll minor version
* R/nanotime.R: Fixed 'as.Date' to force use of timezone and to
allow the timezone argument to be a vector

2023-07-11 Dirk Eddelbuettel <edd@debian.org>

* README.md: Add r-universe badge
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: nanotime
Type: Package
Title: Nanosecond-Resolution Time Support for R
Version: 0.3.7.3
Date: 2023-07-06
Version: 0.3.7.4
Date: 2023-09-21
Author: Dirk Eddelbuettel and Leonardo Silvestri
Maintainer: Dirk Eddelbuettel <edd@debian.org>
Description: Full 64-bit resolution date and time functionality with
Expand Down
13 changes: 12 additions & 1 deletion R/nanotime.R
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,18 @@ setAs("nanotime", "POSIXlt", function(from) as.POSIXlt.nanotime(from))

##' @rdname nanotime
as.Date.nanotime <- function(x, ...) {
as.Date(as.POSIXct(x))
arguments <- list(...)
if (!("tz" %in% names(args))) {
arguments <- c(arguments, list(tz="UTC"))
}
other_args <- setdiff(names(arguments), list('x', 'tz'))
if (length(other_args) > 0) {
stop(paste("'as.Date' called with arguments other than 'tz': ",
paste0("'", other_args, "'", collapse=", ")))
}
as.Date(ISOdate(year = nano_year(x, arguments$tz),
month = nano_month(x, arguments$tz),
day = nano_mday(x, arguments$tz)))
}

setAs("nanotime", "Date", function(from) as.Date(as.POSIXct(from)))
Expand Down
2 changes: 2 additions & 0 deletions inst/NEWS.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
\item New \code{accurate} parameter for conversion from \code{POSIXct} to
\code{nanotime} (Davor Josipovic and Leonardo in \ghpr{116} closing
\ghit{115})
\item The \code{as.Date()} function is now vectorized and can take a TZ
argument (Leonardo and Dirk in \ghpr{119} closing \ghit{118})
}
}

Expand Down
14 changes: 13 additions & 1 deletion inst/tinytest/test_nanotime.R
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,19 @@ expect_equal(p, as.POSIXct("1970-01-01 00:00:00", tz="UTC"), check.tzone=FALSE)
options(nanotimeTz=oldTz)

##test_as_Date <- function() {
expect_identical(as.Date(nanotime(0)), as.Date("1970-01-01"))
## test scalar
nanotime_scalar <- nanotime("2023-09-21T22:00:00 America/New_York")
expect_identical(as.Date(nanotime_scalar, tz="UTC"), as.Date("2023-09-22"))
## test vector
nanotime_vec <- rep(nanotime("2023-09-21T22:00:00 America/New_York"), 2)
tz_vec <- c("UTC", "America/New_York")
expect_identical(as.Date(nanotime_vec, tz=tz_vec), as.Date(c("2023-09-22", "2023-09-21")))
## test missing argument 'tz': now UTC filled in
expect_silent(as.Date(nanotime_vec))
expect_identical(as.Date(nanotime_vec), as.Date(nanotime_vec, tz="UTC"))
## test exception for extraneous argument:
expect_error(as.Date(nanotime_vec, y=2, z=3), "'as.Date' called with arguments other than 'tz': 'y', 'z'")
expect_silent(as.Date(x=nanotime_vec)) # check we can still name 'x' without an exception


## c, subset, subassign and binds
Expand Down
1 change: 1 addition & 0 deletions tests/simpleTests.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ as.POSIXct(x+1000)
as.POSIXlt(x)
as.POSIXlt(x+1000)
as.Date(x)
as.Date(x, tz="UTC")
options("digits.secs"=od)


Expand Down

0 comments on commit e21145e

Please sign in to comment.