Skip to content

Commit

Permalink
[Fix #236] Don't allow zeros in month and day during parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
vspinu committed Jun 29, 2014
1 parent 1a805b0 commit a739131
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 10 additions & 0 deletions inst/tests/test-parsers.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ test_that("ymd functions give warning when parsing absurd formats", {
expect_warning(ymd(c(201001024, 20100103)))
})

test_that("0 month and 0 day in date produces NA",
{
expect_equal(ymd(c("2013-1-1", "2013-0-1"), quiet = TRUE),
as.POSIXct(c("2013-01-01", NA), tz = "UTC"))
expect_equal(ymd(c("2013-1-1", "2013-1-0"), quiet = TRUE),
as.POSIXct(c("2013-01-01", NA), tz = "UTC"))
expect_equal(ymd("2013-1-0", quiet = TRUE),
as.POSIXct(as.character(NA), tz = "UTC"))
})

test_that("ymd_hms correctly handles a variety of formats", {
expect_that(ymd_hms("2010-01-02 23:59:59"), equals(as.POSIXct(
"2010-01-02 23:59:59", tz = "UTC")))
Expand Down
8 changes: 4 additions & 4 deletions src/tparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,16 @@ SEXP parse_dt(SEXP str, SEXP ord, SEXP formats) {
break;
case 'y': // year in yy format
PARSENUM(y, 2);
if (y < 100) y += 2000;
if ( y < 100 ) y += 2000;
break;
case 'm': // month
PARSENUM(m, 2);
if (0 < m && m < 13) secs += sm[m];
if ( 0 < m && m < 13 ) secs += sm[m];
else succeed = 0;
break;
case 'd': // day
PARSENUM(d, 2);
if ( d < 32 ) secs += (d - 1) * 86400;
if ( 0 < d && d < 32 ) secs += (d - 1) * 86400;
else succeed = 0;
break;
case 'H': // hour 24
Expand All @@ -132,7 +132,7 @@ SEXP parse_dt(SEXP str, SEXP ord, SEXP formats) {
PARSENUM(S, 2);
if ( S < 62 ){ // allow leap seconds
secs += S;
if(O_format){
if( O_format ){
// Parse milliseconds; both . and , as decimal separator are allowed
if( *c == '.' || *c == ','){
double ms = 0.0, msfact = 0.1;
Expand Down

0 comments on commit a739131

Please sign in to comment.