Search code examples
rposixctchron

Convert chron to POSIXct in GMT format


Normally, I use chron to represent date/time objects. However, I need to use some functions that work with the POSIX format, so I am trying to go from chron to POSIXct. Using as.POSIXct() seems to work but the result is in localtime instead of GMT (the original data is in GMT).

x <- chron(dates="05/12/15", times="12:30:45")
as.POSIXct(x, tz="GMT")
"2015-05-12 13:30:45 BST"

what I want is:

"2015-05-12 12:30:45 GMT"

but I can't find a way to obtain it.

strptime() won't work because the original input is not a string, but a chron object. Of course I could go from a chron object to a character string and then to POSIXct but it seems a bit convoluted way to do it.

I suppose I could force my R session to use GMT with Sys.timezone(), but I'd prefer not to. Any other suggestion? Thank you.


Solution

  • Just try:

    x <- chron(dates="05/12/15", times="12:30:45")
    y<-as.POSIXct(x)
    attr(y,"tzone")<-"GMT"
    y
    #[1] "2015-05-12 12:30:45 GMT"