问题
I am in the PDT timezone and I want to change the variable "s" to the GMT timezone. Any idea how?
s<-Sys.time()
s
as.POSIXct(s,"GMT")
OUTPUT
> s<-Sys.time()
> s
[1] "2015-06-17 17:56:17 PDT"
> as.POSIXct(s,"GMT")
[1] "2015-06-17 17:56:17 PDT" # <-- how do I get this in GMT??
回答1:
Depending on what you want to do exactly, there are a couple of options:
s <- Sys.time()
s
#[1] "2015-06-18 11:21:22 EST"
Transfer from local time to GMT, without adustment:
as.POSIXct(format(s),tz="GMT")
#[1] "2015-06-18 11:21:22 GMT"
Transfer to GMT, adjusting for the time difference between local time and GMT.
`attr<-`(s,"tzone","GMT")
#[1] "2015-06-18 01:21:22 GMT"
, which is equivalent to the assignment operation:
attr(s,"tzone") <- "GMT"
回答2:
You can also use .POSIXct
:
s <- .POSIXct(s, "GMT")
来源:https://stackoverflow.com/questions/30904851/how-do-you-change-the-timezone-of-sys-time