I'd like to tell r that my data is in the UTC timezone so that I can then shift them to America/New_York
. But when I use indexTZ()
it changes the times.
I want the 16:00 UTC hour to become a 12:00 NY hour.
test = read.zoo(paste0(datadir,"test_.csv"),
index = 1,FUN = as.POSIXct, header = T, sep = ",")
test = as.xts(test)
head(test)
> QQQ.Open QQQ.High QQQ.Low QQQ.Close QQQ.Volume
>
> 2016-09-10 16:38:00 4665.75 4665.75 4665.75 4665.75 1
> 2016-09-11 14:13:00 4665.75 4665.75 4665.75 4665.75 1
> 2016-09-11 22:01:00 4661.25 4667.25 4657.25 4666.75 932
> 2016-09-11 22:02:00 4666.75 4667.25 4663.25 4665.00 174
> 2016-09-11 22:03:00 4665.00 4667.00 4665.00 4666.50 66
indexTZ(test)<- "UTC"
head(test)
QQQ.Open QQQ.High QQQ.Low QQQ.Close QQQ.Volume
2016-09-10 20:38:00 4665.75 4665.75 4665.75 4665.75 1
2016-09-11 18:13:00 4665.75 4665.75 4665.75 4665.75 1
2016-09-12 02:01:00 4661.25 4667.25 4657.25 4666.75 932
2016-09-12 02:02:00 4666.75 4667.25 4663.25 4665.00 174
2016-09-12 02:03:00 4665.00 4667.00 4665.00 4666.50 66
Warning message:
timezone of object (UTC) is different than current timezone ().
> test_dt$hour1 = strftime(test_dt$index, format = "%H", tz = "America/New_York")
> test_dt$hour2 = strftime(test_dt$index, format = "%H", tz = "UTC")
> table(test_dt$hour1)
14 16 22
1 1 3
> table(test_dt$hour2)
02 18 20
3 1 1
Shifting timezones is a little tricky. You need to first step back and realized that the actual stored time is a number (of seconds since the epoch January 1, 1970) representation absolute time. Ie see how I represent same time point ("epoch") as a local time in NY and Moscow:
Now, the time you have stored were probably parsed as localtime. That is they contain an offset store as the timezone. By changing that you just move relative to the previous time:
So I understand your question correctly, you need to do two things: 'undo' the local time you have and then move to the desired timezone.
I wrote a helper for this too -- in the RcppCCTZ package. Here is one example for the
toTz()
function:This also shows the added difficulty of ensuring that you print to the desired timezone -- only the second example shows the correct Sydney time as we explicitly told
format()
to use it.So to get back to your example:
So I explicitly changed the numeric value by 60 minutes to account for the fact that I shifted time by an hour (from Chicago, my local time, to New York).