R Convert Timezone

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))

#[1] "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))

#[1] "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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s