2

Is there any efficient way, how to convert from GMT to EST in R?

This is what I tried:

d1 <- as.POSIXlt(as.POSIXct("2001-05-15 12:30:00"), tz="GMT")
[1] "2001-05-15 10:30:00 GMT"
as.POSIXct(as.character(d1),tz="EST")
[1] "2001-05-15 10:30:00 EST"
Nimantha
  • 6,405
  • 6
  • 28
  • 69
mateskabe
  • 289
  • 2
  • 13

2 Answers2

2

Actually there's no "conversion" in a deep sense. POSIXct objects are always stored as the number of seconds since epoch (midnight UTC on Jan 1 1970).

The conversion you're after is "superficial" in the sense that all you're changing is how your object is displayed.

This is controlled by the tzone attribute of a POSIXct object.

So, assuming attr(x, 'tzone') is already GMT for your object x, you can just write:

attr(x, 'tzone') = 'EST'

to change this superficial behavior in your desired way.

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
  • I thought the `usetz` argument in `strptime` might actually use time zone information when `strptime` is used in `as.POSIX.ct` or `format`, but it doesn't. It just means "print time zone in the output" (see `?strptime`). So, I think this attr modification is the best approach, perhaps with `Sys.timezone` or `OlsonNames`. @MichaelChirico, I'm writing this for others not you. I know you either know or have a formed opinion. – geneorama Apr 14 '21 at 14:47
1
d1 <- as.POSIXct("2001-05-15 12:30:00", format='%Y-%m-%d %H:%M:%S', tz="GMT")
format(d1,tz="America/New_York")
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Prem
  • 11,775
  • 1
  • 19
  • 33