Skip to content

Commit

Permalink
Use the same logic when guessing as we use when parsing
Browse files Browse the repository at this point in the history
Not sure why the logic ended up so much different here, I think it was
legacy behavior of some-kind

Fixes #240
  • Loading branch information
jimhester committed Jan 7, 2021
1 parent cdd7f04 commit 4d302ba
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
@@ -1,5 +1,7 @@
# vroom (development version)

* Datetime formats used when guessing now match those used when parsing (#240)

* `vroom_lines()` now works with empty files (#285)

* `vroom_write()` now works when the delimiter is empty, e.g. `delim = ""` (#287).
Expand Down
7 changes: 2 additions & 5 deletions src/guess_type.cc
Expand Up @@ -98,11 +98,8 @@ static bool isDateTime(const std::string& x, LocaleInfo* pLocale) {
if (!ok)
return false;

if (!parser.compactDate())
return true;

// Values like 00014567 are unlikely to be dates, so don't guess
return parser.year() > 999;
DateTime dt = parser.makeDateTime();
return dt.validDateTime();
}

std::string guess_type__(
Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/test-datetime.R
Expand Up @@ -218,7 +218,7 @@ test_that("DDDD-DD not parsed as date (i.e. doesn't trigger partial date match)"
})

test_that("leading zeros don't get parsed as date without explicit separator", {
expect_type(vroom("00010203\n", col_names = FALSE, delim = "\n", col_types = list())[[1]], "character")
expect_type(vroom("00010203\n", col_names = FALSE, delim = "\n", col_types = list())[[1]], "double")
expect_s3_class(vroom("0001-02-03\n", col_names = FALSE, delim = "\n", col_types = list())[[1]], "Date")
})

Expand All @@ -244,3 +244,7 @@ test_that("subsetting works with both double and integer indexes", {
expect_equal(x$X1[NA_integer_], na_dt)
expect_equal(x$X1[NA_real_], na_dt)
})

test_that("guessing datetime uses the same logic as parsing", {
expect_s3_class(vroom("date\n2015-06-14T09Z\n2015-06-14T09Z", delim=",")[[1]], "character")
})

0 comments on commit 4d302ba

Please sign in to comment.