From 2cd8874445e37da2715ba528341c5fa464e7fc93 Mon Sep 17 00:00:00 2001 From: Vitalie Spinu Date: Wed, 3 Aug 2016 15:16:10 +0200 Subject: [PATCH] [Fix #442] Implement yq parser --- NAMESPACE | 1 + R/parse.r | 6 ++++-- man/parse_date_time.Rd | 16 ++++++++++++---- man/ymd.Rd | 3 +++ tests/testthat/test-parsers.R | 2 ++ 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index c79dde66..7f329c32 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -218,6 +218,7 @@ export(ymd) export(ymd_h) export(ymd_hm) export(ymd_hms) +export(yq) exportClasses(Duration) exportClasses(Interval) exportClasses(Period) diff --git a/R/parse.r b/R/parse.r index e846b558..c6074f8b 100644 --- a/R/parse.r +++ b/R/parse.r @@ -90,6 +90,10 @@ dmy <- function(..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME") dym <- function(..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME"), truncated = 0) .parse_xxx(..., orders = "dym", quiet = quiet, tz = tz, locale = locale, truncated = truncated) +#' @export +#' @rdname ymd +yq <- function(..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME")) + .parse_xxx(..., orders = "yq", quiet = quiet, tz = tz, locale = locale, truncated = 0) ##' Parse dates that have hours, minutes, or seconds elements. ##' @@ -809,8 +813,6 @@ fast_strptime <- function(x, format, tz = "UTC", lt = TRUE){ } .parse_xxx <- function(..., orders, quiet, tz = NULL, locale = locale, truncated){ - ## if(!missing(tz)) - ## .deprecated_arg("tz", "1.5.6", 2) dates <- unlist(lapply(list(...), .num_to_date), use.names = FALSE) if(is.null(tz)){ as.Date.POSIXct(parse_date_time(dates, orders, quiet = quiet, tz = "UTC", diff --git a/man/parse_date_time.Rd b/man/parse_date_time.Rd index 6ad9aea0..847ebd06 100644 --- a/man/parse_date_time.Rd +++ b/man/parse_date_time.Rd @@ -95,10 +95,10 @@ recursively on the input vector. numeric. The list below contains formats recognized by lubridate. For numeric formats -leading 0s are optional. In contrast to \code{strptime}, some of the formats -have been extended for efficiency reasons. They are marked with "*". Fast -parsers, \code{parse_date_time2} and \code{fast_strptime}, currently -accept only formats marked with "!". +leading 0s are optional. As compared to base \code{strptime}, some of the +formats are new or have been extended for efficiency reasons. These formats +are marked with "*". Fast parsers, \code{parse_date_time2} and +\code{fast_strptime}, currently accept only formats marked with "!". \describe{ \item{\code{a}}{Abbreviated weekday name in the current locale. (Also matches full name)} @@ -121,6 +121,8 @@ automatically handled if \code{preproc_wday = TRUE}} \item{\code{j}}{Day of year as decimal number (001--366 or 1--366).} +\item{\code{q}!*}{Quarter (1-4). The quarter month is added to parsed month if \code{m} format is present.} + \item{\code{m}!*}{Month as decimal number (01--12 or 1--12). For \code{parse_date_time}, also matches abbreviated and full months names as \code{b} and \code{B} formats.} @@ -217,6 +219,12 @@ parse_date_time(c('12/17/1996 04:00:00','4/18/1950 0130'), c('\%m/\%d/\%Y \%I:\%M:\%S','\%m/\%d/\%Y \%H\%M'), exact = TRUE) ## [1] "1996-12-17 04:00:00 UTC" "1950-04-18 01:30:00 UTC" +## ** quarters and partial dates ** +parse_date_time(c("2016.2", "2016-04"), orders = "Yq") +## [1] "2016-04-01 UTC" "2016-10-01 UTC" +parse_date_time(c("2016", "2016-04"), orders = c("Y", "Ym")) +## [1] "2016-01-01 UTC" "2016-04-01 UTC" + ## ** fast parsing ** \dontrun{ options(digits.secs = 3) diff --git a/man/ymd.Rd b/man/ymd.Rd index 4d2a575e..749d1a6c 100644 --- a/man/ymd.Rd +++ b/man/ymd.Rd @@ -7,6 +7,7 @@ \alias{myd} \alias{ydm} \alias{ymd} +\alias{yq} \title{Parse dates according to the order in that year, month, and day elements appear in the input vector.} \usage{ @@ -27,6 +28,8 @@ dmy(..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME"), dym(..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME"), truncated = 0) + +yq(..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME")) } \arguments{ \item{...}{a character or numeric vector of suspected dates} diff --git a/tests/testthat/test-parsers.R b/tests/testthat/test-parsers.R index 97c98edc..ad52fc0d 100644 --- a/tests/testthat/test-parsers.R +++ b/tests/testthat/test-parsers.R @@ -491,6 +491,8 @@ test_that("Quarters are parsed correctly", { out <- ymd(c("2016-01-01 UTC", "2016-04-01 UTC", "2016-07-01 UTC", "2016-10-01 UTC", NA), tz = "UTC") expect_equal(parse_date_time2(qs, orders = "Yq"), out) expect_equal(parse_date_time(qs, orders = "Yq"), out) + expect_equal(yq(qs, tz = "UTC"), out) + expect_equal(yq("16.1", "17.3", "2016.1"), ymd(c("2016-01-01", "2017-07-01", "2016-01-01"))) }) test_that( "Vectors of NA's are parsed as vectors of NA's", {