舍入到下一毫秒的时间
Rounding time to the next milisecond
考虑日期:
options(digits.secs = 6)
library(lubridate)
this_special_date <- with_tz(as.POSIXct(strptime('2017-11-20 23:00:00.051438000',
'%Y-%m-%d %H:%M:%OS', tz = 'GMT')),
tzone='Asia/Tokyo')
现在,我可以使用 this answer 中的函数 myformat.POSIXct
将其四舍五入到最接近的毫秒数:
myformat.POSIXct <- function(x, digits=0) {
x2 <- round(unclass(x), digits)
attributes(x2) <- attributes(x)
x <- as.POSIXlt(x2)
x$sec <- round(x$sec, digits) + 10^(-digits-1)
format.POSIXlt(x, paste("%Y-%m-%d %H:%M:%OS",digits,sep=""))
}
myformat.POSIXct(this_special_date, digits=3)
return:“2017-11-21 08:00:00.051”。但是如何舍入 POSIXct 时间
到 下一个 毫秒? - IE。在上述情况下,它将是
“2017-11-21 08:00:00.052”.
你能把所有值都加上 00.0005 吗?这将确保它们始终相对于起始值向上舍入。
myformat.POSIXct <- function(x, digits=0) {
x2 <- round(unclass(x + (5*10^(-digits-1))), digits)
attributes(x2) <- attributes(x)
x <- as.POSIXlt(x2)
x$sec <- round(x$sec, digits) + 10^(-digits-1)
format.POSIXlt(x, paste("%Y-%m-%d %H:%M:%OS",digits,sep=""))
}
myformat.POSIXct(this_special_date, digits=3)
myceil.POSIXct <- function(x, digits=0) {
x1 <- as.numeric(x)
x1 <- ceiling(x1 * 10 ^ digits) / 10 ^ digits
attributes(x1) <- attributes(x)
x1
}
myceil.POSIXct(this_special_date, 3)
#[1] "2017-11-21 08:00:00.052 JST"
考虑日期:
options(digits.secs = 6)
library(lubridate)
this_special_date <- with_tz(as.POSIXct(strptime('2017-11-20 23:00:00.051438000',
'%Y-%m-%d %H:%M:%OS', tz = 'GMT')),
tzone='Asia/Tokyo')
现在,我可以使用 this answer 中的函数 myformat.POSIXct
将其四舍五入到最接近的毫秒数:
myformat.POSIXct <- function(x, digits=0) {
x2 <- round(unclass(x), digits)
attributes(x2) <- attributes(x)
x <- as.POSIXlt(x2)
x$sec <- round(x$sec, digits) + 10^(-digits-1)
format.POSIXlt(x, paste("%Y-%m-%d %H:%M:%OS",digits,sep=""))
}
myformat.POSIXct(this_special_date, digits=3)
return:“2017-11-21 08:00:00.051”。但是如何舍入 POSIXct 时间 到 下一个 毫秒? - IE。在上述情况下,它将是 “2017-11-21 08:00:00.052”.
你能把所有值都加上 00.0005 吗?这将确保它们始终相对于起始值向上舍入。
myformat.POSIXct <- function(x, digits=0) {
x2 <- round(unclass(x + (5*10^(-digits-1))), digits)
attributes(x2) <- attributes(x)
x <- as.POSIXlt(x2)
x$sec <- round(x$sec, digits) + 10^(-digits-1)
format.POSIXlt(x, paste("%Y-%m-%d %H:%M:%OS",digits,sep=""))
}
myformat.POSIXct(this_special_date, digits=3)
myceil.POSIXct <- function(x, digits=0) {
x1 <- as.numeric(x)
x1 <- ceiling(x1 * 10 ^ digits) / 10 ^ digits
attributes(x1) <- attributes(x)
x1
}
myceil.POSIXct(this_special_date, 3)
#[1] "2017-11-21 08:00:00.052 JST"