From 9c4166dbb25e1a0b53e0d776cddc9feececccc78 Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Wed, 18 Dec 2002 23:30:20 +0000 Subject: [PATCH] Create time subpackage git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137194 13f79535-47bb-0310-9956-ffa450edef68 --- build.xml | 29 +- .../lang/{ => time}/CalendarUtils.java | 2 +- .../commons/lang/{ => time}/DateUtils.java | 518 +++++++++--------- .../apache/commons/lang/LangTestSuite.java | 6 +- .../lang/{ => time}/CalendarUtilsTest.java | 2 +- .../lang/{ => time}/DateUtilsTest.java | 290 +++++----- .../commons/lang/time/TimeTestSuite.java | 92 ++++ 7 files changed, 525 insertions(+), 414 deletions(-) rename src/java/org/apache/commons/lang/{ => time}/CalendarUtils.java (99%) rename src/java/org/apache/commons/lang/{ => time}/DateUtils.java (96%) rename src/test/org/apache/commons/lang/{ => time}/CalendarUtilsTest.java (99%) rename src/test/org/apache/commons/lang/{ => time}/DateUtilsTest.java (96%) create mode 100644 src/test/org/apache/commons/lang/time/TimeTestSuite.java diff --git a/build.xml b/build.xml index 1df9a4128..e457f4f27 100644 --- a/build.xml +++ b/build.xml @@ -3,7 +3,7 @@ @@ -156,11 +156,13 @@ @@ -219,4 +221,23 @@ + + + + + + + + + + + + + + + + + diff --git a/src/java/org/apache/commons/lang/CalendarUtils.java b/src/java/org/apache/commons/lang/time/CalendarUtils.java similarity index 99% rename from src/java/org/apache/commons/lang/CalendarUtils.java rename to src/java/org/apache/commons/lang/time/CalendarUtils.java index 28d72fbf4..22b20691d 100644 --- a/src/java/org/apache/commons/lang/CalendarUtils.java +++ b/src/java/org/apache/commons/lang/time/CalendarUtils.java @@ -1,4 +1,4 @@ -package org.apache.commons.lang; +package org.apache.commons.lang.time; /* ==================================================================== * The Apache Software License, Version 1.1 diff --git a/src/java/org/apache/commons/lang/DateUtils.java b/src/java/org/apache/commons/lang/time/DateUtils.java similarity index 96% rename from src/java/org/apache/commons/lang/DateUtils.java rename to src/java/org/apache/commons/lang/time/DateUtils.java index 1b8e0aa5e..2094df837 100644 --- a/src/java/org/apache/commons/lang/DateUtils.java +++ b/src/java/org/apache/commons/lang/time/DateUtils.java @@ -1,259 +1,259 @@ -/* - * The Apache Software License, Version 1.1 - * - * Copyright (c) 2002 The Apache Software Foundation. All rights - * reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * 3. The end-user documentation included with the redistribution, if - * any, must include the following acknowlegement: - * "This product includes software developed by the - * Apache Software Foundation (http://www.apache.org/)." - * Alternately, this acknowlegement may appear in the software itself, - * if and wherever such third-party acknowlegements normally appear. - * - * 4. The names "The Jakarta Project", "Ant", and "Apache Software - * Foundation" must not be used to endorse or promote products derived - * from this software without prior written permission. For written - * permission, please contact apache@apache.org. - * - * 5. Products derived from this software may not be called "Apache" - * nor may "Apache" appear in their names without prior written - * permission of the Apache Group. - * - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR - * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF - * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Software Foundation. For more - * information on the Apache Software Foundation, please see - * . - */ -package org.apache.commons.lang; - -import java.text.ChoiceFormat; -import java.text.DateFormat; -import java.text.MessageFormat; -import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Date; -import java.util.Locale; -import java.util.TimeZone; - -/** - * Helper methods to deal with date/time formatting. [Relies heavily on - * code taken from the DateUtils class of the jakarata-ant project.] - * - * @author Stephane Bailliez - * @author Stefan Bodewig - * - * @since Lang 2.0 - * - * @version $Revision: 1.1 $ - */ -public final class DateUtils { - - /** - * ISO8601-like pattern for date-time. It does not support timezone. - * yyyy-MM-ddTHH:mm:ss - */ - public static final String ISO8601_DATETIME_PATTERN - = "yyyy-MM-dd'T'HH:mm:ss"; - - /** - * ISO8601-like pattern for date. yyyy-MM-dd - */ - public static final String ISO8601_DATE_PATTERN - = "yyyy-MM-dd"; - - /** - * ISO8601-like pattern for time. HH:mm:ss - */ - public static final String ISO8601_TIME_PATTERN - = "HH:mm:ss"; - - /** - * Format used for SMTP (and probably other) Date headers. - */ - public static final DateFormat DATE_HEADER_FORMAT - = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss ", Locale.US); - - -// code from Magesh moved from DefaultLogger and slightly modified - private static final MessageFormat MINUTE_SECONDS - = new MessageFormat("{0}{1}"); - - private static final double[] LIMITS = {0, 1, 2}; - - private static final String[] MINUTES_PART = - {"", "1 minute ", "{0,number} minutes "}; - - private static final String[] SECONDS_PART = - {"0 seconds", "1 second", "{1,number} seconds"}; - - private static final ChoiceFormat MINUTES_FORMAT = - new ChoiceFormat(LIMITS, MINUTES_PART); - - private static final ChoiceFormat SECONDS_FORMAT = - new ChoiceFormat(LIMITS, SECONDS_PART); - - static { - MINUTE_SECONDS.setFormat(0, MINUTES_FORMAT); - MINUTE_SECONDS.setFormat(1, SECONDS_FORMAT); - } - - /** public constructor */ - /// TODO: Insert note that this should not be used - public DateUtils() { - } - - - /** - * Format a date/time into a specific pattern. - * @param date the date to format expressed in milliseconds. - * @param pattern the pattern to use to format the date. - * @return the formatted date. - */ - public static String format(long date, String pattern) { - return format(new Date(date), pattern); - } - - - /** - * Format a date/time into a specific pattern. - * @param date the date to format expressed in milliseconds. - * @param pattern the pattern to use to format the date. - * @return the formatted date. - */ - public static String format(Date date, String pattern) { - DateFormat df = createDateFormat(pattern); - return df.format(date); - } - - - /** - * Format an elapsed time into a plurialization correct string. - * It is limited only to report elapsed time in minutes and - * seconds and has the following behavior. - * - * @param time the elapsed time to report in milliseconds. - * @return the formatted text in minutes/seconds. - */ - public static String formatElapsedTime(long millis) { - long seconds = millis / 1000; - long minutes = seconds / 60; - Object[] args = {new Long(minutes), new Long(seconds % 60)}; - return MINUTE_SECONDS.format(args); - } - - /** - * return a lenient date format set to GMT time zone. - * @param pattern the pattern used for date/time formatting. - * @return the configured format for this pattern. - */ - private static DateFormat createDateFormat(String pattern) { - SimpleDateFormat sdf = new SimpleDateFormat(pattern); - TimeZone gmt = TimeZone.getTimeZone("GMT"); - sdf.setTimeZone(gmt); - sdf.setLenient(true); - return sdf; - } - - /** - * Calculate the phase of the moon for a given date. - * - *

Code heavily influenced by hacklib.c in Nethack

- * - *

The Algorithm: - * - *

-     * moon period = 29.53058 days ~= 30, year = 365.2422 days
-     *
-     * days moon phase advances on first day of year compared to preceding year
-     *  = 365.2422 - 12*29.53058 ~= 11
-     *
-     * years in Metonic cycle (time until same phases fall on the same days of
-     *  the month) = 18.6 ~= 19
-     *
-     * moon phase on first day of year (epact) ~= (11*(year%19) + 18) % 30
-     *  (18 as initial condition for 1900)
-     *
-     * current phase in days = first day phase + days elapsed in year
-     *
-     * 6 moons ~= 177 days
-     * 177 ~= 8 reported phases * 22
-     * + 11/22 for rounding
-     * 
- * - * @return The phase of the moon as a number between 0 and 7 with - * 0 meaning new moon and 4 meaning full moon. - * - * @since 1.2, Ant 1.5 - */ - public static int getPhaseOfMoon(Calendar cal) { - int dayOfTheYear = cal.get(Calendar.DAY_OF_YEAR); - int yearInMetonicCycle = ((cal.get(Calendar.YEAR) - 1900) % 19) + 1; - int epact = (11 * yearInMetonicCycle + 18) % 30; - if ((epact == 25 && yearInMetonicCycle > 11) || epact == 24) { - epact++; - } - return (((((dayOfTheYear + epact) * 6) + 11) % 177) / 22) & 7; - } - - /** - * Returns the current Date in a format suitable for a SMTP date - * header. - * - * @since Ant 1.5.2 - */ - public static String getDateForHeader() { - Calendar cal = Calendar.getInstance(); - TimeZone tz = cal.getTimeZone(); - int offset = tz.getOffset(cal.get(Calendar.ERA), - cal.get(Calendar.YEAR), - cal.get(Calendar.MONTH), - cal.get(Calendar.DAY_OF_MONTH), - cal.get(Calendar.DAY_OF_WEEK), - cal.get(Calendar.MILLISECOND)); - StringBuffer tzMarker = new StringBuffer(offset < 0 ? "-" : "+"); - offset = Math.abs(offset); - int hours = offset / (60 * 60 * 1000); - int minutes = offset / (60 * 1000) - 60 * hours; - if (hours < 10) { - tzMarker.append("0"); - } - tzMarker.append(hours); - if (minutes < 10) { - tzMarker.append("0"); - } - tzMarker.append(minutes); - return DATE_HEADER_FORMAT.format(cal.getTime()) + tzMarker.toString(); - } -} +/* + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "The Jakarta Project", "Ant", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +package org.apache.commons.lang.time; + +import java.text.ChoiceFormat; +import java.text.DateFormat; +import java.text.MessageFormat; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; + +/** + * Helper methods to deal with date/time formatting. [Relies heavily on + * code taken from the DateUtils class of the jakarata-ant project.] + * + * @author Stephane Bailliez + * @author Stefan Bodewig + * + * @since Lang 2.0 + * + * @version $Revision: 1.1 $ + */ +public final class DateUtils { + + /** + * ISO8601-like pattern for date-time. It does not support timezone. + * yyyy-MM-ddTHH:mm:ss + */ + public static final String ISO8601_DATETIME_PATTERN + = "yyyy-MM-dd'T'HH:mm:ss"; + + /** + * ISO8601-like pattern for date. yyyy-MM-dd + */ + public static final String ISO8601_DATE_PATTERN + = "yyyy-MM-dd"; + + /** + * ISO8601-like pattern for time. HH:mm:ss + */ + public static final String ISO8601_TIME_PATTERN + = "HH:mm:ss"; + + /** + * Format used for SMTP (and probably other) Date headers. + */ + public static final DateFormat DATE_HEADER_FORMAT + = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss ", Locale.US); + + +// code from Magesh moved from DefaultLogger and slightly modified + private static final MessageFormat MINUTE_SECONDS + = new MessageFormat("{0}{1}"); + + private static final double[] LIMITS = {0, 1, 2}; + + private static final String[] MINUTES_PART = + {"", "1 minute ", "{0,number} minutes "}; + + private static final String[] SECONDS_PART = + {"0 seconds", "1 second", "{1,number} seconds"}; + + private static final ChoiceFormat MINUTES_FORMAT = + new ChoiceFormat(LIMITS, MINUTES_PART); + + private static final ChoiceFormat SECONDS_FORMAT = + new ChoiceFormat(LIMITS, SECONDS_PART); + + static { + MINUTE_SECONDS.setFormat(0, MINUTES_FORMAT); + MINUTE_SECONDS.setFormat(1, SECONDS_FORMAT); + } + + /** public constructor */ + /// TODO: Insert note that this should not be used + public DateUtils() { + } + + + /** + * Format a date/time into a specific pattern. + * @param date the date to format expressed in milliseconds. + * @param pattern the pattern to use to format the date. + * @return the formatted date. + */ + public static String format(long date, String pattern) { + return format(new Date(date), pattern); + } + + + /** + * Format a date/time into a specific pattern. + * @param date the date to format expressed in milliseconds. + * @param pattern the pattern to use to format the date. + * @return the formatted date. + */ + public static String format(Date date, String pattern) { + DateFormat df = createDateFormat(pattern); + return df.format(date); + } + + + /** + * Format an elapsed time into a plurialization correct string. + * It is limited only to report elapsed time in minutes and + * seconds and has the following behavior. + *
    + *
  • minutes are not displayed when 0. (ie: "45 seconds")
  • + *
  • seconds are always displayed in plural form (ie "0 seconds" or + * "10 seconds") except for 1 (ie "1 second")
  • + *
+ * @param time the elapsed time to report in milliseconds. + * @return the formatted text in minutes/seconds. + */ + public static String formatElapsedTime(long millis) { + long seconds = millis / 1000; + long minutes = seconds / 60; + Object[] args = {new Long(minutes), new Long(seconds % 60)}; + return MINUTE_SECONDS.format(args); + } + + /** + * return a lenient date format set to GMT time zone. + * @param pattern the pattern used for date/time formatting. + * @return the configured format for this pattern. + */ + private static DateFormat createDateFormat(String pattern) { + SimpleDateFormat sdf = new SimpleDateFormat(pattern); + TimeZone gmt = TimeZone.getTimeZone("GMT"); + sdf.setTimeZone(gmt); + sdf.setLenient(true); + return sdf; + } + + /** + * Calculate the phase of the moon for a given date. + * + *

Code heavily influenced by hacklib.c in Nethack

+ * + *

The Algorithm: + * + *

+     * moon period = 29.53058 days ~= 30, year = 365.2422 days
+     *
+     * days moon phase advances on first day of year compared to preceding year
+     *  = 365.2422 - 12*29.53058 ~= 11
+     *
+     * years in Metonic cycle (time until same phases fall on the same days of
+     *  the month) = 18.6 ~= 19
+     *
+     * moon phase on first day of year (epact) ~= (11*(year%19) + 18) % 30
+     *  (18 as initial condition for 1900)
+     *
+     * current phase in days = first day phase + days elapsed in year
+     *
+     * 6 moons ~= 177 days
+     * 177 ~= 8 reported phases * 22
+     * + 11/22 for rounding
+     * 
+ * + * @return The phase of the moon as a number between 0 and 7 with + * 0 meaning new moon and 4 meaning full moon. + * + * @since 1.2, Ant 1.5 + */ + public static int getPhaseOfMoon(Calendar cal) { + int dayOfTheYear = cal.get(Calendar.DAY_OF_YEAR); + int yearInMetonicCycle = ((cal.get(Calendar.YEAR) - 1900) % 19) + 1; + int epact = (11 * yearInMetonicCycle + 18) % 30; + if ((epact == 25 && yearInMetonicCycle > 11) || epact == 24) { + epact++; + } + return (((((dayOfTheYear + epact) * 6) + 11) % 177) / 22) & 7; + } + + /** + * Returns the current Date in a format suitable for a SMTP date + * header. + * + * @since Ant 1.5.2 + */ + public static String getDateForHeader() { + Calendar cal = Calendar.getInstance(); + TimeZone tz = cal.getTimeZone(); + int offset = tz.getOffset(cal.get(Calendar.ERA), + cal.get(Calendar.YEAR), + cal.get(Calendar.MONTH), + cal.get(Calendar.DAY_OF_MONTH), + cal.get(Calendar.DAY_OF_WEEK), + cal.get(Calendar.MILLISECOND)); + StringBuffer tzMarker = new StringBuffer(offset < 0 ? "-" : "+"); + offset = Math.abs(offset); + int hours = offset / (60 * 60 * 1000); + int minutes = offset / (60 * 1000) - 60 * hours; + if (hours < 10) { + tzMarker.append("0"); + } + tzMarker.append(hours); + if (minutes < 10) { + tzMarker.append("0"); + } + tzMarker.append(minutes); + return DATE_HEADER_FORMAT.format(cal.getTime()) + tzMarker.toString(); + } +} diff --git a/src/test/org/apache/commons/lang/LangTestSuite.java b/src/test/org/apache/commons/lang/LangTestSuite.java index 311128573..beef85134 100644 --- a/src/test/org/apache/commons/lang/LangTestSuite.java +++ b/src/test/org/apache/commons/lang/LangTestSuite.java @@ -60,9 +60,9 @@ import junit.textui.TestRunner; /** * Test suite for the Lang package. * - * @author Stephen Colebourne + * @author Stephen Colebourne * @author Ringo De Smet - * @version $Id: LangTestSuite.java,v 1.10 2002/12/16 21:55:20 bayard Exp $ + * @version $Id: LangTestSuite.java,v 1.11 2002/12/18 23:30:19 scolebourne Exp $ */ public class LangTestSuite extends TestCase { @@ -88,10 +88,8 @@ public class LangTestSuite extends TestCase { suite.setName("Commons-Lang Tests"); suite.addTest(ArrayUtilsTest.suite()); suite.addTest(BooleanUtilsTest.suite()); - suite.addTest(CalendarUtilsTest.suite()); suite.addTest(CharSetUtilsTest.suite()); suite.addTest(ClassUtilsTest.suite()); - suite.addTest(DateUtilsTest.suite()); suite.addTest(NumberRangeTest.suite()); suite.addTest(NumberUtilsTest.suite()); suite.addTest(ObjectUtilsTest.suite()); diff --git a/src/test/org/apache/commons/lang/CalendarUtilsTest.java b/src/test/org/apache/commons/lang/time/CalendarUtilsTest.java similarity index 99% rename from src/test/org/apache/commons/lang/CalendarUtilsTest.java rename to src/test/org/apache/commons/lang/time/CalendarUtilsTest.java index 12474a6da..fdf0fe196 100644 --- a/src/test/org/apache/commons/lang/CalendarUtilsTest.java +++ b/src/test/org/apache/commons/lang/time/CalendarUtilsTest.java @@ -1,4 +1,4 @@ -package org.apache.commons.lang; +package org.apache.commons.lang.time; /* ==================================================================== * The Apache Software License, Version 1.1 diff --git a/src/test/org/apache/commons/lang/DateUtilsTest.java b/src/test/org/apache/commons/lang/time/DateUtilsTest.java similarity index 96% rename from src/test/org/apache/commons/lang/DateUtilsTest.java rename to src/test/org/apache/commons/lang/time/DateUtilsTest.java index 8b87108a0..89ef485aa 100644 --- a/src/test/org/apache/commons/lang/DateUtilsTest.java +++ b/src/test/org/apache/commons/lang/time/DateUtilsTest.java @@ -1,145 +1,145 @@ -/* - * The Apache Software License, Version 1.1 - * - * Copyright (c) 2002 The Apache Software Foundation. All rights - * reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * 3. The end-user documentation included with the redistribution, if - * any, must include the following acknowlegement: - * "This product includes software developed by the - * Apache Software Foundation (http://www.apache.org/)." - * Alternately, this acknowlegement may appear in the software itself, - * if and wherever such third-party acknowlegements normally appear. - * - * 4. The names "The Jakarta Project", "Ant", and "Apache Software - * Foundation" must not be used to endorse or promote products derived - * from this software without prior written permission. For written - * permission, please contact apache@apache.org. - * - * 5. Products derived from this software may not be called "Apache" - * nor may "Apache" appear in their names without prior written - * permission of the Apache Group. - * - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR - * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF - * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Software Foundation. For more - * information on the Apache Software Foundation, please see - * . - */ -package org.apache.commons.lang; - -import java.util.Date; -import java.util.Calendar; -import java.util.TimeZone; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; -import junit.textui.TestRunner; - -/** - * TestCase for DateUtils. [Relies heavily on code taken from the - * DateUtilsTest class of the jakarata-ant project.] - * - * @author Stephane Bailliez - * @author Stefan Bodewig - */ -public class DateUtilsTest extends TestCase { - - public static void main(String[] args) { - TestRunner.run(suite()); - } - - public static Test suite() { - TestSuite suite = new TestSuite(DateUtilsTest.class); - suite.setName("DateUtils Tests"); - return suite; - } - - public DateUtilsTest(String s) { - super(s); - } - - public void testElapsedTime(){ - String text = DateUtils.formatElapsedTime(50*1000); - assertEquals("50 seconds", text); - text = DateUtils.formatElapsedTime(65*1000); - assertEquals("1 minute 5 seconds", text); - text = DateUtils.formatElapsedTime(120*1000); - assertEquals("2 minutes 0 seconds", text); - text = DateUtils.formatElapsedTime(121*1000); - assertEquals("2 minutes 1 second", text); - } - - public void testDateTimeISO(){ - TimeZone timeZone = TimeZone.getTimeZone("GMT+1"); - Calendar cal = Calendar.getInstance(timeZone); - cal.set(2002,1,23,10,11,12); - String text = DateUtils.format(cal.getTime(), - DateUtils.ISO8601_DATETIME_PATTERN); - assertEquals("2002-02-23T09:11:12", text); - } - - public void testDateISO(){ - TimeZone timeZone = TimeZone.getTimeZone("GMT"); - Calendar cal = Calendar.getInstance(timeZone); - cal.set(2002,1,23); - String text = DateUtils.format(cal.getTime(), - DateUtils.ISO8601_DATE_PATTERN); - assertEquals("2002-02-23", text); - } - - public void testTimeISODate(){ - // make sure that elapsed time in set via date works - TimeZone timeZone = TimeZone.getTimeZone("GMT+1"); - Calendar cal = Calendar.getInstance(timeZone); - cal.set(2002,1,23, 21, 11, 12); - String text = DateUtils.format(cal.getTime(), - DateUtils.ISO8601_TIME_PATTERN); - assertEquals("20:11:12", text); - } - - public void testTimeISO(){ - // make sure that elapsed time in ms works - long ms = (20*3600 + 11*60 + 12)*1000; - String text = DateUtils.format(ms, - DateUtils.ISO8601_TIME_PATTERN); - assertEquals("20:11:12", text); - } - - public void testPhaseOfMoon() { - TimeZone timeZone = TimeZone.getTimeZone("GMT"); - Calendar cal = Calendar.getInstance(timeZone); - // should be full moon - cal.set(2002, 2, 27); - assertEquals(4, DateUtils.getPhaseOfMoon(cal)); - // should be new moon - cal.set(2002, 2, 12); - assertEquals(0, DateUtils.getPhaseOfMoon(cal)); - } -} +/* + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "The Jakarta Project", "Ant", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +package org.apache.commons.lang.time; + +import java.util.Date; +import java.util.Calendar; +import java.util.TimeZone; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import junit.textui.TestRunner; + +/** + * TestCase for DateUtils. [Relies heavily on code taken from the + * DateUtilsTest class of the jakarata-ant project.] + * + * @author Stephane Bailliez + * @author Stefan Bodewig + */ +public class DateUtilsTest extends TestCase { + + public static void main(String[] args) { + TestRunner.run(suite()); + } + + public static Test suite() { + TestSuite suite = new TestSuite(DateUtilsTest.class); + suite.setName("DateUtils Tests"); + return suite; + } + + public DateUtilsTest(String s) { + super(s); + } + + public void testElapsedTime(){ + String text = DateUtils.formatElapsedTime(50*1000); + assertEquals("50 seconds", text); + text = DateUtils.formatElapsedTime(65*1000); + assertEquals("1 minute 5 seconds", text); + text = DateUtils.formatElapsedTime(120*1000); + assertEquals("2 minutes 0 seconds", text); + text = DateUtils.formatElapsedTime(121*1000); + assertEquals("2 minutes 1 second", text); + } + + public void testDateTimeISO(){ + TimeZone timeZone = TimeZone.getTimeZone("GMT+1"); + Calendar cal = Calendar.getInstance(timeZone); + cal.set(2002,1,23,10,11,12); + String text = DateUtils.format(cal.getTime(), + DateUtils.ISO8601_DATETIME_PATTERN); + assertEquals("2002-02-23T09:11:12", text); + } + + public void testDateISO(){ + TimeZone timeZone = TimeZone.getTimeZone("GMT"); + Calendar cal = Calendar.getInstance(timeZone); + cal.set(2002,1,23); + String text = DateUtils.format(cal.getTime(), + DateUtils.ISO8601_DATE_PATTERN); + assertEquals("2002-02-23", text); + } + + public void testTimeISODate(){ + // make sure that elapsed time in set via date works + TimeZone timeZone = TimeZone.getTimeZone("GMT+1"); + Calendar cal = Calendar.getInstance(timeZone); + cal.set(2002,1,23, 21, 11, 12); + String text = DateUtils.format(cal.getTime(), + DateUtils.ISO8601_TIME_PATTERN); + assertEquals("20:11:12", text); + } + + public void testTimeISO(){ + // make sure that elapsed time in ms works + long ms = (20*3600 + 11*60 + 12)*1000; + String text = DateUtils.format(ms, + DateUtils.ISO8601_TIME_PATTERN); + assertEquals("20:11:12", text); + } + + public void testPhaseOfMoon() { + TimeZone timeZone = TimeZone.getTimeZone("GMT"); + Calendar cal = Calendar.getInstance(timeZone); + // should be full moon + cal.set(2002, 2, 27); + assertEquals(4, DateUtils.getPhaseOfMoon(cal)); + // should be new moon + cal.set(2002, 2, 12); + assertEquals(0, DateUtils.getPhaseOfMoon(cal)); + } +} diff --git a/src/test/org/apache/commons/lang/time/TimeTestSuite.java b/src/test/org/apache/commons/lang/time/TimeTestSuite.java new file mode 100644 index 000000000..d19fec714 --- /dev/null +++ b/src/test/org/apache/commons/lang/time/TimeTestSuite.java @@ -0,0 +1,92 @@ +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ +package org.apache.commons.lang.time; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import junit.textui.TestRunner; +/** + * Test suite for the Time package. + * + * @author Stephen Colebourne + * @version $Id: TimeTestSuite.java,v 1.1 2002/12/18 23:30:19 scolebourne Exp $ + */ +public class TimeTestSuite extends TestCase { + + /** + * Construct a new instance. + */ + public TimeTestSuite(String name) { + super(name); + } + + /** + * Command-line interface. + */ + public static void main(String[] args) { + TestRunner.run(suite()); + } + + /** + * Get the suite of tests + */ + public static Test suite() { + TestSuite suite = new TestSuite(); + suite.setName("Commons-Lang-Time Tests"); + suite.addTest(CalendarUtilsTest.suite()); + suite.addTest(DateUtilsTest.suite()); + return suite; + } +}