001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.log4j.helpers;
019
020import java.util.Date;
021import java.util.Calendar;
022import java.util.TimeZone;
023import java.text.FieldPosition;
024import java.text.ParsePosition;
025import java.text.DateFormat;
026
027
028/**
029   Formats a {@link Date} in the format "HH:mm:ss,SSS" for example,
030   "15:49:37,459".
031   
032   @author Ceki Gülcü
033   @author Andrew Vajoczki    
034
035   @since 0.7.5
036*/
037public class AbsoluteTimeDateFormat extends DateFormat {
038   private static final long serialVersionUID = -388856345976723342L;
039
040  /**
041     String constant used to specify {@link
042     org.apache.log4j.helpers.AbsoluteTimeDateFormat} in layouts. Current
043     value is <b>ABSOLUTE</b>.  */
044  public final static String ABS_TIME_DATE_FORMAT = "ABSOLUTE";
045
046  /**
047     String constant used to specify {@link
048     org.apache.log4j.helpers.DateTimeDateFormat} in layouts.  Current
049     value is <b>DATE</b>.
050  */
051  public final static String DATE_AND_TIME_DATE_FORMAT = "DATE";
052
053  /**
054     String constant used to specify {@link
055     org.apache.log4j.helpers.ISO8601DateFormat} in layouts. Current
056     value is <b>ISO8601</b>.
057  */
058  public final static String ISO8601_DATE_FORMAT = "ISO8601";
059
060  public
061  AbsoluteTimeDateFormat() {
062    setCalendar(Calendar.getInstance());
063  }
064  
065  public
066  AbsoluteTimeDateFormat(TimeZone timeZone) {
067    setCalendar(Calendar.getInstance(timeZone));
068  }
069
070  private static long   previousTime;
071  private static char[] previousTimeWithoutMillis = new char[9]; // "HH:mm:ss."
072
073  /**
074     Appends to <code>sbuf</code> the time in the format
075     "HH:mm:ss,SSS" for example, "15:49:37,459"
076
077     @param date the date to format
078     @param sbuf the string buffer to write to
079     @param fieldPosition remains untouched
080    */
081  public
082  StringBuffer format(Date date, StringBuffer sbuf,
083                      FieldPosition fieldPosition) {
084
085    long now = date.getTime();
086    int millis = (int)(now % 1000);
087
088    if ((now - millis) != previousTime || previousTimeWithoutMillis[0] == 0) {
089      // We reach this point at most once per second
090      // across all threads instead of each time format()
091      // is called. This saves considerable CPU time.
092
093      calendar.setTime(date);
094
095      int start = sbuf.length();
096      
097      int hour = calendar.get(Calendar.HOUR_OF_DAY);
098      if(hour < 10) {
099        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}