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.Calendar;
021import java.util.TimeZone;
022import java.util.Date;
023import java.text.FieldPosition;
024import java.text.ParsePosition;
025
026// Contributors: Arndt Schoenewald <arndt@ibm23093i821.mc.schoenewald.de>
027
028/**
029   Formats a {@link Date} in the format "yyyy-MM-dd HH:mm:ss,SSS" for example
030   "1999-11-27 15:49:37,459".
031
032   <p>Refer to the <a
033   href=http://www.cl.cam.ac.uk/~mgk25/iso-time.html>summary of the
034   International Standard Date and Time Notation</a> for more
035   information on this format.
036
037   @author Ceki G&uuml;lc&uuml;
038   @author Andrew Vajoczki
039
040   @since 0.7.5
041*/
042public class ISO8601DateFormat extends AbsoluteTimeDateFormat {
043  private static final long serialVersionUID = -759840745298755296L;
044
045  public
046  ISO8601DateFormat() {
047  }
048
049  public
050  ISO8601DateFormat(TimeZone timeZone) {
051    super(timeZone);
052  }
053
054  static private long   lastTime;
055  static private char[] lastTimeString = new char[20];
056
057  /**
058     Appends a date in the format "YYYY-mm-dd HH:mm:ss,SSS"
059     to <code>sbuf</code>. For example: "1999-11-27 15:49:37,459".
060
061     @param sbuf the <code>StringBuffer</code> to write to
062  */
063  public
064  StringBuffer format(Date date, StringBuffer sbuf,
065                      FieldPosition fieldPosition) {
066
067    long now = date.getTime();
068    int millis = (int)(now % 1000);
069
070    if ((now - millis) != lastTime || lastTimeString[0] == 0) {
071      // We reach this point at most once per second
072      // across all threads instead of each time format()
073      // is called. This saves considerable CPU time.
074
075      calendar.setTime(date);
076
077      int start = sbuf.length();
078
079      int year =  calendar.get(Calendar.YEAR);
080      sbuf.append(year);
081
082      String month;
083      switch(calendar.get(Calendar.MONTH)) {
084      case Calendar.JANUARY: month = "-01-"; break;
085      case Calendar.FEBRUARY: month = "-02-";  break;
086      case Calendar.MARCH: month = "-03-"; break;
087      case Calendar.APRIL: month = "-04-";  break;
088      case Calendar.MAY: month = "-05-"; break;
089      case Calendar.JUNE: month = "-06-";  break;
090      case Calendar.JULY: month = "-07-"; break;
091      case Calendar.AUGUST: month = "-08-";  break;
092      case Calendar.SEPTEMBER: month = "-09-"; break;
093      case Calendar.OCTOBER: month = "-10-"; break;
094      case Calendar.NOVEMBER: month = "-11-";  break;
095      case Calendar.DECEMBER: month = "-12-";  break;
096      default: month = "-NA-"; break;
097      }
098      sbuf.append(month);
099
100      int day = calendar.get(Calendar.DAY_OF_MONTH);
101      if(day < 10)
102        sbuf.append('0');
103      sbuf.append(day);
104
105      sbuf.append(' ');
106
107      int hour = calendar.get(Calendar.HOUR_OF_DAY);
108      if(hour < 10) {
109        sbuf.append('0');
110      }
111      sbuf.append(hour);
112      sbuf.append(':');
113
114      int mins = calendar.get(Calendar.MINUTE);
115      if(mins < 10) {
116        sbuf.append('0');
117      }
118      sbuf.append(mins);
119      sbuf.append(':');
120
121      int secs = calendar.get(Calendar.SECOND);
122      if(secs < 10) {
123        sbuf.append('0');
124      }
125      sbuf.append(secs);
126
127      sbuf.append(',');
128
129      // store the time string for next time to avoid recomputation
130      sbuf.getChars(start, sbuf.length(), lastTimeString, 0);
131      lastTime = now - millis;
132    }
133    else {
134      sbuf.append(lastTimeString);
135    }
136
137
138    if (millis < 100)
139      sbuf.append('0');
140    if (millis < 10)
141      sbuf.append('0');
142
143    sbuf.append(millis);
144    return sbuf;
145  }
146
147  /**
148    This method does not do anything but return <code>null</code>.
149   */
150  public
151  Date parse(java.lang.String s, ParsePosition pos) {
152    return null;
153  }
154}
155