-
Notifications
You must be signed in to change notification settings - Fork 2
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
Bug 17672: seq.Date should accept to,by,length.out (i.e., no need for from) #46
Comments
This is an opportunity to extend the functionality of |
Working on this with @shannonpileggi Here are some examples and expected outputs to = as.Date("2024-08-15")
# by various days
seq(to = to, by = 'day', length.out = 3)
# [1] "2024-08-13" "2024-08-14" "2024-08-15"
seq(to = to, by = '2 days', length.out = 3)
# [1] "2024-08-11" "2024-08-13" "2024-08-15"
seq(to = to, by = 3, length.out = 3)
# [1] "2024-08-09" "2024-08-12" "2024-08-15"
# backwards
seq(to = to, by = "-4 days", length.out=3)
# [1] "2024-08-23" "2024-08-19" "2024-08-15"
# non-integer 'by'
seq(to = to, by = 1.5, length.out = 3)
# [1] "2024-08-12" "2024-08-13" "2024-08-15"
# other by=STRING
seq(to = to, by = "2 weeks", length.out = 3)
[1] "2024-07-18" "2024-08-01" "2024-08-15"
# NB: month,quarter,year are "irregular" in terms of the underlying count of days
seq(to = to, by = "month", length.out = 3)
[1] "2024-06-15" "2024-07-15" "2024-08-15"
seq(to = to, by = "quarter", length.out = 3)
[1] "2024-02-15" "2024-05-15" "2024-08-15"
seq(to = to, by = "year", length.out = 3)
[1] "2022-08-15" "2023-08-15" "2024-08-15"
# by=difftime()
seq(to = to, by = as.difftime(1, units='days'), length.out = 3)
[1] "2024-08-13" "2024-08-14" "2024-08-15"
seq(to = to, by = as.difftime(-1, units='weeks'), length.out = 3)
[1] "2024-08-29" "2024-08-22" "2024-08-15" |
One tangential note: almost surely, this code should be replaced by dispatching: if (inherits(by, "difftime")) {
by <- switch(attr(by,"units"), secs = 1/86400, mins = 1/1440,
hours = 1/24, days = 1, weeks = 7) * unclass(by)
} Can be replaced by: if (inherits(by, "difftime")) by <- as.double(by, units="days") This is shorter & doesn't hardcode the logic for this conversion in a second place. For the record, the logic is handled by |
One branch in seq(as.Date("2011-01-07"), as.Date("2011-03-01"), by = "month")
# [1] "2011-01-07" "2011-02-07" I think this error only applies when both Note that this matches the similar seq(1, 3, by=5)
# [1] 1 |
Current status: The logic for the cases of Of course, there's no reason to support inferring Given the substantial overlap of logic of the two functions, we discussed some options with @lawremi:
We'll go ahead with the last option, and evaluate later if the overhead is noticeable / should be reduced with some smarter approach (e.g., we can check if |
not sure if this helps!
|
Work on this issue has also exposed another (very minor) bug(ish): |
Linking the preliminary patch on r-svn: I have some tests (added to tests/datetime3.R), but feel like I came up short on other possibilities to check for regression. @shannonpileggi any suggestions for new cases to check? |
To bolster some more confidence in the patch, I found some 33 CRAN packages relying on https://gist.github.com/MichaelChirico/aedcc59a07d49800bcce3be71400cee1 |
Seems this could be ready to post back on Bugzilla? |
Yep, I was waiting a few days to let things simmer. Posted now: https://bugs.r-project.org/show_bug.cgi?id=17672 |
Bug 17672 - seq.Date should accept to,by,length.out (i.e., no need for from
The idea is for this to "just work":
seq.default()
is perfectly capable of doing so:The workaround is far less readable:
And requires us to do a bunch of arithmetic to figure out
from
in the generalSys.Date(to = t, length.out=n, by='k days')
.The text was updated successfully, but these errors were encountered: