001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2001, 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.sourcecontrols.accurev;
037:
038: import org.apache.log4j.Logger;
039:
040: import java.text.ParseException;
041: import java.text.SimpleDateFormat;
042: import java.util.Calendar;
043: import java.util.Date;
044: import java.util.GregorianCalendar;
045:
046: /**
047: * Full date timespec, format is either yyyy/MM/dd HH:mm:ss or yyyy/MM/dd HH:mm:ss.count
048: *
049: * @author <a href="mailto:Nicola_Orru@scee.net">Nicola Orru'</a>
050: */
051: public class DateTimespec extends Timespec {
052: private static final Logger LOG = Logger
053: .getLogger(DateTimespec.class);
054: private static final String DATETIME_FORMAT = "yyyy/MM/dd HH:mm:ss";
055: /**
056: * Convenience constant containing the KewordTimespec "now"
057: */
058: public static final KeywordTimespec NOW = new KeywordTimespec("now");
059: private Date date;
060:
061: /**
062: * Creates a new DateTimespec without count (yyyy/MM/dd HH:mm:ss form)
063: *
064: * @param date the timespec date
065: */
066: public DateTimespec(Date date) {
067: this .date = date;
068: }
069:
070: /**
071: * Creates a new DateTimespec without count (yyyy/MM/dd HH:mm:ss form) containing the current time
072: * "shifted" by the given amount of seconds
073: *
074: * @param secondsFromNow distance in seconds from "now" (e.g. 3600 means "one hour from now", -60 means "one
075: * minute ago").
076: */
077: public DateTimespec(int secondsFromNow) {
078: Calendar calendar = new GregorianCalendar();
079: calendar.add(Calendar.SECOND, secondsFromNow);
080: this .date = calendar.getTime();
081: }
082:
083: /**
084: * Returns the formatted date
085: *
086: * @return the formatted date if date is not null or a blank string ig the date is null
087: */
088: public String format() {
089: if (date == null) {
090: return "";
091: }
092: return new SimpleDateFormat(DATETIME_FORMAT).format(date);
093: }
094:
095: /**
096: * Parses a date from Accurev in the format <code>YYYY/MM/DD hh:mm:ss</code>
097: *
098: * @param date
099: * String containing the date to parse
100: * @return a new Date whose value reflects the date string
101: */
102: public static Date parse(String date) {
103: try {
104: return new SimpleDateFormat(DATETIME_FORMAT).parse(date);
105: } catch (ParseException e) {
106: LOG.error("Error parsing date " + date + " using format"
107: + DATETIME_FORMAT);
108: return null;
109: }
110: }
111: }
|