Skip to content

Commit

Permalink
[Fix #466] Correct formats in ymd_h family of functions
Browse files Browse the repository at this point in the history
  • Loading branch information
vspinu committed Sep 14, 2016
1 parent f3788e9 commit 1eb5a02
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
4 changes: 2 additions & 2 deletions R/parse.r
Expand Up @@ -204,7 +204,7 @@ dmy_hm <- function(..., quiet = FALSE, tz = "UTC", locale = Sys.getlocale("LC_TI
#' @export
#' @rdname ymd_hms
dmy_h <- function(..., quiet = FALSE, tz = "UTC", locale = Sys.getlocale("LC_TIME"), truncated = 0)
.parse_xxx_hms(..., orders = "dmyR", quiet = quiet, tz = tz, locale = locale, truncated = truncated)
.parse_xxx_hms(..., orders = "dmyr", quiet = quiet, tz = tz, locale = locale, truncated = truncated)

#' @export
#' @rdname ymd_hms
Expand Down Expand Up @@ -234,7 +234,7 @@ ydm_hm <- function(..., quiet = FALSE, tz = "UTC", locale = Sys.getlocale("LC_TI
#' @export
#' @rdname ymd_hms
ydm_h <- function(..., quiet = FALSE, tz = "UTC", locale = Sys.getlocale("LC_TIME"), truncated = 0)
.parse_xxx_hms(..., orders = "ydmR", quiet = quiet, tz = tz, locale = locale, truncated = truncated)
.parse_xxx_hms(..., orders = "ydmr", quiet = quiet, tz = tz, locale = locale, truncated = truncated)


##' @rdname hms
Expand Down
20 changes: 19 additions & 1 deletion tests/testthat/test-parsers.R
Expand Up @@ -268,6 +268,7 @@ test_that("ymd functions correctly parse dates with no separators and b and B fo
equals(as.Date("2010-01-02")))
})


test_that("ymd functions correctly parse dates with no separators and no quotes", {
expect_that(ymd(20100102),
equals(as.Date("2010-01-02")))
Expand Down Expand Up @@ -606,11 +607,28 @@ test_that("heterogeneous formats are correctly parsed", {
})

test_that("truncated formats are correctly parsed", {
x <- c("2011-12-31 12:59:59", "2010-01-01 12:11", "2010-01-01 12", "2010-01-01")

expect_that({
x <- c("2011-12-31 12:59:59", "2010-01-01 12:11", "2010-01-01 12", "2010-01-01")
ymd_hms(x, truncated = 3)
}, equals(as.POSIXct(c("2011-12-31 12:59:59", "2010-01-01 12:11:00", "2010-01-01 12:00:00",
"2010-01-01 00:00:00"), tz = "UTC")))

x <- c("2011-12-31 12", "2010-01-01 12", "2010-01-01 12")
expect_equal(ymd_h(x),
as.POSIXct(c("2011-12-31 12:00:00 UTC", "2010-01-01 12:00:00 UTC", "2010-01-01 12:00:00 UTC"),
tz = "UTC"))

x <- c("2011-12-31 12:01", "2010-01-01 12:02", "2010-01-01 12:03")
expect_equal(ymd_hm(x),
as.POSIXct(c("2011-12-31 12:01:00 UTC", "2010-01-01 12:02:00 UTC", "2010-01-01 12:03:00 UTC"),
tz = "UTC"))


expect_equal(dmy_h("05-07-2011 13"), ymd_hms("2011-07-05 13:00:00"))
expect_equal(ymd_h("2011-07-05 13"), ymd_hms("2011-07-05 13:00:00"))
expect_equal(dmy_hm("05-07-2011 13:02"), ymd_hms("2011-07-05 13:02:00"))
expect_equal(ymd_hm("2011-07-05 13:02"), ymd_hms("2011-07-05 13:02:00"))
})

test_that("truncation on non-dates results in NAs indeed", {
Expand Down

0 comments on commit 1eb5a02

Please sign in to comment.