Repository: groovy Updated Branches: refs/heads/GROOVY_2_5_X dff07ca7d -> c659137e3
improve doco/tests for dateutil module - test could fail for one hour around a daulight saving time change Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/c659137e Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/c659137e Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/c659137e Branch: refs/heads/GROOVY_2_5_X Commit: c659137e311263c1cdc554d7b1d4f70a092c6b8d Parents: dff07ca Author: paulk <[email protected]> Authored: Mon Mar 26 13:18:12 2018 +1000 Committer: paulk <[email protected]> Committed: Mon Mar 26 13:18:12 2018 +1000 ---------------------------------------------------------------------- .../runtime/DefaultGroovyStaticMethods.java | 47 +--------- .../extensions/DateTimeStaticExtensions.java | 4 + subprojects/groovy-dateutil/build.gradle | 2 +- .../dateutil/extensions/DateUtilExtensions.java | 3 +- .../extensions/DateUtilStaticExtensions.java | 91 ++++++++++++++++++++ .../gdk/WorkingWithDateUtilTypesTest.groovy | 11 ++- 6 files changed, 107 insertions(+), 51 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/c659137e/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyStaticMethods.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyStaticMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyStaticMethods.java index 04421af..7d94771 100644 --- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyStaticMethods.java +++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyStaticMethods.java @@ -161,60 +161,19 @@ public class DefaultGroovyStaticMethods { sleepImpl(milliseconds, onInterrupt); } - /** - * Parse a String into a Date instance using the given pattern. - * This convenience method acts as a wrapper for {@link java.text.SimpleDateFormat}. - * <p> - * Note that a new SimpleDateFormat instance is created for every - * invocation of this method (for thread safety). - * - * @param self placeholder variable used by Groovy categories; ignored for default static methods - * @param format pattern used to parse the input string. - * @param input String to be parsed to create the date instance - * @return a new Date instance representing the parsed input string - * @throws ParseException if there is a parse error - * @see java.text.SimpleDateFormat#parse(java.lang.String) - * @since 1.5.7 - */ + @Deprecated public static Date parse(Date self, String format, String input) throws ParseException { return new SimpleDateFormat(format).parse(input); } - /** - * Parse a String into a Date instance using the given pattern and TimeZone. - * This convenience method acts as a wrapper for {@link java.text.SimpleDateFormat}. - * <p> - * Note that a new SimpleDateFormat instance is created for every - * invocation of this method (for thread safety). - * - * @param self placeholder variable used by Groovy categories; ignored for default static methods - * @param format pattern used to parse the input string. - * @param input String to be parsed to create the date instance - * @param zone TimeZone to use when parsing - * @return a new Date instance representing the parsed input string - * @throws ParseException if there is a parse error - * @see java.text.SimpleDateFormat#parse(java.lang.String) - * @since 2.4.1 - */ + @Deprecated public static Date parse(Date self, String format, String input, TimeZone zone) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat(format); sdf.setTimeZone(zone); return sdf.parse(input); } - /** - * Parse a String matching the pattern EEE MMM dd HH:mm:ss zzz yyyy - * containing US-locale-constants only (e.g. Sat for Saturdays). - * Such a string is generated by the toString method of {@link java.util.Date} - * <p> - * Note that a new SimpleDateFormat instance is created for every - * invocation of this method (for thread safety). - * - * @param self placeholder variable used by Groovy categories; ignored for default static methods - * @param dateToString String to be parsed to create the date instance. Must match the pattern EEE MMM dd HH:mm:ss zzz yyyy with US-locale symbols - * @return a new Date instance representing the parsed input string - * @throws ParseException if there is a parse error - */ + @Deprecated public static Date parseToStringDate(Date self, String dateToString) throws ParseException { return new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US).parse(dateToString); } http://git-wip-us.apache.org/repos/asf/groovy/blob/c659137e/subprojects/groovy-datetime/src/main/java/org/apache/groovy/datetime/extensions/DateTimeStaticExtensions.java ---------------------------------------------------------------------- diff --git a/subprojects/groovy-datetime/src/main/java/org/apache/groovy/datetime/extensions/DateTimeStaticExtensions.java b/subprojects/groovy-datetime/src/main/java/org/apache/groovy/datetime/extensions/DateTimeStaticExtensions.java index 9015504..ef9e588 100644 --- a/subprojects/groovy-datetime/src/main/java/org/apache/groovy/datetime/extensions/DateTimeStaticExtensions.java +++ b/subprojects/groovy-datetime/src/main/java/org/apache/groovy/datetime/extensions/DateTimeStaticExtensions.java @@ -46,6 +46,8 @@ public class DateTimeStaticExtensions { /** * Parse text into a {@link java.time.LocalDate} using the provided pattern. * + * Note: the order of parameters differs from versions of this method for the legacy Date class. + * * @param type placeholder variable used by Groovy categories; ignored for default static methods * @param text String to be parsed to create the date instance * @param pattern pattern used to parse the text @@ -63,6 +65,8 @@ public class DateTimeStaticExtensions { /** * Parse text into a {@link java.time.LocalDateTime} using the provided pattern. * + * Note: the order of parameters differs from versions of this method for the legacy Date class. + * * @param type placeholder variable used by Groovy categories; ignored for default static methods * @param text String to be parsed to create the date instance * @param pattern pattern used to parse the text http://git-wip-us.apache.org/repos/asf/groovy/blob/c659137e/subprojects/groovy-dateutil/build.gradle ---------------------------------------------------------------------- diff --git a/subprojects/groovy-dateutil/build.gradle b/subprojects/groovy-dateutil/build.gradle index 296f0fa..000f26d 100644 --- a/subprojects/groovy-dateutil/build.gradle +++ b/subprojects/groovy-dateutil/build.gradle @@ -23,6 +23,6 @@ dependencies { task moduleDescriptor(type: org.codehaus.groovy.gradle.WriteExtensionDescriptorTask) { extensionClasses = 'org.apache.groovy.dateutil.extensions.DateUtilExtensions' -// staticExtensionClasses = 'org.apache.groovy.dateutil.extensions.DateUtilStaticExtensions' + staticExtensionClasses = 'org.apache.groovy.dateutil.extensions.DateUtilStaticExtensions' } compileJava.dependsOn moduleDescriptor http://git-wip-us.apache.org/repos/asf/groovy/blob/c659137e/subprojects/groovy-dateutil/src/main/java/org/apache/groovy/dateutil/extensions/DateUtilExtensions.java ---------------------------------------------------------------------- diff --git a/subprojects/groovy-dateutil/src/main/java/org/apache/groovy/dateutil/extensions/DateUtilExtensions.java b/subprojects/groovy-dateutil/src/main/java/org/apache/groovy/dateutil/extensions/DateUtilExtensions.java index 19cf6dd..b2fb898 100644 --- a/subprojects/groovy-dateutil/src/main/java/org/apache/groovy/dateutil/extensions/DateUtilExtensions.java +++ b/subprojects/groovy-dateutil/src/main/java/org/apache/groovy/dateutil/extensions/DateUtilExtensions.java @@ -20,7 +20,6 @@ package org.apache.groovy.dateutil.extensions; import groovy.lang.Closure; import groovy.lang.GroovyRuntimeException; -import org.codehaus.groovy.runtime.DefaultGroovyMethodsSupport; import java.sql.Timestamp; import java.text.DateFormat; @@ -35,7 +34,7 @@ import java.util.TimeZone; * This class defines new groovy methods which appear on normal JDK * Date and Calendar classes inside the Groovy environment. */ -public class DateUtilExtensions extends DefaultGroovyMethodsSupport { +public class DateUtilExtensions { /** * Support the subscript operator for a Date. http://git-wip-us.apache.org/repos/asf/groovy/blob/c659137e/subprojects/groovy-dateutil/src/main/java/org/apache/groovy/dateutil/extensions/DateUtilStaticExtensions.java ---------------------------------------------------------------------- diff --git a/subprojects/groovy-dateutil/src/main/java/org/apache/groovy/dateutil/extensions/DateUtilStaticExtensions.java b/subprojects/groovy-dateutil/src/main/java/org/apache/groovy/dateutil/extensions/DateUtilStaticExtensions.java new file mode 100644 index 0000000..ddfb1d4 --- /dev/null +++ b/subprojects/groovy-dateutil/src/main/java/org/apache/groovy/dateutil/extensions/DateUtilStaticExtensions.java @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.groovy.dateutil.extensions; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; + +/** + * This class defines new groovy static methods which appear on normal JDK + * Date and Calendar classes inside the Groovy environment. + */ +public class DateUtilStaticExtensions { + /** + * Parse a String into a Date instance using the given pattern. + * This convenience method acts as a wrapper for {@link java.text.SimpleDateFormat}. + * <p> + * Note that a new SimpleDateFormat instance is created for every + * invocation of this method (for thread safety). + * + * @param self placeholder variable used by Groovy categories; ignored for default static methods + * @param format pattern used to parse the input string. + * @param input String to be parsed to create the date instance + * @return a new Date instance representing the parsed input string + * @throws ParseException if there is a parse error + * @see java.text.SimpleDateFormat#parse(java.lang.String) + * @since 1.5.7 + */ + public static Date parse(Date self, String format, String input) throws ParseException { + return new SimpleDateFormat(format).parse(input); + } + + /** + * Parse a String into a Date instance using the given pattern and TimeZone. + * This convenience method acts as a wrapper for {@link java.text.SimpleDateFormat}. + * <p> + * Note that a new SimpleDateFormat instance is created for every + * invocation of this method (for thread safety). + * + * @param self placeholder variable used by Groovy categories; ignored for default static methods + * @param format pattern used to parse the input string. + * @param input String to be parsed to create the date instance + * @param zone TimeZone to use when parsing + * @return a new Date instance representing the parsed input string + * @throws ParseException if there is a parse error + * @see java.text.SimpleDateFormat#parse(java.lang.String) + * @since 2.4.1 + */ + public static Date parse(Date self, String format, String input, TimeZone zone) throws ParseException { + SimpleDateFormat sdf = new SimpleDateFormat(format); + sdf.setTimeZone(zone); + return sdf.parse(input); + } + + /** + * Parse a String matching the pattern EEE MMM dd HH:mm:ss zzz yyyy + * containing US-locale-constants only (e.g. Sat for Saturdays). + * Such a string is generated by the toString method of {@link java.util.Date} + * <p> + * Note that a new SimpleDateFormat instance is created for every + * invocation of this method (for thread safety). + * + * @param self placeholder variable used by Groovy categories; ignored for default static methods + * @param dateToString String to be parsed to create the date instance. Must match the pattern EEE MMM dd HH:mm:ss zzz yyyy with US-locale symbols + * @return a new Date instance representing the parsed input string + * @throws ParseException if there is a parse error + * @since 1.8.4 + */ + public static Date parseToStringDate(Date self, String dateToString) throws ParseException { + return new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US).parse(dateToString); + } + +} http://git-wip-us.apache.org/repos/asf/groovy/blob/c659137e/subprojects/groovy-dateutil/src/spec/test/gdk/WorkingWithDateUtilTypesTest.groovy ---------------------------------------------------------------------- diff --git a/subprojects/groovy-dateutil/src/spec/test/gdk/WorkingWithDateUtilTypesTest.groovy b/subprojects/groovy-dateutil/src/spec/test/gdk/WorkingWithDateUtilTypesTest.groovy index 7599867..ec60df9 100644 --- a/subprojects/groovy-dateutil/src/spec/test/gdk/WorkingWithDateUtilTypesTest.groovy +++ b/subprojects/groovy-dateutil/src/spec/test/gdk/WorkingWithDateUtilTypesTest.groovy @@ -36,14 +36,17 @@ class WorkingWithDateUtilTypesTest extends GroovyTestCase { void testDateArithmetic() { // tag::date_arithmetic[] - def yesterday = new Date() - 1 - def tomorrow = new Date() + 1 + def utc = TimeZone.getTimeZone('UTC') + Date date = Date.parse("yyyy-MM-dd HH:mm", "2010-05-23 09:01", utc) - def diffInDays = tomorrow - yesterday + def prev = date - 1 + def next = date + 1 + + def diffInDays = next - prev assert diffInDays == 2 int count = 0 - yesterday.upto(tomorrow) { count++ } + prev.upto(next) { count++ } assert count == 3 // end::date_arithmetic[] }
