Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

timezone

r

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??
like image 496
user3022875 Avatar asked Jun 18 '15 00:06

user3022875


2 Answers

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"
like image 119
thelatemail Avatar answered Oct 18 '22 08:10

thelatemail


You can also use .POSIXct:

s <- .POSIXct(s, "GMT")
like image 20
Carlos Cinelli Avatar answered Oct 18 '22 08:10

Carlos Cinelli