Skip to content

Commit 248a0e8

Browse files
committed
Guess YYYYww numeric as "custom", fix incidental typo
- Previously we guessed YYYYww numeric as "year", almost surely wrong. But we can't easily/reliably tell what type of yearweek it is, so just use "custom" to avoid various/bad guesses in time_type-reliant code. - Don't use <int> <= <chr>; seemed nonimpactful in this case, at least in tested locale.
1 parent 2e694e5 commit 248a0e8

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: epiprocess
33
Title: Tools for basic signal processing in epidemiology
4-
Version: 0.7.7
4+
Version: 0.7.8
55
Authors@R: c(
66
person("Jacob", "Bien", role = "ctb"),
77
person("Logan", "Brooks", email = "[email protected]", role = c("aut", "cre")),

NEWS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ Pre-1.0.0 numbering scheme: 0.x will indicate releases, while 0.x.y will indicat
2727
the `epi_df` (#382).
2828
- Refactored internals to use `cli` for warnings/errors and `checkmate` for
2929
argument checking (#413).
30-
- Fix logic to auto-assign `ep_df` `time_type` to `week` (#416).
30+
- Fix logic to auto-assign `epi_df` `time_type` to `week` (#416) and `year`
31+
(#441).
3132

3233
## Breaking changes
3334

R/utils.R

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ guess_geo_type <- function(geo_value) {
403403
guess_time_type <- function(time_value) {
404404
# Convert character time values to Date or POSIXct
405405
if (is.character(time_value)) {
406-
if (nchar(time_value[1]) <= "10") {
406+
if (nchar(time_value[1]) <= 10L) {
407407
new_time_value <- tryCatch(
408408
{
409409
as.Date(time_value)
@@ -440,14 +440,8 @@ guess_time_type <- function(time_value) {
440440
return("yearmonth")
441441
} else if (inherits(time_value, "yearquarter")) {
442442
return("yearquarter")
443-
}
444-
445-
# Else, if it's an integer that's at least 1582, then use "year"
446-
if (
447-
is.numeric(time_value) &&
448-
all(time_value == as.integer(time_value)) &&
449-
all(time_value >= 1582)
450-
) {
443+
} else if (rlang::is_integerish(time_value) &&
444+
all(nchar(as.character(time_value)) == 4L)) { # nolint: indentation_linter
451445
return("year")
452446
}
453447

tests/testthat/test-utils.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ test_that("guess_time_type works for different types", {
5656
yearquarters <- tsibble::yearquarter(10)
5757

5858
years <- c(1999, 2000)
59+
ambiguous_yearweeks <- c(199901, 199902) # -> "custom"
60+
61+
daytimes <- as.POSIXct(c("2022-01-01 05:00:00", "2022-01-01 15:0:00"), tz = "UTC")
62+
daytimes_chr <- as.character(daytimes)
5963

6064
# YYYY-MM-DD is the accepted format
6165
not_ymd1 <- "January 1, 2022"
@@ -72,13 +76,17 @@ test_that("guess_time_type works for different types", {
7276
expect_equal(guess_time_type(yearquarters), "yearquarter")
7377

7478
expect_equal(guess_time_type(years), "year")
79+
expect_equal(guess_time_type(ambiguous_yearweeks), "custom")
80+
81+
expect_equal(guess_time_type(daytimes), "day-time")
82+
expect_equal(guess_time_type(daytimes_chr), "day-time")
7583

7684
expect_equal(guess_time_type(not_ymd1), "custom")
7785
expect_equal(guess_time_type(not_ymd2), "custom")
7886
expect_equal(guess_time_type(not_ymd3), "custom")
7987
expect_equal(guess_time_type(not_a_date), "custom")
8088
})
81-
3
89+
8290
test_that("guess_time_type works with gaps", {
8391
days_gaps <- as.Date("2022-01-01") + c(0, 1, 3, 4, 8, 8 + 7)
8492
weeks_gaps <- as.Date("2022-01-01") + 7 * c(0, 1, 3, 4, 8, 8 + 7)

0 commit comments

Comments
 (0)