Revision: 4153 http://vexi.svn.sourceforge.net/vexi/?rev=4153&view=rev Author: mkpg2 Date: 2011-05-27 02:20:38 +0000 (Fri, 27 May 2011)
Log Message: ----------- Feature. Implement DateDiff (for months only ...). - Improved validation of arguments (java exceptions were being thrown in some cases). Modified Paths: -------------- trunk/org.vexi-library.js/src/main/jpp/org/ibex/js/JSDate.jpp trunk/org.vexi-library.js/src/test/java/test/js/exec/date/methods.js Modified: trunk/org.vexi-library.js/src/main/jpp/org/ibex/js/JSDate.jpp =================================================================== --- trunk/org.vexi-library.js/src/main/jpp/org/ibex/js/JSDate.jpp 2011-05-27 02:17:51 UTC (rev 4152) +++ trunk/org.vexi-library.js/src/main/jpp/org/ibex/js/JSDate.jpp 2011-05-27 02:20:38 UTC (rev 4153) @@ -71,6 +71,12 @@ return JSU.N(value); } + static private JSDate expectDate(JS[] args, int i) throws JSExn{ + JS arg = JSU.expectArg(args, i); + if(arg==null || !(arg instanceof JSDate)) + throw new JSExn("Argument "+i+" expected date, got: "+JSU.toString(arg)); + return (JSDate)arg; + } static final int[] CALENDAR_PART = {Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH}; static final String[] PART_NAMES = {"year","month","day"}; @@ -136,8 +142,8 @@ case 2: { //#switch(JSU.toString(method)) case "compare": - JSDate a = (JSDate)args[0]; - JSDate b = (JSDate)args[1]; + JSDate a = expectDate(args,0); + JSDate b = expectDate(args,1); return JSU.N(a.compareTo(b)); //#end } @@ -185,6 +191,10 @@ return parts[index]; } + private JS getPartJS(int index){ + if(index>=parts.length) return null; + return JSU.N(parts[index]); + } // private JS getPart(int index, int default_){ // int r = getPart(index, -1); // if(r==-1) @@ -224,17 +234,17 @@ public JS get(JS key) throws JSExn { //#switch(JSU.toString(key)) - case "year": return JSU.N(getPart(PART_YEAR)); - case "month": return toIntOrNull(getPart(PART_MONTH)); - case "day": return toIntOrNull(getPart(PART_DAY)); + case "year": return getPartJS(PART_YEAR); + case "month": return getPartJS((PART_MONTH)); + case "day": return getPartJS((PART_DAY)); case "hours": case "minutes": case "seconds": throw new JSExn("Unsupported: "+key); case "addPeriod": return METHOD; + case "dateDiff": return METHOD; case "withPart": return METHOD; - case "subtractDate": return METHOD; //#end return super.get(key); @@ -264,7 +274,7 @@ switch(args.length) { case 2: { //#switch(JSU.toString(method)) - case "addPeriod": + case "addPeriod": { int period = expectPartIndex(JSU.toString(args[0])); int amount = JSU.toInt(args[1]); Calendar c = asCalendar(); @@ -274,8 +284,20 @@ r[i] = getCalendarPart(c,CALENDAR_PART[i]); } return new JSDate(r); - - case "withPart": + } + case "dateDiff": { + String periodStr = JSU.toString(args[0]); + int period = expectPartIndex(periodStr); + JSDate date = expectDate(args,1); + if(period==PART_MONTH){ + int years = getPart(PART_YEAR, 0) - date.getPart(PART_YEAR, 0); + int months = getPart(PART_MONTH, 1) - date.getPart(PART_MONTH, 1); + return JSU.N(years*12 +months); + }else{ + throw new JSExn("Unsupported "+periodStr); + } + } + case "withPart": { int period = expectPartIndex(JSU.toString(args[0])); JS amount = args[1]; if(amount==null){ @@ -283,8 +305,7 @@ }else{ return with(period, JSU.toInt(amount)); } - case "subtractDate": - throw new JSExn("Unsupported: "+method); + } //#end } } Modified: trunk/org.vexi-library.js/src/test/java/test/js/exec/date/methods.js =================================================================== --- trunk/org.vexi-library.js/src/test/java/test/js/exec/date/methods.js 2011-05-27 02:17:51 UTC (rev 4152) +++ trunk/org.vexi-library.js/src/test/java/test/js/exec/date/methods.js 2011-05-27 02:20:38 UTC (rev 4153) @@ -13,6 +13,15 @@ const ym = new Date(2011,01); const ymd = new Date(2011,01,05); +assertEquals(2011,ym.year); +assertEquals(1,ym.month); +assertEquals(null,ym.day); + +assertEquals(2011,ymd.year); +assertEquals(1,ymd.month); +assertEquals(5,ymd.day); + + assertEquals( ymd, ym.withPart("day",ymd.day)); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ vRanger cuts backup time in half-while increasing security. With the market-leading solution for virtual backup and recovery, you get blazing-fast, flexible, and affordable data protection. Download your free trial now. http://p.sf.net/sfu/quest-d2dcopy1 _______________________________________________ Vexi-svn mailing list Vexi-svn@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/vexi-svn