View | Details | Raw Unified | Return to bug 47350
Collapse All | Expand All

(-)tests/src/java/org/apache/log4j/helpers/ISO8601DateFormatTest.java (+50 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 * 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 * 
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
//
19
// Log4j uses the JUnit framework for internal unit testing. JUnit
20
// available from
21
//
22
//     http://www.junit.org
23
24
25
package org.apache.log4j.helpers;
26
27
import java.util.Date;
28
import java.util.TimeZone;
29
30
import junit.framework.TestCase;
31
32
public class ISO8601DateFormatTest extends TestCase {
33
34
    public void testDateFormatingUsingDifferentTimeZone() {
35
        ISO8601DateFormat f1 = new ISO8601DateFormat();
36
        ISO8601DateFormat f2 = new ISO8601DateFormat();
37
        f1.setTimeZone(TimeZone.getTimeZone("GMT"));
38
        f2.setTimeZone(TimeZone.getTimeZone("GMT+1:00"));
39
        Date date = new Date(900000);
40
        
41
        StringBuffer sbuf = new StringBuffer();
42
        f1.format(date, sbuf, null);
43
        assertEquals("1970-01-01 00:15:00,000", sbuf.toString());
44
        
45
        sbuf = new StringBuffer();
46
        f2.format(date, sbuf, null);
47
        assertEquals("1970-01-01 01:15:00,000", sbuf.toString());
48
49
    }
50
}
0
  + Author Date Id Revision
51
  + Author Date Id Revision
1
  + native
52
  + native
(-)tests/src/java/org/apache/log4j/helpers/AbsoluteTimeDateFormatTest.java (+50 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 * 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 * 
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
//
19
// Log4j uses the JUnit framework for internal unit testing. JUnit
20
// available from
21
//
22
//     http://www.junit.org
23
24
25
package org.apache.log4j.helpers;
26
27
import java.util.Date;
28
import java.util.TimeZone;
29
30
import junit.framework.TestCase;
31
32
public class AbsoluteTimeDateFormatTest extends TestCase {
33
34
    public void testDateFormatingUsingDifferentTimeZone() {
35
        AbsoluteTimeDateFormat f1 = new AbsoluteTimeDateFormat();
36
        AbsoluteTimeDateFormat f2 = new AbsoluteTimeDateFormat();
37
        f1.setTimeZone(TimeZone.getTimeZone("GMT"));
38
        f2.setTimeZone(TimeZone.getTimeZone("GMT+1:00"));
39
        Date date = new Date(900000);
40
        
41
        StringBuffer sbuf = new StringBuffer();
42
        f1.format(date, sbuf, null);
43
        assertEquals("00:15:00,000", sbuf.toString());
44
        
45
        sbuf = new StringBuffer();
46
        f2.format(date, sbuf, null);
47
        assertEquals("01:15:00,000", sbuf.toString());
48
49
    }
50
}
0
  + Author Date Id Revision
51
  + Author Date Id Revision
1
  + native
52
  + native
(-)src/main/java/org/apache/log4j/helpers/AbsoluteTimeDateFormat.java (-1 / +3 lines)
Lines 69-74 Link Here
69
69
70
  private static long   previousTime;
70
  private static long   previousTime;
71
  private static char[] previousTimeWithoutMillis = new char[9]; // "HH:mm:ss."
71
  private static char[] previousTimeWithoutMillis = new char[9]; // "HH:mm:ss."
72
  private static TimeZone previousTimeZone;
72
73
73
  /**
74
  /**
74
     Appends to <code>sbuf</code> the time in the format
75
     Appends to <code>sbuf</code> the time in the format
Lines 85-91 Link Here
85
    long now = date.getTime();
86
    long now = date.getTime();
86
    int millis = (int)(now % 1000);
87
    int millis = (int)(now % 1000);
87
88
88
    if ((now - millis) != previousTime) {
89
    if ((now - millis) != previousTime || !getTimeZone().equals(previousTimeZone)) {
89
      // We reach this point at most once per second
90
      // We reach this point at most once per second
90
      // across all threads instead of each time format()
91
      // across all threads instead of each time format()
91
      // is called. This saves considerable CPU time.
92
      // is called. This saves considerable CPU time.
Lines 119-124 Link Here
119
      sbuf.getChars(start, sbuf.length(), previousTimeWithoutMillis, 0);
120
      sbuf.getChars(start, sbuf.length(), previousTimeWithoutMillis, 0);
120
      
121
      
121
      previousTime = now - millis;
122
      previousTime = now - millis;
123
      previousTimeZone = getTimeZone();
122
    }
124
    }
123
    else {
125
    else {
124
      sbuf.append(previousTimeWithoutMillis);
126
      sbuf.append(previousTimeWithoutMillis);
(-)src/main/java/org/apache/log4j/helpers/ISO8601DateFormat.java (-1 / +3 lines)
Lines 53-58 Link Here
53
53
54
  static private long   lastTime;
54
  static private long   lastTime;
55
  static private char[] lastTimeString = new char[20];
55
  static private char[] lastTimeString = new char[20];
56
  static private TimeZone lastTimeZone;
56
57
57
  /**
58
  /**
58
     Appends a date in the format "YYYY-mm-dd HH:mm:ss,SSS"
59
     Appends a date in the format "YYYY-mm-dd HH:mm:ss,SSS"
Lines 67-73 Link Here
67
    long now = date.getTime();
68
    long now = date.getTime();
68
    int millis = (int)(now % 1000);
69
    int millis = (int)(now % 1000);
69
70
70
    if ((now - millis) != lastTime) {
71
    if ((now - millis) != lastTime || !getTimeZone().equals(lastTimeZone)) {
71
      // We reach this point at most once per second
72
      // We reach this point at most once per second
72
      // across all threads instead of each time format()
73
      // across all threads instead of each time format()
73
      // is called. This saves considerable CPU time.
74
      // is called. This saves considerable CPU time.
Lines 129-134 Link Here
129
      // store the time string for next time to avoid recomputation
130
      // store the time string for next time to avoid recomputation
130
      sbuf.getChars(start, sbuf.length(), lastTimeString, 0);
131
      sbuf.getChars(start, sbuf.length(), lastTimeString, 0);
131
      lastTime = now - millis;
132
      lastTime = now - millis;
133
      lastTimeZone = getTimeZone();
132
    }
134
    }
133
    else {
135
    else {
134
      sbuf.append(lastTimeString);
136
      sbuf.append(lastTimeString);

Return to bug 47350