--- projects/cms/source/util/uk/org/iscream/cms/util/DateUtils.java 2001/02/03 23:03:30 1.1 +++ projects/cms/source/util/uk/org/iscream/cms/util/DateUtils.java 2003/02/05 14:27:58 1.8 @@ -1,5 +1,25 @@ +/* + * i-scream central monitoring system + * http://www.i-scream.org.uk + * Copyright (C) 2000-2002 i-scream + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + //---PACKAGE DECLARATION--- -package uk.ac.ukc.iscream.util; +package uk.org.iscream.cms.util; //---IMPORTS--- import java.util.*; @@ -9,7 +29,7 @@ import java.text.*; * Provides easy to use date functions. * * @author $Author: tdb $ - * @version $Id: DateUtils.java,v 1.1 2001/02/03 23:03:30 tdb Exp $ + * @version $Id: DateUtils.java,v 1.8 2003/02/05 14:27:58 tdb Exp $ */ public class DateUtils { @@ -18,13 +38,19 @@ public class DateUtils { /** * The current CVS revision of this class */ - public final String REVISION = "$Revision: 1.1 $"; + public final String REVISION = "$Revision: 1.8 $"; public static final long secsPerMonth = 30*24*60*60; public static final long secsPerWeek = 7*24*60*60; public static final long secsPerDay = 24*60*60; public static final long secsPerHour = 60*60; + public static final long secsPerMin = 60; + private static final String daysKey = "%DAYS%"; + private static final String hoursKey = "%HOURS%"; + private static final String minsKey = "%MINS%"; + private static final String secsKey = "%SECS%"; + //---STATIC METHODS--- /** @@ -119,8 +145,8 @@ public class DateUtils { SimpleDateFormat formatter = new SimpleDateFormat("EEE"); Date conv = new Date(t*1000); return formatter.format(conv); - } - + } + /** * Return a String representation of the time (short format). */ @@ -128,8 +154,81 @@ public class DateUtils { SimpleDateFormat formatter = new SimpleDateFormat("HH:mm"); Date conv = new Date(t*1000); return formatter.format(conv); - } + } + /** + * Format a long time (in seconds) as a String. Can be used to + * clearly layout a period of time in days, hours, minutes and + * seconds. It could, for example, be used to format an uptime. + * This method uses a built in default layout, as shown here; + * "Days: xx, Hours: xx, Mins: xx, Secs: xx" + * + * @param time A long value representing the time period in seconds + * @return A string representation of the given time period + */ + public static String formatTime(long time) { + String defaultLayout = "Days: "+daysKey+", Hours: "+hoursKey+", Mins: "+minsKey+", Secs: "+secsKey; + return formatTime(time, defaultLayout); + } + + /** + * Format a long time (in seconds) as a String. Can be used to + * clearly layout a period of time in days, hours, minutes and + * seconds. It could, for example, be used to format an uptime. + * This method uses a custom layout given as a String. This string + * should contain the following keys; + * "%DAYS% %HOURS% %MINS% %SECS%" + * As an example, the default layout given by formatTime() is + * represented by the following layout String; + * "Days: %DAYS%, Hours: %HOURS%, Mins: %MINS%, Secs: %SECS%" + * + * @param time A long value representing the time period in seconds + * @param layout The custom layout format to use. + * @return A string representation of the given time period + */ + public static String formatTime(long time, String layout) { + // calculate days + String days = new Long(time / secsPerDay).toString(); + time = time % secsPerDay; + + // calculate hours + String hours = new Long(time / secsPerHour).toString(); + time = time % secsPerHour; + + // calculate minutes + String mins = new Long(time / secsPerMin).toString(); + time = time % secsPerMin; + + // seconds are left over + String secs = new Long(time).toString(); + + // put the values into the layout + layout = StringUtils.replaceText(layout, daysKey, days); + layout = StringUtils.replaceText(layout, hoursKey, hours); + layout = StringUtils.replaceText(layout, minsKey, mins); + layout = StringUtils.replaceText(layout, secsKey, secs); + + return layout; + } + + /** + * Takes a time period in seconds and converts it to a + * reasonable message. + * + * @param time the time period in seconds + * @return a String respresentation of the given time period + */ + public static String getTimeString(long time) { + String timeString = null; + if (time >= 3600) { + timeString = ((time/60) / 60) + " hour(s)"; + } else if (time >= 60) { + timeString = (time / 60) + " minute(s)"; + } else { + timeString = time + " second(s)"; + } + return timeString; + } //---CONSTRUCTORS--- @@ -139,7 +238,7 @@ public class DateUtils { * Overrides the {@link java.lang.Object#toString() Object.toString()} * method to provide clean logging (every class should have this). * - * This uses the uk.ac.ukc.iscream.util.FormatName class + * This uses the uk.org.iscream.cms.server.util.FormatName class * to format the toString() * * @return the name of this class and its CVS revision @@ -150,7 +249,7 @@ public class DateUtils { getClass().getName(), REVISION); } - + //---PRIVATE METHODS--- //---ACCESSOR/MUTATOR METHODS---