-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
'as.Date' forces use of 'tz' and can handle 'tz' as a vector; closes … #119
'as.Date' forces use of 'tz' and can handle 'tz' as a vector; closes … #119
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's good and simple. I have one proposal though: would this be better?
edd@rob:~/git/nanotime(118-asdate-does-not-handle-the-case-where-tz-is-a-vector)$ git diff
diff --git a/R/nanotime.R b/R/nanotime.R
index 58c362e..494204a 100644
--- a/R/nanotime.R
+++ b/R/nanotime.R
@@ -339,6 +339,9 @@ setAs("nanotime", "POSIXlt", function(from) as.POSIXlt.nanotime(from))
##' @rdname nanotime
as.Date.nanotime <- function(x, ...) {
args <- list(...)
+ if (length(args) == 0) {
+ args <- list(tz="UTC")
+ }
if (!("tz" %in% names(args))) {
stop("'as.Date' call is missing mandatory timezone argument 'tz'")
}
diff --git a/inst/tinytest/test_nanotime.R b/inst/tinytest/test_nanotime.R
index 67fcf81..5d05eb1 100644
--- a/inst/tinytest/test_nanotime.R
+++ b/inst/tinytest/test_nanotime.R
@@ -352,7 +352,7 @@ 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'")
+#expect_error(as.Date(nanotime_vec), "'as.Date' call is missing mandatory timezone argument 'tz'")
## c, subset, subassign and binds
##test_c <- function() {
diff --git a/tests/simpleTests.R b/tests/simpleTests.R
index 2bf5fdc..8d96dc0 100644
--- a/tests/simpleTests.R
+++ b/tests/simpleTests.R
@@ -34,6 +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)
edd@rob:~/git/nanotime(118-asdate-does-not-handle-the-case-where-tz-is-a-vector)$
Passes all tests. Question is what we want nanotime(x)
to be.
R/nanotime.R
Outdated
} | ||
as.Date(ISOdate(year = nano_year(x, args$tz), | ||
month = nano_month(x, args$tz), | ||
day = nano_mday(x, args$tz))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nicely done.
@@ -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") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only thing I am wondering is ... that we are loosing an automatic, implied as.Date(x)
where the UTC timezone is given.
I think I can live with that (as I can live with |
Yes, I think you're right on this. By using a default of UTC, we have the same behavior as the generic |
setMethod("nanotime", "Date", function(from) nanotime(as.POSIXct(from))) I think we should be forcing an argument |
Happy to commit what I have into the branch. We could possible use an |
Yes, that would be great if you could commit what you have. Not so sure about the |
What are your thoughts on forcing the |
Not sure myself and we could delay that / decouple it from this PR. |
Agreed, let's decouple. |
I just pushed my changes from yesterday so now you can (informally, no button to click) my new changes to the PR. |
R/nanotime.R
Outdated
@@ -339,6 +339,9 @@ setAs("nanotime", "POSIXlt", function(from) as.POSIXlt.nanotime(from)) | |||
##' @rdname nanotime | |||
as.Date.nanotime <- function(x, ...) { | |||
args <- list(...) | |||
if (length(args) == 0) { | |||
args <- list(tz="UTC") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think line 343 could replace the stop
. That is, if we don't find tz
then we add it, so no exception. We could also potentially be stricter as the ellipsis for this case should either be no argument or tz
, but other argument names are not supported, so we might want to raise an exception if the user calls as.Date(some_nanotime, foo=1)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I committed the above so do you want to just commit another refining iteration? When I wrote it was mostly as a 'how about ...' to raise an argument.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
Got distracted again but I think we are ready to merge as is. I jusst added a quick extension to ChangeLog (for the last bit) and NEWS (for this PR). |
Yes, great, it looks all good, merging it. |
…#118
I think forcing the use of 'tz' is essential; we didn't enforce it before, so it would revert to the default way that
POSIXct
infers timezone which is exactly what we set out not to do innanotime
because we wanted timezones to always be explicit. I think it reduces a lot the subtle errors that invariable creep in when functions automatically assume something about timezones.The other improvement here of course is that 'tz' can now be a vector.