考虑日期:
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')
现在,我可以使用此答案中的函数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 时间舍入到下一个毫秒?--即在上述情况下,它将是“2017-11-21 08:00:00.052”。