Skip to content

Commit

Permalink
Allow to parse dates from long numbers
Browse files Browse the repository at this point in the history
With as.character(), long numbers with many zeros at the end get
converted to scientific notation which the parse of course fails to
recognise. Using format(..., scientific = FALSE) prevents that.

trim = TRUE is necessary for variable length input.

Add tests for these problematic numbers. Tests also check wether
the behaviour of as.character() changes but format()
outperforms as.character() and may be preferred anyway.
  • Loading branch information
jiho committed Jan 30, 2015
1 parent 9d83127 commit 369b9cc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions NEWS
@@ -1,3 +1,8 @@

BUG FIXES

* allow to parse long numbers such as 20140911000000 as date+time (@jiho, #302)

Version 1.3.3
-------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion R/parse.r
Expand Up @@ -695,7 +695,7 @@ fast_strptime <- function(x, format, tz = "UTC"){
if (is.numeric(x)) {
out <- rep.int(as.character(NA), length(x))
nnas <- !is.na(x)
x <- as.character(x[nnas])
x <- format(x[nnas], scientific = FALSE, trim = TRUE)
x <- paste(ifelse(nchar(x) %% 2 == 1, "0", ""), x, sep = "")
out[nnas] <- x
out
Expand Down
14 changes: 14 additions & 0 deletions inst/tests/test-parsers.R
Expand Up @@ -156,6 +156,20 @@ test_that("ymd functions correctly parse dates with no separators and no quotes"
equals(as.POSIXct("2010-01-02 23:59:59", tz = "UTC")))
})

test_that("numbers are correctly converted into character for parsing", {
# numbers with 000000 at the end are wrongly converted by as.character
# which outputs scientific notation
expect_that(ymd_hms(20100102000000),
equals(as.POSIXct("2010-01-02 00:00:00", tz = "UTC")))
expect_that(.num_to_date(20100102000000),
equals("20100102000000"))
# check for a fix in as.character
# if as.character is fixed, consider using it in .num_to_date
expect_that(as.character(20100102000000),
equals("2.0100102e+13"))
expect_warning(ymd_hms(as.character(20100102000000)))
})

test_that("ymd functions parse absurd formats as NA's", {
## should not through errors, just as as.POSIX and strptime
pna <- as.POSIXct(as.POSIXlt(NA, tz = "UTC"))
Expand Down

0 comments on commit 369b9cc

Please sign in to comment.