Skip to content

Commit

Permalink
'as.Date' forces use of 'tz' and can handle 'tz' as a vector; closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
lsilvest committed Sep 22, 2023
1 parent 3c07620 commit 3a9edde
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
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
8 changes: 7 additions & 1 deletion R/nanotime.R
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,13 @@ setAs("nanotime", "POSIXlt", function(from) as.POSIXlt.nanotime(from))

##' @rdname nanotime
as.Date.nanotime <- function(x, ...) {
as.Date(as.POSIXct(x))
args <- list(...)
if (!("tz" %in% names(args))) {
stop("'as.Date' call is missing mandatory timezone argument 'tz'")
}
as.Date(ISOdate(year = nano_year(x, args$tz),
month = nano_month(x, args$tz),
day = nano_mday(x, args$tz)))
}

setAs("nanotime", "Date", function(from) as.Date(as.POSIXct(from)))
Expand Down
11 changes: 9 additions & 2 deletions inst/tinytest/test_nanotime.R
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,15 @@ 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':
expect_error(as.Date(nanotime_vec), "'as.Date' call is missing mandatory timezone argument 'tz'")

## c, subset, subassign and binds
##test_c <- function() {
Expand Down
2 changes: 1 addition & 1 deletion tests/simpleTests.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ as.POSIXct(x)
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 3a9edde

Please sign in to comment.