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 package org.apache.log4j.helpers;
19
20 import java.util.Date;
21 import java.util.Calendar;
22 import java.util.TimeZone;
23 import java.text.FieldPosition;
24 import java.text.ParsePosition;
25 import java.text.DateFormat;
26
27
28 /**
29 Formats a {@link Date} in the format "HH:mm:ss,SSS" for example,
30 "15:49:37,459".
31
32 @author Ceki Gülcü
33 @author Andrew Vajoczki
34
35 @since 0.7.5
36 */
37 public class AbsoluteTimeDateFormat extends DateFormat {
38 private static final long serialVersionUID = -388856345976723342L;
39
40 /**
41 String constant used to specify {@link
42 org.apache.log4j.helpers.AbsoluteTimeDateFormat} in layouts. Current
43 value is <b>ABSOLUTE</b>. */
44 public final static String ABS_TIME_DATE_FORMAT = "ABSOLUTE";
45
46 /**
47 String constant used to specify {@link
48 org.apache.log4j.helpers.DateTimeDateFormat} in layouts. Current
49 value is <b>DATE</b>.
50 */
51 public final static String DATE_AND_TIME_DATE_FORMAT = "DATE";
52
53 /**
54 String constant used to specify {@link
55 org.apache.log4j.helpers.ISO8601DateFormat} in layouts. Current
56 value is <b>ISO8601</b>.
57 */
58 public final static String ISO8601_DATE_FORMAT = "ISO8601";
59
60 public
61 AbsoluteTimeDateFormat() {
62 setCalendar(Calendar.getInstance());
63 }
64
65 public
66 AbsoluteTimeDateFormat(TimeZone timeZone) {
67 setCalendar(Calendar.getInstance(timeZone));
68 }
69
70 private static long previousTime;
71 private static char[] previousTimeWithoutMillis = new char[9]; // "HH:mm:ss."
72
73 /**
74 Appends to <code>sbuf</code> the time in the format
75 "HH:mm:ss,SSS" for example, "15:49:37,459"
76
77 @param date the date to format
78 @param sbuf the string buffer to write to
79 @param fieldPosition remains untouched
80 */
81 public
82 StringBuffer format(Date date, StringBuffer sbuf,
83 FieldPosition fieldPosition) {
84
85 long now = date.getTime();
86 int millis = (int)(now % 1000);
87
88 if ((now - millis) != previousTime || previousTimeWithoutMillis[0] == 0) {
89 // We reach this point at most once per second
90 // across all threads instead of each time format()
91 // is called. This saves considerable CPU time.
92
93 calendar.setTime(date);
94
95 int start = sbuf.length();
96
97 int hour = calendar.get(Calendar.HOUR_OF_DAY);
98 if(hour < 10) {
99 sbuf.append('0');
100 }
101 sbuf.append(hour);
102 sbuf.append(':');
103
104 int mins = calendar.get(Calendar.MINUTE);
105 if(mins < 10) {
106 sbuf.append('0');
107 }
108 sbuf.append(mins);
109 sbuf.append(':');
110
111 int secs = calendar.get(Calendar.SECOND);
112 if(secs < 10) {
113 sbuf.append('0');
114 }
115 sbuf.append(secs);
116 sbuf.append(',');
117
118 // store the time string for next time to avoid recomputation
119 sbuf.getChars(start, sbuf.length(), previousTimeWithoutMillis, 0);
120
121 previousTime = now - millis;
122 }
123 else {
124 sbuf.append(previousTimeWithoutMillis);
125 }
126
127
128
129 if(millis < 100)
130 sbuf.append('0');
131 if(millis < 10)
132 sbuf.append('0');
133
134 sbuf.append(millis);
135 return sbuf;
136 }
137
138 /**
139 This method does not do anything but return <code>null</code>.
140 */
141 public
142 Date parse(String s, ParsePosition pos) {
143 return null;
144 }
145 }
146