From 392eae60542b82f93c7b2697b1b6c9bd9fc326f2 Mon Sep 17 00:00:00 2001 From: Alun Hewinson Date: Tue, 29 Sep 2015 10:08:37 +0100 Subject: [PATCH 1/2] bugfix (issue 336) on coercion from interval to period If the sign of the month and day of the new period are opposite to each other, set @day to zero. https://github.com/hadley/lubridate/issues/336 --- R/coercion.r | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/R/coercion.r b/R/coercion.r index 8f826e93..8e5601c7 100644 --- a/R/coercion.r +++ b/R/coercion.r @@ -441,8 +441,10 @@ setMethod("as.period", signature(x = "Interval"), function(x, unit = NULL, ...) to.per$month[nmons] <- 12 + to.per$month[nmons] to.per$year[nmons] <- to.per$year[nmons] - 1 - new("Period", to.per$second, year = to.per$year, month = to.per$month, - day = to.per$day, hour = to.per$hour, minute = to.per$minute) + np <- new("Period", to.per$second, year = to.per$year, month = to.per$month, + day = to.per$day, hour = to.per$hour, minute = to.per$minute) + if (abs(sign(np@month) - sign(np@day))) np@day <- 0 + np } setMethod("as.period", signature(x = "Duration"), function(x, unit = NULL, ...) { From ffddfdf5a17b9008b93138c08cf32b5ee975f542 Mon Sep 17 00:00:00 2001 From: Alun Hewinson Date: Tue, 29 Sep 2015 11:42:54 +0100 Subject: [PATCH 2/2] [Fix 336] fix handles NAs in period; corrected missing "== 2" in sign test --- R/coercion.r | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/R/coercion.r b/R/coercion.r index 8e5601c7..413a531b 100644 --- a/R/coercion.r +++ b/R/coercion.r @@ -441,9 +441,10 @@ setMethod("as.period", signature(x = "Interval"), function(x, unit = NULL, ...) to.per$month[nmons] <- 12 + to.per$month[nmons] to.per$year[nmons] <- to.per$year[nmons] - 1 - np <- new("Period", to.per$second, year = to.per$year, month = to.per$month, + np <- new("Period", to.per$second, year = to.per$year, month = to.per$month, day = to.per$day, hour = to.per$hour, minute = to.per$minute) - if (abs(sign(np@month) - sign(np@day))) np@day <- 0 + if (is.na(np@month) | is.na(np@day)) return(np) + if (abs(sign(np@month) - sign(np@day)) == 2) np@day <- 0 np }