【发布时间】:2018-02-22 12:56:05
【问题描述】:
考虑日期:
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)
返回:“2017-11-21 08:00:00.051”。但是如何舍入一个 POSIXct 时间 下一个毫秒? - IE。在上述情况下,它将是 “2017-11-21 08:00:00.052”。
【问题讨论】: