2016-12-16 4 views
0

데이터에 UTC 시간대가 있음을 알려 주시면 America/New_York으로 변경할 수 있습니다. 하지만 indexTZ()을 사용하면 시간이 변경됩니다.xts에서 시간대를 변경하는 방법

16:00 UTC 시간이 12:00 NY 시간이되기를 원합니다.

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 

답변

2

이동 시간대는 약간 까다 롭습니다. 처음으로 뒤로 물러나서 실제 저장된 시간은 절대 시간을 나타내는 1970 년 1 월 1 일 이후의 시간 (초)입니다.

R> format(as.POSIXct(0,origin="1970-01-01"), tz="UTC") 
[1] "1970-01-01" 
R> format(as.POSIXct(0,origin="1970-01-01"), tz="America/New_York") 
[1] "1969-12-31 19:00:00" 
R> format(as.POSIXct(0,origin="1970-01-01"), tz="Europe/Moscow") 
[1] "1970-01-01 03:00:00" 
R> 

지금, 당신이 저장 한 시간이 아마의 현지로 분석되었다 : 즉 내가 뉴욕과 모스크바에서 현지 시간으로 같은 시점 ("신기원")를 표현하는 방법을 참조하십시오. 즉, 시간대로 오프셋 저장소가 포함됩니다. 당신은 단지 이전 시간을 기준으로 이동하는 것이 변경하여 :
R> as.POSIXct("2016-09-10 16:38:00") # CDT as I am in Chicago 
[1] "2016-09-10 16:38:00 CDT" 
R> format(as.POSIXct("2016-09-10 16:38:00"), tz="America/New_York") 
[1] "2016-09-10 17:38:00" 
R> format(as.POSIXct("2016-09-10 16:38:00"), tz="America/Los_Angeles") 
[1] "2016-09-10 14:38:00" 
R> 

그래서 내가 제대로 질문을 이해, 당신은 두 가지 작업을 수행해야합니다 당신은 현지 시간을 '취소'다음 원하는 시간대로 이동합니다.

도우미는 RcppCCTZ 패키지에서 작성했습니다. 여기에 toTz() 기능을위한 하나의 예입니다

R> example(toTz) 

toTzR> toTz(Sys.time(), "America/New_York", "Europe/London") 
[1] "2016-12-17 01:04:14.184086 CST" 

toTzR> # this redoes the 'Armstrong on the moon in NYC and Sydney' example 
toTzR> # note that the default print method will print the return object in _your local time_ 
toTzR> toTz(ISOdatetime(1969,7,20,22,56,0,tz="UTC"), "America/New_York", "Australia/Sydney", verbose=TRUE) 
1969-07-20 22:56:00 -0400 
1969-07-21 12:56:00 +1000 
[1] "1969-07-20 21:56:00 CDT" 

toTzR> # whereas explicitly formating for Sydney time does the right thing 
toTzR> format(toTz(ISOdatetime(1969,7,20,22,56,0,tz="UTC"), 
toTz+    "America/New_York", "Australia/Sydney", verbose=TRUE), 
toTz+  tz="Australia/Sydney") 
1969-07-20 22:56:00 -0400 
1969-07-21 12:56:00 +1000 
[1] "1969-07-21 12:56:00" 

이것은 또한 당신이 원하는 시간대에 인쇄하는 것이 보장의 추가 어려움을 보여줍니다 - 오직 두 번째 예는 우리가 명시 적으로 사용하는 format()에게로 올바른 시드니 시간을 보여줍니다 .

그래서 다시 예에 도착 : 나는 시간 시간을 이동하는 것이 그래서 명시 적으로 (사실을 설명 60 분 시카고에서, 내 로컬 시간을 숫자 값을 변경

R> x <- xts(1:2, Sys.time() + 0:1) 
R> x 
          [,1] 
2016-12-16 20:13:43.29767 1 
2016-12-16 20:13:44.29767 2 
R> tzone(x) <- "America/New_York" 
R> x 
          [,1] 
2016-12-16 21:13:43.29767 1 
2016-12-16 21:13:44.29767 2 
Warning message: 
timezone of object (America/New_York) is different than current timezone(). 
R> index(x) <- index(x) - 60*60 # dirty method, last resort 
R> x 
          [,1] 
2016-12-16 20:13:43.29767 1 
2016-12-16 20:13:44.29767 2 
Warning message: 
timezone of object (America/New_York) is different than current timezone(). 
R> tzone(x) 
       TZ 
"America/New_York" 
R> 

, 뉴욕에).

2

아마도 "미국/뉴욕 _ 요크"시간으로 데이터가로드되었을 것입니다. 아마 시간대를 "UTC"로 지정해야했을 것입니다. (당신은 FUN=POSIXct에 대한 통과 매개 변수로 시간대 인수에 전달할 수 있습니다.)

재현 원래 상황 :

data <- " 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" 

data = read.table(text = data, 
        col.names = c("date", "time", "Open" , "High", "Low", "Close", "Volume") 
       ) 
# assumes data is loaded in America/New_York time zone 
x_data <- xts(order.by = as.POSIXct(paste(data$date, data$time), tz = "America/New_York"), data[3:NCOL(data)]) 


x_data 
# Open High  Low Close 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(x_data) <- "UTC" 

# This reproduces your situation (problem): 
head(x_data) 
# Open High  Low Close 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 


# This is what you probably wanted to do. Set the initial timezone to "UTC" when you loaded the data into R and created your `POSIXct` objects. 

x_data <- xts(order.by = as.POSIXct(paste(data$date, data$time), tz = "UTC"), data[3:NCOL(data)]) 
head(x_data) 
# Open High  Low Close 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(x_data) <- "America/New_York" 
# Now you get your desired outcome: 
head(x_data) 
# Open High  Low Close Volume 
# 2016-09-10 12:38:00 4665.75 4665.75 4665.75 4665.75  1 
# 2016-09-11 10:13:00 4665.75 4665.75 4665.75 4665.75  1 
# 2016-09-11 18:01:00 4661.25 4667.25 4657.25 4666.75 932 
# 2016-09-11 18:02:00 4666.75 4667.25 4663.25 4665.00 174 
# 2016-09-11 18:03:00 4665.00 4667.00 4665.00 4666.50  66 
0
아하는, 예, 대답은 XTS을 만드는 것입니다

올바른 객체를 시간대 :

예 :

S = as.xts(test, tz = "UTC")

관련 문제