How do you change the timezone of Sys.time()
Asked Answered
T

3

9

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??
Termor answered 18/6, 2015 at 0:57 Comment(4)
Sys.setenv(TZ="GMT")Parrie
@Parrie This will work but maybe he doesn't want to change his system timezone.Nuncio
Do you want the corresponding time in GMT for the PDT time, or do you want to just essentially change the label from PDT to GMT?Kazantzakis
I don't want to just change the label. I want to change the actual time. So if the time is 17:56:17 PDT i want to add/subtract to get to a differnent timezone.Termor
K
11

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"
Kazantzakis answered 18/6, 2015 at 1:22 Comment(0)
H
10

You can also use .POSIXct:

s <- .POSIXct(s, "GMT")
Heterograft answered 18/6, 2015 at 1:57 Comment(1)
Just noting that this adjusts the time to GMT (i.e. - equivalent to second example in my answer)>Kazantzakis
K
0

You could use clock::date_now(zone = "GMT").

Kugler answered 3/6 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.