001package ch.qos.logback.core.util;
002
003import static org.junit.Assert.assertEquals;
004
005import java.text.ParseException;
006import java.text.SimpleDateFormat;
007import java.util.Date;
008import java.util.TimeZone;
009
010import org.junit.Before;
011import org.junit.Test;
012
013public class CachingFotmatterTest {
014
015    final static String DATE_PATTERN = "yyyy-MM-dd'T'HH:mm";
016
017    SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);
018    TimeZone perthTZ = TimeZone.getTimeZone("Australia/Perth");
019    TimeZone utcTZ = TimeZone.getTimeZone("UTC");
020
021    @Before
022    public void setUp() {
023        sdf.setTimeZone(utcTZ);
024    }
025
026    @Test
027    public void timeZoneIsTakenIntoAccount() throws ParseException {
028
029        CachingDateFormatter cdf = new CachingDateFormatter(DATE_PATTERN);
030        TimeZone perthTZ = TimeZone.getTimeZone("Australia/Perth");
031        cdf.setTimeZone(perthTZ);
032
033        Date march26_2015_0949_UTC = sdf.parse("2015-03-26T09:49");
034        System.out.print(march26_2015_0949_UTC);
035
036        String result = cdf.format(march26_2015_0949_UTC.getTime());
037        // AWST (Perth) is 8 hours ahead of UTC
038        assertEquals("2015-03-26T17:49", result);
039    }
040}