Revision: 4711
          http://sourceforge.net/p/vexi/code/4711
Author:   mkpg2
Date:     2014-07-31 21:50:12 +0000 (Thu, 31 Jul 2014)
Log Message:
-----------
Date.format(<pattern>).

Modified Paths:
--------------
    branches/vexi3/org.vexi-library.js/src/main/jpp/org/ibex/js/JSDate.jpp
    branches/vexi3/org.vexi-library.value/src/main/java/org/vexi/value/Date.java
    
branches/vexi3/org.vexi-library.value/src/test/java/org/vexi/value/TestDate.java

Modified: branches/vexi3/org.vexi-library.js/src/main/jpp/org/ibex/js/JSDate.jpp
===================================================================
--- branches/vexi3/org.vexi-library.js/src/main/jpp/org/ibex/js/JSDate.jpp      
2014-07-31 15:13:14 UTC (rev 4710)
+++ branches/vexi3/org.vexi-library.js/src/main/jpp/org/ibex/js/JSDate.jpp      
2014-07-31 21:50:12 UTC (rev 4711)
@@ -204,6 +204,7 @@
         case "asInstant": return METHOD;
         case "diff":      return METHOD; 
         case "withPart":  return METHOD;
+        case "format":    return METHOD;
         //#end
         return super.get(jskey);
     }
@@ -237,6 +238,10 @@
                 int amount = JSU.expectArg_int(args,1);
                 return new JSDate(date.with(partId, amount));
             }
+            case "format": {
+               String format = JSU.expectArg_string(args,0);
+               return JSU.S(date.format(format));
+            }
             //#end
             return super.callMethod(this_, method, args);
         }catch(ValueException e){

Modified: 
branches/vexi3/org.vexi-library.value/src/main/java/org/vexi/value/Date.java
===================================================================
--- 
branches/vexi3/org.vexi-library.value/src/main/java/org/vexi/value/Date.java    
    2014-07-31 15:13:14 UTC (rev 4710)
+++ 
branches/vexi3/org.vexi-library.value/src/main/java/org/vexi/value/Date.java    
    2014-07-31 21:50:12 UTC (rev 4711)
@@ -331,6 +331,74 @@
         }
     }
 
+    private boolean isPatternChar(char c){
+       switch(c){
+       case 'Y': case 'M': case 'D': case 'm':
+               return true;
+       }
+       return false;
+    }
+
+    static final String[] MONTHS = {
+        "January", "February", "March", "April", "May", "June",
+        "July", "August", "September", "October", "November", "December"
+    };
+    
+    private String formatPart(char c, int run) throws ValueException{
+       switch(c){
+       case 'Y':{
+               String s = ""+getPart(Date.PART_YEAR);
+               if(run==2){
+                       return padZero(s.substring(Math.max(0, s.length()-2)));
+               }                       
+               return s;               
+       }
+       case 'M': { 
+               int month = getPart(Date.PART_MONTH);
+               if(run>=2){
+                       return padZero(month);
+               }
+               return ""+month;
+       }
+       case 'D': 
+               int day = getPart(Date.PART_DAY);
+               if(run>=2){
+                       return padZero(day);
+               }
+               return ""+day;
+               
+       case 'm':{
+                       int monthIndex = getPart(Date.PART_MONTH)-1;
+                       String month = MONTHS[monthIndex];
+               if(run<=3){
+                       month = month.substring(0,3);
+               }
+               return month;
+       }
+       }
+       throw new Error();
+    }
+    
+    public String format(String format) throws ValueException{
+       StringBuilder sb = new StringBuilder();
+       for(int i=0; i<format.length(); i++){
+               char c0 = format.charAt(i);
+               if(isPatternChar(c0)){
+                       int j=i+1;
+                       while(j<format.length() && format.charAt(j)==c0){
+                               j++;
+                       }
+                       int run = j-i;
+                       sb.append(formatPart(c0,run));
+                       i=j-1;
+               }else{
+                       sb.append(c0);
+               }
+       }
+       return sb.toString();
+    }
+    
+    
     public String format() { 
         int year = expectPart(PART_YEAR);
         if(scheme==SCHEME_Y){
@@ -459,12 +527,13 @@
     }
     public Scheme getScheme(){ return scheme; }
     
-
-    private String padZero(int n) {
-        if (n<10) return "0"+n;
-        return ""+n;
+    private String padZero(int n) { return padZero(""+n); }
+    private String padZero(String s) {
+        if(s.length()==1) return "0"+s;
+       return s;
     }
     
+    
 
     private Calendar asCalendar() {
        if(hasPart(PART_WEEK)){
@@ -665,6 +734,6 @@
        } while (cursor.getTimeInMillis() <= endInstant);  
        return presumedDays;  
     }
-    
 
+
 }

Modified: 
branches/vexi3/org.vexi-library.value/src/test/java/org/vexi/value/TestDate.java
===================================================================
--- 
branches/vexi3/org.vexi-library.value/src/test/java/org/vexi/value/TestDate.java
    2014-07-31 15:13:14 UTC (rev 4710)
+++ 
branches/vexi3/org.vexi-library.value/src/test/java/org/vexi/value/TestDate.java
    2014-07-31 21:50:12 UTC (rev 4711)
@@ -101,5 +101,15 @@
        
assertEquals(Date.newYW(2010,24),Date.newYW(2011,24).addPeriod(Date.PART_YEAR,-1));
 // Sat
     }
     
+    public void testFormat() throws ValueException{
+       Date date = Date.newYMD(2013,8,24);
+       assertEquals("24/08/13", date.format("DD/MM/YY"));
+       assertEquals("24/8/13", date.format("D/M/YY"));
+       assertEquals("24/8/2013", date.format("D/M/YYYY"));
+       assertEquals("08/24/2013", date.format("MM/DD/YYYY"));
+    }
     
+    static public void main(String[] args) throws ValueException {
+               new TestDate().testFormat();
+       }
 }

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Infragistics Professional
Build stunning WinForms apps today!
Reboot your WinForms applications with our WinForms controls. 
Build a bridge from your legacy apps to the future.
http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk
_______________________________________________
Vexi-svn mailing list
Vexi-svn@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/vexi-svn

Reply via email to