-
Notifications
You must be signed in to change notification settings - Fork 2
/
minute.R
87 lines (79 loc) · 1.89 KB
/
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#' Get and Set Minute Values
#'
#' Gets and sets minute values for date/time vectors.
#'
#' @inheritParams params
#' @param value A integer vector of the minute value(s).
#'
#' @return An integer vector (or the modified date/time vector).
#' @family set time
#' @seealso [dtt_minute_decimal()]
#' @export
#'
#' @examples
#' x <- as.POSIXct("1990-01-02 23:40:51")
#' dtt_minute(x)
#' dtt_minute(x) <- 27L
#' x
#'
#' x <- hms::as_hms("23:40:51")
#' dtt_minute(x)
#' dtt_minute(x) <- 27L
#' x
dtt_minute <- function(x, ...) {
UseMethod("dtt_minute")
}
#' @rdname dtt_minute
#' @export
`dtt_minute<-` <- function(x, value) {
UseMethod("dtt_minute<-")
}
#' @describeIn dtt_minute Get integer vector of minute values for a Date vector
#' @export
dtt_minute.Date <- function(x, ...) {
chk_unused(...)
rep(0L, length(x))
}
#' @describeIn dtt_minute Get integer vector of minute values for a POSIXct
#' vector
#' @export
dtt_minute.POSIXct <- function(x, ...) {
chk_unused(...)
x <- as.POSIXlt(x, tz = dtt_tz(x))
as.integer(x$min)
}
#' @describeIn dtt_minute Get integer vector of minute values for a hms vector
#' @export
dtt_minute.hms <- function(x, ...) {
chk_unused(...)
x <- dtt_time(x)
x <- as.POSIXlt(x)
as.integer(x$min)
}
#' @describeIn dtt_minute Set minute values for a POSIXct vector
#' @export
`dtt_minute<-.POSIXct` <- function(x, value) {
chk_whole_numeric(value)
chk_range(value, c(0L, 59L))
chk_subset(length(value), c(1L, length(x)))
if (!length(x)) {
return(x)
}
tz <- dtt_tz(x)
x <- as.POSIXlt(x, tz = tz)
x$min <- value
as.POSIXct(format(x), tz = tz)
}
#' @describeIn dtt_minute Set minute values for a hms vector
#' @export
`dtt_minute<-.hms` <- function(x, value) {
chk_whole_numeric(value)
chk_range(value, c(0L, 59L))
chk_subset(length(value), c(1L, length(x)))
if (!length(x)) {
return(x)
}
x <- as.POSIXlt(x)
x$min <- value
dtt_time(x)
}