001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2007, ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package net.sourceforge.cruisecontrol.dashboard.utils;
037:
038: import java.text.SimpleDateFormat;
039: import java.util.Locale;
040:
041: import net.sourceforge.cruisecontrol.util.DateUtil;
042:
043: import org.joda.time.DateTime;
044: import org.joda.time.DateTimeZone;
045: import org.joda.time.format.DateTimeFormat;
046: import org.joda.time.format.DateTimeFormatter;
047:
048: public final class CCDateFormatter {
049:
050: private static final int DATE_START = 3;
051:
052: private static final int DATE_END = 17;
053:
054: private static final int MILLONS_OF_DAY = 1000 * 3600 * 24;
055:
056: private static DateTimeFormatter yyyyMMddHHmmssPattern = DateTimeFormat
057: .forPattern("yyyyMMddHHmmss");
058:
059: private static DateTimeFormatter iso8601Pattern = DateTimeFormat
060: .forPattern("yyyy-MM-dd'T'HH:mm:ss");
061:
062: private static SimpleDateFormat yyyyMMddHHmmssSimpleDateFormat = new SimpleDateFormat(
063: "yyyyMMddHHmmss", Locale.ENGLISH);
064:
065: private static SimpleDateFormat iso8601SimpleDateFormat = new SimpleDateFormat(
066: "yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH);
067:
068: private CCDateFormatter() {
069: }
070:
071: public static String duration(long timeSpan) {
072: String when = timeSpan > 0 ? "ago" : "later";
073: long days = timeSpan / CCDateFormatter.MILLONS_OF_DAY;
074: String daysString = days == 0 ? "" : days + " days ";
075: String remaining = DateUtil.formatTime(timeSpan - days
076: * CCDateFormatter.MILLONS_OF_DAY);
077: String space = remaining.endsWith(" ") ? "" : " ";
078: return daysString + remaining + space + when;
079: }
080:
081: public static String getDateStringInHumanBeingReadingStyle(
082: DateTime date) {
083: String dateString = getDateFormatterWithTimeZone().format(
084: date.toDate());
085: int colonPlace = dateString.length() - 2;
086: return dateString.substring(0, colonPlace) + ":"
087: + dateString.substring(colonPlace);
088: }
089:
090: private static SimpleDateFormat getDateFormatterWithTimeZone() {
091: return new SimpleDateFormat("d MMM yyyy HH:mm 'GMT' Z",
092: Locale.ENGLISH);
093: }
094:
095: public static String yyyyMMddHHmmss(DateTime date) {
096: return yyyyMMddHHmmssSimpleDateFormat.format(date.toDate());
097: }
098:
099: public static DateTime format(String datetime, String dateFormat) {
100: return DateTimeFormat.forPattern(dateFormat).parseDateTime(
101: datetime);
102: }
103:
104: public static String format(DateTime datetime, String format) {
105: return new SimpleDateFormat(format, Locale.ENGLISH)
106: .format(datetime.toDate());
107: }
108:
109: public static DateTime formatLogName(String logFileName) {
110: return yyyyMMddHHmmssPattern
111: .parseDateTime(getBuildDateFromLogFileName(logFileName));
112: }
113:
114: public static String getBuildDateFromLogFileName(String logFileName) {
115: return logFileName.substring(DATE_START, DATE_END);
116: }
117:
118: public static DateTime iso8601(String datetime) {
119: try {
120: return new DateTime(DateUtil.parseIso8601(datetime)
121: .getTime(), DateTimeZone.forOffsetHours(0));
122: } catch (Exception e) {
123: throw new RuntimeException(e);
124: }
125: }
126: }
|