/
accessors-minute.r
68 lines (59 loc) · 1.45 KB
/
accessors-minute.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#' @include periods.r
NULL
#' Get/set minutes component of a date-time
#'
#' Date-time must be a POSIXct, POSIXlt, Date, Period, chron, yearmon, yearqtr, zoo,
#' zooreg, timeDate, xts, its, ti, jul, timeSeries, and fts objects.
#'
#' @export
#' @param x a date-time object
#' @param value numeric value to be assigned
#' @keywords utilities manip chron methods
#' @return the minutes element of x as a decimal number
#' @examples
#' x <- ymd("2012-03-26")
#' minute(x)
#' minute(x) <- 1
#' minute(x) <- 61
#' minute(x) > 2
minute <- function(x) {
UseMethod("minute")
}
#' @export
minute.default <- function(x) {
as.POSIXlt(x, tz = tz(x))$min
}
#' @export
minute.Period <- function(x) {
slot(x, "minute")
}
#' @rdname minute
#' @export
setGeneric("minute<-",
function (x, value) standardGeneric("minute<-"),
useAsDefault = function(x, value) {
y <- update_datetime(as.POSIXct(x), minutes = value)
reclass_date(y, x)
}
)
#' @export
setMethod("minute<-", "Duration", function(x, value) {
x <- x + minutes(value - minute(x))
})
#' @export
setMethod("minute<-", signature("Period"), function(x, value) {
slot(x, "minute") <- value
x
})
#' @export
setMethod("minute<-", "Interval", function(x, value) {
x <- x + minutes(value - minute(x))
})
#' @export
setMethod("minute<-", "POSIXt", function(x, value) {
update_datetime(x, minutes = value)
})
#' @export
setMethod("minute<-", "Date", function(x, value) {
update_datetime(x, minutes = value)
})