ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Kavanagh <...@dotech.com>
Subject DateSelector patch
Date Tue, 06 Apr 2004 04:19:08 GMT
Here is a patch for the DateSelector. It allows it to take a pattern 
that defines how SimpleDateSelector parses dates. It allows it to deal 
with finer grained time stamps.
I've added a parameter called "pattern" much like the <format> for 
<tstamp> takes. (In fact, just like it).
If the pattern is supplied, it overrides the default date format of the 
DateSelector.

I'd like to have this considered for addition into the next point 
release. It make the build.xml for the <hotswap> target much nicer and 
more straight forward.
Matt, what do you think?

Thanks,
David

*** DateSelector.java    Tue Apr  6 00:14:35 2004
--- DateSelector.java.dak    Tue Apr  6 00:12:26 2004
***************
*** 19,24 ****
--- 19,25 ----
 
  import java.io.File;
  import java.text.DateFormat;
+ import java.text.SimpleDateFormat;
  import java.text.ParseException;
  import java.util.Locale;
 
***************
*** 40,45 ****
--- 41,47 ----
      private boolean includeDirs = false;
      private int granularity = 0;
      private int cmp = 2;
+     private String pattern;
      /** Key to used for parameterized custom selector */
      public static final String MILLIS_KEY = "millis";
      /** Key to used for parameterized custom selector */
***************
*** 50,55 ****
--- 52,59 ----
      public static final String GRANULARITY_KEY = "granularity";
      /** Key to used for parameterized custom selector */
      public static final String WHEN_KEY = "when";
+     /** Key to used for parameterized custom selector */
+     public static final String PATTERN_KEY = "pattern";
 
      /**
       * Creates a new <code>DateSelector</code> instance.
***************
*** 107,132 ****
       */
      public void setDatetime(String dateTime) {
          this.dateTime = dateTime;
-         if (dateTime != null) {
-             DateFormat df = DateFormat.getDateTimeInstance(
-                     DateFormat.SHORT,
-                     DateFormat.SHORT,
-                     Locale.US);
-             try {
-                 setMillis(df.parse(dateTime).getTime());
-                 if (millis < 0) {
-                     setError("Date of " + dateTime
-                             + " results in negative milliseconds value 
relative"
-                             + " to epoch (January 1, 1970, 00:00:00 
GMT).");
-                 }
-             } catch (ParseException pe) {
-                 setError("Date of " + dateTime
-                         + " Cannot be parsed correctly. It should be in"
-                         + " MM/DD/YYYY HH:MM AM_PM format.");
-             }
-         }
      }
 
      /**
       * Should we be checking dates on directories?
       *
--- 111,119 ----
       */
      public void setDatetime(String dateTime) {
          this.dateTime = dateTime;
      }
 
+
      /**
       * Should we be checking dates on directories?
       *
***************
*** 156,161 ****
--- 143,157 ----
      }
 
      /**
+      * Sets the pattern to be used for the SimpleDateFormat
+      *
+      * @param pattern the pattern that defines the date format
+      */
+     public void setPattern(String pattern) {
+         this.pattern = pattern;
+     }
+
+     /**
       * When using this as a custom selector, this method will be called.
       * It translates each parameter into the appropriate setXXX() call.
       *
***************
*** 190,195 ****
--- 186,193 ----
                      TimeComparisons cmp = new TimeComparisons();
                      cmp.setValue(parameters[i].getValue());
                      setWhen(cmp);
+                 } else if (PATTERN_KEY.equalsIgnoreCase(paramname)) {
+                     setPattern(parameters[i].getValue());
                  } else {
                      setError("Invalid parameter " + paramname);
                  }
***************
*** 222,228 ****
--- 220,253 ----
       * @return whether the file should be selected or not
       */
      public boolean isSelected(File basedir, String filename, File file) {
+         // check millis and only set it once.
+         if (millis == -1 && dateTime != null) {
+             DateFormat df = null;
+             if (pattern != null) {
+                 df = new SimpleDateFormat(pattern);
+             }
+             else {
+                 df = DateFormat.getDateTimeInstance(
+                     DateFormat.SHORT,
+                     DateFormat.SHORT,
+                     Locale.US);
+             }
+             try {
+                 setMillis(df.parse(dateTime).getTime());
+                 if (millis < 0) {
+                     setError("Date of " + dateTime
+                             + " results in negative milliseconds value 
relative"
+                             + " to epoch (January 1, 1970, 00:00:00 
GMT).");
+                 }
+             } catch (ParseException pe) {
+                 setError("Date of " + dateTime
+                         + " Cannot be parsed correctly. It should be in"
+                         + " MM/DD/YYYY HH:MM AM_PM format.");
+             }
+         }
+
          validate();
+
          if (file.isDirectory() && (!includeDirs)) {
              return true;
          }





---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message