考虑这个简单的例子
DT <- data.table::data.table(mytime = c(nanotime('2011-12-05 08:30:00.000',format ="%Y-%m-%d %H:%M:%E9S", tz ="GMT"),
nanotime('2011-12-05 08:30:00.100',format ="%Y-%m-%d %H:%M:%E9S", tz ="GMT"),
nanotime('2011-12-05 08:30:00.825',format ="%Y-%m-%d %H:%M:%E9S", tz ="GMT")))
#convert the timestamp to string
DT[, mytime_character := strftime(mytime, format = '%Y-%m-%d %H:%M:%OS3')]
> DT
mytime mytime_character
1: 2011-12-05T08:30:00.000000000+00:00 2011-12-05 03:30:00.000
2: 2011-12-05T08:30:00.100000000+00:00 2011-12-05 03:30:00.099
3: 2011-12-05T08:30:00.825000000+00:00 2011-12-05 03:30:00.825
如您所见,将nanotime
时间戳转换为character
(使用strftime
)会为第二个时间戳创建错误的毫秒部分:.099
而不是.100
不幸的是,我需要将时间戳转换为字符串,这样它们才能被其他编程语言摄取。
有没有一种方法可以将时间戳正确地转换为字符而不损失精度?
谢谢!
https://stackoverflow.com/questions/56244125
复制相似问题