Skip to content

Instantly share code, notes, and snippets.

@dsdstudio
Created May 17, 2012 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsdstudio/2717276 to your computer and use it in GitHub Desktop.
Save dsdstudio/2717276 to your computer and use it in GitHub Desktop.
TimeZoneTest
package test;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
import org.junit.Test;
public class TimeZoneTest {
private TimeZone getSummertimeTimeZone() {
// PST/PDT 기준으로 만든 Timezone입니다. 4월 첫째주 일요일, 10월 마지막주 일요일이 TZ가 바뀌는 날입니다.
SimpleTimeZone zone = new SimpleTimeZone(-28800 * 1000, "WLTZ");
zone.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 7200 * 1000);
zone.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 7200 * 1000);
return zone;
}
@Test
public void timezoneTest() {
/**
* 실제 PST/PDT 바뀌는날짜의 timestamp를 추출,
* 엔진에서는 실제 이값이 analysis_day.list에 있을것이라 가정
*/
long [] timestamps = {
1333180800 * 1000L, // 2012.03.31 00:00 (GMT-08:00)
1333267200 * 1000L, // 2012.04.01 00:00 (GMT-08:00)
1333350000 * 1000L // 2012.04.02 00:00 (GMT-07:00)
};
/**
* 설정파일에서 설정해준 Timezone이라 가정
*/
TimeZone zone = this.getSummertimeTimeZone();
/**
* 이것을 default로 설정한다.
*/
TimeZone.setDefault(zone);
/**
* timestamp파일을 읽어다가 파싱한다고 가정
*/
for ( long timestamp : timestamps ){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timestamp);
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").format(cal.getTime()) + " offset : " + ( zone.getOffset(timestamp) / (60*60*1000)) );
}
/**
* 같은 시간이 2번나오는 날은 timezone에서 GMTOffset 을 이전시간과 비교해보면 알수있다.
* 현재 시간의 Timezoneoffset을 얻어낼때는 TimeZone.getoffset(long date)로 알아낼수있다.
* 2012.10.28 01:59:59 (GMT-07:00) -> 2012.10.28 01:00:00 (GMT-08:00)
*/
/**
* 위소스 출력결과는 아래와 같다.
* 2012-03-31 00:00:00 GMT-08:00 offset : -8
* 2012-04-01 00:00:00 GMT-08:00 offset : -8
* 2012-04-02 00:00:00 GMT-07:00 offset : -7
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment