在 R 中使用 strptime 转换时区

Use strptime to convert time zone in R

我的 'date/time' 字段的示例是:

2020-12-30T03:32:30.000Z

我希望将此时区转换为“tz = 新加坡”。 当我执行这段代码时,

geodf$time <- strptime(geodf$time, format = "%Y-%m-%dT%H:%M:%OS", tz="Singapore")

结果输出是:

"2020-12-30 03:32:30 +08"

唯一的变化是后面的附加标签 (+08)。时间完全没有改变 - 我需要将时间更改为新加坡的时间,而不仅仅是添加 +08。

出了什么问题?

你的时间里的Z表示时区是UTC。一种方法是使用 class POSIXct 的对象并更改时区:

geodf$time <- as.POSIXct(geodf$time,
                         format = "%Y-%m-%dT%H:%M:%OS", tz="UTC")
geodf
#                 time
#1 2020-12-30 03:32:30

attributes(geodf$time)$tzone <- "Singapore"
geodf
#                 time
#1 2020-12-30 11:32:30

示例数据:

geodf <- structure(list(time = "2020-12-30T03:32:30.000Z"),
                   class = "data.frame", row.names = c(NA, -1L))