2

由于某种原因,我无法调整时区as.POSIXlt

time <- "Wed Jun 22 01:53:56 +0000 2016"
t <- strptime(time, format = '%a %b %d %H:%M:%S %z %Y')
t
[1] "2016-06-21 21:53:56"

无法更改时区

as.POSIXlt(t, "EST")
[1] "2016-06-21 21:53:56"
as.POSIXlt(t, "Australia/Darwin")
[1] "2016-06-21 21:53:56"

可以更改时区为Sys.time()

as.POSIXlt(Sys.time(), "EST")
[1] "2016-09-26 01:47:22 EST"
as.POSIXlt(Sys.time(), "Australia/Darwin")
[1] "2016-09-26 16:19:48 ACST"

如何解决?

4

2 回答 2

0

尝试这个:

time <- "Wed Jun 22 01:53:56 +0000 2016"
strptime(time, format = '%a %b %d %H:%M:%S %z %Y')
#[1] "2016-06-22 07:23:56"
strptime(time, format = '%a %b %d %H:%M:%S %z %Y', tz="EST")
#[1] "2016-06-21 20:53:56"
strptime(time, format = '%a %b %d %H:%M:%S %z %Y', tz="Australia/Darwin")
#[1] "2016-06-22 11:23:56"
于 2016-09-26T07:07:19.147 回答
0

strptime返回一个POSIXlt对象。调用只是返回as.POSIXlt。没有方法,所以被派遣。您可以看到第一条语句检查是否继承了该类,如果为真则返回。ttas.POSIXlt.POSIXltas.POSIXlt.defaultifxPOSIXltx

str(t)
# POSIXlt[1:1], format: "2016-06-21 20:53:56"
print(as.POSIXlt.default)
# function (x, tz = "", ...) 
# {
#     if (inherits(x, "POSIXlt")) 
#         return(x)
#     if (is.logical(x) && all(is.na(x))) 
#         return(as.POSIXlt(as.POSIXct.default(x), tz = tz))
#     stop(gettextf("do not know how to convert '%s' to class %s", 
#         deparse(substitute(x)), dQuote("POSIXlt")), domain = NA)
# }
# <bytecode: 0x2d6aa18>
# <environment: namespace:base>

您要么需要使用as.POSIXct而不是strptime指定所需的时区,然后转换为POSIXlt

ct <- as.POSIXct(time, tz = "Australia/Darwin", format = "%a %b %d %H:%M:%S %z %Y")
t <- as.POSIXlt(ct)

或使用strptime并转换tPOSIXct然后返回POSIXlt

t <- strptime(time, format = "%a %b %d %H:%M:%S %z %Y")
t <- as.POSIXlt(as.POSIXct(t, tz = "Australia/Darwin"))
于 2016-09-26T13:01:08.170 回答