Skip to content

Commit

Permalink
[Fix #597] as_datetime understands ymd strings
Browse files Browse the repository at this point in the history
  • Loading branch information
vspinu committed Oct 31, 2017
1 parent 5b9ccb3 commit e53e101
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
7 changes: 7 additions & 0 deletions NEWS.md
@@ -1,3 +1,10 @@
Version 1.7.0.9000
==================

## BUG FIXES

* [#597](https://github.com/tidyverse/lubridate/issues/597) Fix incorrect parsing of `ymd` strings by `as_datetime`.

Version 1.7.0
=============

Expand Down
2 changes: 1 addition & 1 deletion R/coercion.r
Expand Up @@ -673,7 +673,7 @@ setMethod("as_datetime", "numeric",
#' @export
setMethod("as_datetime", "character",
function(x, tz = "UTC") {
parse_date_time(x, orders = c("ymdTz", "ymdT"), tz = tz)
parse_date_time(x, orders = c("ymdTz", "ymdT", "ymd"), tz = tz, train = FALSE)
})

#' @rdname as_date
Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/test-POSIXt.R
Expand Up @@ -56,3 +56,28 @@ test_that("addition of large seconds doesn't overflow", {
"2038-01-19 03:14:09", "2038-01-19 03:14:10"))
expect_equal(from_period, from_char)
})

test_that("as_datetime works correctly", {

x <- c("17-01-20", "2017-01-20 01:02:03", "2017-03-22T15:48:00.000Z",
"2017-01-20", "2017-01-20 01:02:03", "2017-03-22T15:48:00.000Z")

y <- c("2017-01-20 00:00:00 UTC", "2017-01-20 01:02:03 UTC",
"2017-03-22 15:48:00 UTC", "2017-01-20 00:00:00 UTC",
"2017-01-20 01:02:03 UTC", "2017-03-22 15:48:00 UTC")

zns <- c(3, 6)
putc <- ymd_hms(y)
pus <- ymd_hms(y[-zns], tz = "America/Chicago")
expect_equal(as_datetime(x), putc)
expect_equal(as_datetime(x[-zns], tz = "America/Chicago"), pus)

for (i in seq_along(x)) {
expect_equal(as_datetime(x[[i]]), putc[[i]])
}

for (i in seq_along(pus)) {
expect_equal(as_datetime(x[-zns][[i]], tz = "America/Chicago"), pus[[i]])
}

})

0 comments on commit e53e101

Please sign in to comment.