How would you convert the timezone of a time object which class is POSIXct?
Make a time object of class “POSIXct”, using a function called Sys.time. It returns a local current local time.
timePDT <- Sys.time() print(paste("The current local time in PDT: ", timePDT)) # "The current local time in PDT: 2014-05-30 16:52:08"
Convert this time object’s timezone , using a function, called strftime. This function, however, converts the object’s class from “POSIXct” to “character”. Therefore, you need to use a function, as.POSIXct, to convert the class back to “POSIXct”.
timeGMT_char <- strftime(x=time, format="%Y-%m-%d %H:%M:%S", tz="gmt") timeGMT <- as.POSIXct(x=timeGMT_char, format="%Y-%m-%d %H:%M:%S", tz="gmt") print(paste0("The current local time in GMT: ", timeGMT)) # "The current local time in GMT: 2014-05-30 23:33:21"
A discussion related to this topic of time-conversion in R can be found at http://blog.revolutionanalytics.com/2009/06/converting-time-zones.html