Revision: 4542 http://sourceforge.net/p/vexi/code/4542 Author: mkpg2 Date: 2013-08-27 21:56:49 +0000 (Tue, 27 Aug 2013) Log Message: ----------- JSDate. Support weekday property. Date. Refactored.
Modified Paths: -------------- branches/vexi3/org.vexi-library.js/src/main/jpp/org/ibex/js/JSDate.jpp branches/vexi3/org.vexi-library.js/src/test/java/test/js/exec/date/TestDate.java branches/vexi3/org.vexi-library.js/src/test/java/test/js/exec/object/copy.js 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/Instant.java branches/vexi3/org.vexi-library.value/src/test/java/org/vexi/value/TestDate.java Added Paths: ----------- branches/vexi3/org.vexi-library.js/src/test/java/test/js/exec/date/week.js 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 2013-08-23 11:21:39 UTC (rev 4541) +++ branches/vexi3/org.vexi-library.js/src/main/jpp/org/ibex/js/JSDate.jpp 2013-08-27 21:56:49 UTC (rev 4542) @@ -106,8 +106,8 @@ private JS getPartJS(String key) throws JSExn { try{ - int partId = Date.getPartId(key); - if (!date.hasPart(partId)) return null; + int partId = Date.partId(key); + if (!date.hasPartExactly(partId)) return null; return JSU.N(date.getPart(partId)); }catch(ValueException e){ throw new JSExn(e); @@ -145,13 +145,12 @@ /* The year as a standard Gregorian date. * @type(Number) */ case "day": return getPartJS(key); - - case "weekday": - case "hours": - case "minutes": - case "seconds": - throw new JSExn("Unsupported: "+key); - case "scheme": return JSU.S(Date.getSchemeName(date.getScheme())); + case "weekday": return getPartJS(key); + case "hours": throw new JSExn("Unsupported: "+key); + case "minutes": throw new JSExn("Unsupported: "+key); + case "seconds": throw new JSExn("Unsupported: "+key); + + case "scheme": return JSU.S(date.getScheme().name); case "addPeriod": return METHOD; case "as": return METHOD; @@ -177,17 +176,17 @@ return new JSInstant(r); } case "addPeriod": { - int partId = Date.getPartId(JSU.toString(JSU.expectArg(args,0))); + int partId = Date.partId(JSU.toString(JSU.expectArg(args,0))); int amount = JSU.expectArg_int(args,1); return new JSDate(date.addPeriod(partId, amount)); } case "diff": { - int partId = Date.getPartId(JSU.toString(JSU.expectArg(args,0))); + int partId = Date.partId(JSU.toString(JSU.expectArg(args,0))); JSDate from = expectDate(args,1); return JSU.N(date.diff(partId, from.date)); } case "withPart": { - int partId = Date.getPartId(JSU.toString(JSU.expectArg(args,0))); + int partId = Date.partId(JSU.toString(JSU.expectArg(args,0))); int amount = JSU.expectArg_int(args,1); return new JSDate(date.with(partId, amount)); } Modified: branches/vexi3/org.vexi-library.js/src/test/java/test/js/exec/date/TestDate.java =================================================================== --- branches/vexi3/org.vexi-library.js/src/test/java/test/js/exec/date/TestDate.java 2013-08-23 11:21:39 UTC (rev 4541) +++ branches/vexi3/org.vexi-library.js/src/test/java/test/js/exec/date/TestDate.java 2013-08-27 21:56:49 UTC (rev 4542) @@ -11,7 +11,7 @@ public static void main(String[] args) throws Throwable { JSTestSuite jts = new JSTestSuite(TestDate.class); - String SCRIPT = "methods.js"; + String SCRIPT = "week.js"; //String SCRIPT = "testNumber2String.js"; TestCase t = jts.createTestCase(jts.getResourceDirs(), SCRIPT); t.runBare(); Added: branches/vexi3/org.vexi-library.js/src/test/java/test/js/exec/date/week.js =================================================================== --- branches/vexi3/org.vexi-library.js/src/test/java/test/js/exec/date/week.js (rev 0) +++ branches/vexi3/org.vexi-library.js/src/test/java/test/js/exec/date/week.js 2013-08-27 21:56:49 UTC (rev 4542) @@ -0,0 +1,9 @@ +sys.import("shared"); + +const Date = sys.js.Date; + +var today = Date.today; +sys.trace(["today", today.weekday]); + +const ymd = new Date("YMD",2011,01,05); +sys.trace(["2011-01-05", ymd.weekday]); Property changes on: branches/vexi3/org.vexi-library.js/src/test/java/test/js/exec/date/week.js ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Modified: branches/vexi3/org.vexi-library.js/src/test/java/test/js/exec/object/copy.js =================================================================== --- branches/vexi3/org.vexi-library.js/src/test/java/test/js/exec/object/copy.js 2013-08-23 11:21:39 UTC (rev 4541) +++ branches/vexi3/org.vexi-library.js/src/test/java/test/js/exec/object/copy.js 2013-08-27 21:56:49 UTC (rev 4542) @@ -1,8 +1,8 @@ var aref = { }; -var orig = { key1:123, foo:"bar", ref:aref }; -var copy = orig.copy(); +var orig = { key:123, foo:"bar", ref:aref }; +var copy = sys.js.copy(orig); assert(copy != orig); assert(copy.key == 123); -assert(copy.bar == "bar"); +assert(copy.foo == "bar"); assert(copy.ref == aref); 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 2013-08-23 11:21:39 UTC (rev 4541) +++ branches/vexi3/org.vexi-library.value/src/main/java/org/vexi/value/Date.java 2013-08-27 21:56:49 UTC (rev 4542) @@ -37,125 +37,155 @@ */ public class Date implements Comparable { - static final public int SCHEME_Y = 0; - static final public int SCHEME_YM = 1; - static final public int SCHEME_YMD = 2; - static final public int SCHEME_YQ = 3; - static final private String[] SCHEMES = {"Y","YM","YMD","YQ"}; - static final private int[] SCHEME_LENGTHS = {1,2,3,2}; + static public Date newY(int y){ return new Date(SCHEME_Y, new int[]{y}); } + static public Date newYM(int y, int m){ return new Date(SCHEME_YM, new int[]{y, m}); } + static public Date newYMD(int y, int m, int d){ return new Date(SCHEME_YMD, new int[]{y, m, d}); } + static public Date newYQ(int y, int q){ return new Date(SCHEME_YQ, new int[]{y, q}); } + static public Date newDate(String schemeStr, int[] parts) throws ValueException{ + Scheme scheme = getScheme(schemeStr); + return scheme.newDate(parts); + } + + static final public int - PART_YEAR = 0, - PART_MONTH = -1, - PART_DAY = -2, - PART_QUARTER = -3; - - static public int getScheme(String s) throws ValueException{ - for(int i=0; i<SCHEMES.length; i++){ - if(SCHEMES[i].equals(s)){ - return i; - } - } - throw new ValueException("No such date scheme: "+s); - } - static public String getSchemeName(int scheme) { - return SCHEMES[scheme]; - } - - static public int getPartId(String partName) throws ValueException{ + PART_YEAR = 0, + PART_MONTH = -1, + PART_DAY = -2, + PART_QUARTER = -3, + PART_WEEKDAY = -4; + static public int partId(String partName) throws ValueException{ if("year".equals(partName)) return PART_YEAR; if("month".equals(partName)) return PART_MONTH; if("day".equals(partName)) return PART_DAY; if("quarter".equals(partName)) return PART_QUARTER; + if("weekday".equals(partName)) return PART_WEEKDAY; throw new ValueException("No such part: "+partName); } - - static private int getPartIndex(int part){ - switch(part){ - case PART_YEAR: return 0; - case PART_MONTH: return 1; - case PART_DAY: return 2; - case PART_QUARTER: return 1; - } - throw new Error("No such part: "+part); - } - static public String getPartName(int part){ - switch(part){ + static public String partName(int partId){ + switch(partId){ case PART_YEAR: return "year"; case PART_MONTH: return "month"; case PART_DAY: return "day"; case PART_QUARTER: return "quarter"; + case PART_WEEKDAY: return "weekday"; } - throw new Error("No such part: "+part); + throw new Error("No such part: "+partId); } - - static private void setPart(int part, int[] vs, int value){ - int index = getPartIndex(part); - vs[index] = value; + static public int partGet(int partId, Calendar cal){ + switch(partId){ + case PART_YEAR: return cal.get(Calendar.YEAR); + case PART_MONTH: return cal.get(Calendar.MONTH)+1; + case PART_DAY: return cal.get(Calendar.DAY_OF_MONTH); + case PART_QUARTER: return (cal.get(Calendar.MONTH)/3)+1; + case PART_WEEKDAY: return cal.get(Calendar.DAY_OF_WEEK); + } + throw new Error("No such part: "+partId); } - static private int getPart(int part, int[] vs){ - int index = getPartIndex(part); - return vs[index]; + + static private boolean partValid(int partId, int value) throws ValueException { + switch(partId){ + case PART_DAY: + // HACK should check based on month + return (value>=1 && value<=31); + case PART_MONTH: + return value>=1 && value<=12; + case PART_QUARTER: + return value>=1 && value<=4; + case PART_WEEKDAY: + return value>=1 || value<=7; + } + return true; } - - static public Date newY(int y){ return new Date(SCHEME_Y, new int[]{y}); } - static public Date newYM(int y, int m){ return new Date(SCHEME_YM, new int[]{y, m}); } - static public Date newYMD(int y, int m, int d){ return new Date(SCHEME_YMD, new int[]{y, m, d}); } - static public Date newYQ(int y, int q){ return new Date(SCHEME_YQ, new int[]{y, q}); } - + static private ValueException invalid(String part, int value) { + return new ValueException("Invalid "+part+": "+value); + } - static public Date newDate(String schemeStr, int[] parts) throws ValueException{ - int scheme = getScheme(schemeStr); - return new Date(scheme, parts); - } - static public Date newDate(int scheme, int[] parts) throws ValueException{ - checkValidity(scheme, parts); - return new Date(scheme, parts); - } - static public Date newDate(int scheme, Calendar cal) { - int year = cal.get(Calendar.YEAR); - switch(scheme){ - case SCHEME_Y: return newY(year); - case SCHEME_YM: return newYM(year, cal.get(Calendar.MONTH)+1); - case SCHEME_YMD: return newYMD(year, cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH)); - case SCHEME_YQ: return newYQ(year, (cal.get(Calendar.MONTH)/3)+1); + static public class Scheme{ + final public String name; + final int[] partIds; + Scheme(String name, int[] partIds){ + this.name = name; + this.partIds = partIds; + } + + int getPartIndex(int partId){ + for(int i=0; i<partIds.length; i++){ + if(partIds[i]==partId){ + return i; + } + } + return -1; + } + + void setPart(int partId, int[] vs, int value){ + int index = getPartIndex(partId); + vs[index] = value; } - throw new Error("!"); - } + + Date newDate(Calendar cal) { + int[] parts = new int[partIds.length]; + for(int i=0; i<partIds.length; i++){ + parts[i] = partGet(partIds[i], cal); + } + return newDateNoCheck(parts); + } + + Date newDate(int[] parts) throws ValueException{ + // conservatively always checking, may skip this test in certain situations + checkValidity(parts); + return newDateNoCheck(parts); + } + Date newDateNoCheck(int[] parts) { + return new Date(this, parts); + } + + void checkValidity(int[] parts) throws ValueException{ + int length = partIds.length; + if(length!=parts.length) throw new ValueException("Invalid number of parts, expected "+length+", got "+parts.length); + + for(int i=0; i<partIds.length; i++){ + int partId = partIds[i]; + int part = parts[i]; + if(!partValid(partId, part)){ + throw invalid(partName(partId), part); + } + } + } + + } - static private ValueException invalid(String part, int value) { - return new ValueException("Invalid "+part+": "+value); - } + static final public Scheme SCHEME_Y = new Scheme("Y", new int[]{PART_YEAR}); + static final public Scheme SCHEME_YM = new Scheme("YM", new int[]{PART_YEAR, PART_MONTH}); + static final public Scheme SCHEME_YMD = new Scheme("YMD", new int[]{PART_YEAR, PART_MONTH, PART_DAY}); + static final public Scheme SCHEME_YQ = new Scheme("YQ", new int[]{PART_YEAR, PART_QUARTER}); - static private void checkValidity(int scheme, int[] parts) throws ValueException { - int length = SCHEME_LENGTHS[scheme]; - if(length!=parts.length) throw new ValueException("Invalid number of parts, expected "+length+", got "+parts.length); - - switch(scheme){ - case SCHEME_YMD: - int day = getPart(PART_DAY,parts); - // HACK should check - if (day<1 || day>31) throw invalid("day", day); - - case SCHEME_YM: - int month = getPart(PART_MONTH,parts); - if (month<1 || month>12) throw invalid("month", month); - break; - case SCHEME_YQ: - int quarter = getPart(PART_QUARTER,parts); - if (quarter<1 || quarter>4) throw invalid("quarter", quarter); - break; + + static final private Scheme[] SCHEMES = { + SCHEME_Y, + SCHEME_YM, + SCHEME_YMD, + SCHEME_YQ + }; + + static public Scheme getScheme(String name) throws ValueException{ + for(int i=0; i<SCHEMES.length; i++){ + Scheme s = SCHEMES[i]; + if(s.name.equals(name)){ + return s; + } } + throw new ValueException("No such date scheme: "+name); } - + // ... etc. //static final public int SCHEME_YQW; //static final public int SCHEME_YMD; @@ -167,12 +197,8 @@ // static final public int PART_MONTH = 1; // static final public int PART_DAY = 2; // - static int getCalendarPart(Calendar c, int index) { - int r = c.get(index); - if (index == Calendar.MONTH) r = r +1; - return r; - } + static Pattern p = Pattern.compile("([0-9]{1,4})([q-])?([0-9]{1,2})?(-([0-9]{1,2}))?"); static public Date parseString(String s) throws ValueException{ Date r = tryParseString(s); @@ -211,19 +237,19 @@ public String format() { int year = expectPart(PART_YEAR); - switch(scheme){ - case SCHEME_Y: - return year+""; - case SCHEME_YM:{ + if(scheme==SCHEME_Y){ + return year+""; + } + if(scheme==SCHEME_YM){ int month = expectPart(PART_MONTH); - return year+"-"+padZero(month); + return year+"-"+padZero(month); } - case SCHEME_YMD: { + if(scheme==SCHEME_YMD){ int month = expectPart(PART_MONTH); int day = expectPart(PART_DAY); - return year+"-"+padZero(month)+"-"+padZero(day); + return year+"-"+padZero(month)+"-"+padZero(day); } - case SCHEME_YQ: + if(scheme==SCHEME_YQ){ int quarter = expectPart(PART_QUARTER); return year+"q"+quarter; } @@ -232,24 +258,25 @@ - final int scheme; + final Scheme scheme; final int[] parts; - private Date(int scheme, int[] parts) { + private Date(Scheme scheme, int[] parts) { this.scheme = scheme; this.parts = parts; } - public boolean hasPart(int part) { - if(part == PART_YEAR) return true; - switch(scheme){ - case SCHEME_YMD:if(part == PART_DAY) return true; - case SCHEME_YM: return part == PART_MONTH; - case SCHEME_YQ: return part == PART_QUARTER; - } - return false; + public boolean hasPart(int partId) { + return scheme.getPartIndex(partId)!=-1; } + public boolean hasPartExactly(int partId) { + if(hasPart(partId)) return true; + if(partId==PART_QUARTER) hasPart(PART_MONTH); + if(partId==PART_WEEKDAY) hasPart(PART_DAY); + return false; + } + private int getPartByIndex(int index, int default_){ if(index>=parts.length) return default_; return parts[index]; @@ -257,12 +284,14 @@ public int getPart(int partId, int default_) { if(!hasPart(partId)) return default_; - int index = getPartIndex(partId); + int index = scheme.getPartIndex(partId); return parts[index]; } public int getPart(int partId) throws ValueException { - if (!hasPart(partId)) throw new ValueException("No value for part: "+getPartName(partId)); - int index = getPartIndex(partId); + if (!hasPart(partId)) { + return partGet(partId, asCalendar()); + } + int index = scheme.getPartIndex(partId); return parts[index]; } public int getPartDefault(int part){ @@ -287,10 +316,10 @@ return 1; } public int expectPart(int part) { - int index = getPartIndex(part); + int index = scheme.getPartIndex(part); return parts[index]; } - public int getScheme(){ return scheme; } + public Scheme getScheme(){ return scheme; } private String padZero(int n) { @@ -317,41 +346,32 @@ } public Date as(String scheme) throws ValueException { return as(getScheme(scheme)); } - public Date as(int scheme2) throws ValueException { + public Date as(Scheme scheme2) throws ValueException { if(this.scheme == scheme2) return this; - int year = expectPart(PART_YEAR); - switch(scheme2){ - case SCHEME_Y: return newY(year); - case SCHEME_YM: return newYM(year, getPartDefault(PART_MONTH)); - case SCHEME_YMD: return newYMD(year, getPartDefault(PART_MONTH), getPartDefault(PART_DAY)); - case SCHEME_YQ: return newYQ(year, getPartDefault(PART_QUARTER)); - } - throw new ValueException("Cannot convert date "+getSchemeName(scheme)+" to "+getSchemeName(scheme2)); + return scheme2.newDate(asCalendar()); } + public Date with(int part, int value) throws ValueException { if(hasPart(part)){ int[] r = new int[parts.length]; System.arraycopy(parts, 0, r, 0, r.length); - setPart(part, r, value); - return newDate(scheme, r); + scheme.setPart(part, r, value); + return scheme.newDate(r); }else{ int year = expectPart(PART_YEAR); - switch(scheme){ - case SCHEME_Y: + if(SCHEME_Y==scheme){ if(part==PART_MONTH) return newYM(year, value); if(part==PART_QUARTER) return newYQ(year, value); - break; - case SCHEME_YM: + }else if(SCHEME_YM==scheme){ if(part==PART_DAY) return newYMD(year, expectPart(PART_MONTH), value); - break; } } - throw new ValueException("Cannot derive "+getSchemeName(scheme)+" with date part "+getPartName(part)); + throw new ValueException("Cannot derive "+scheme.name+" with date part "+partName(part)); } public Date addPeriod(int part, int amount) throws ValueException{ - if(!hasPart(part)) throw new ValueException("Cannot add "+getPartName(part)+" to "+ getSchemeName(scheme)); + if(!hasPart(part)) throw new ValueException("Cannot add "+partName(part)+" to "+ scheme.name); Calendar c = asCalendar(); switch(part){ case PART_YEAR: @@ -367,7 +387,7 @@ c.add(Calendar.DAY_OF_MONTH, amount); break; } - return newDate(scheme, c); + return scheme.newDate(c); } public int diff(int part, Date from) throws ValueException{ int years = getPart(PART_YEAR)-from.getPart(PART_YEAR); @@ -384,7 +404,7 @@ return years*4 +quarters; } - throw new Error("Unsupported dateDiff: "+getPartName(part)); + throw new Error("Unsupported dateDiff: "+partName(part)); } public int hashCode() { Modified: branches/vexi3/org.vexi-library.value/src/main/java/org/vexi/value/Instant.java =================================================================== --- branches/vexi3/org.vexi-library.value/src/main/java/org/vexi/value/Instant.java 2013-08-23 11:21:39 UTC (rev 4541) +++ branches/vexi3/org.vexi-library.value/src/main/java/org/vexi/value/Instant.java 2013-08-27 21:56:49 UTC (rev 4542) @@ -107,7 +107,7 @@ } public Date getDate(TimeZone timezone) { - return Date.newDate(Date.SCHEME_YMD, asCalendar(timezone)); + return Date.SCHEME_YMD.newDate(asCalendar(timezone)); } public String getTime(TimeZone timezone) { 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 2013-08-23 11:21:39 UTC (rev 4541) +++ branches/vexi3/org.vexi-library.value/src/test/java/org/vexi/value/TestDate.java 2013-08-27 21:56:49 UTC (rev 4542) @@ -74,4 +74,12 @@ public void testCompare() throws ValueException { assertEquals(Date.newY(2000).compareTo(Date.newY(1999)), new Long(2000).compareTo(new Long(1999))); } + + + public void testWeekdays() throws ValueException { + assertEquals(7,Date.newYMD(2013,8,24).getPart(Date.PART_WEEKDAY)); // Sat + assertEquals(1,Date.newYMD(2013,8,25).getPart(Date.PART_WEEKDAY)); // Sun + assertEquals(2,Date.newYMD(2013,8,26).getPart(Date.PART_WEEKDAY)); // Mon + assertEquals(3,Date.newYMD(2013,8,27).getPart(Date.PART_WEEKDAY)); // Tue + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more! Discover the easy way to master current and previous Microsoft technologies and advance your career. Get an incredible 1,500+ hours of step-by-step tutorial videos with LearnDevNow. Subscribe today and save! http://pubads.g.doubleclick.net/gampad/clk?id=58040911&iu=/4140/ostg.clktrk _______________________________________________ Vexi-svn mailing list Vexi-svn@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/vexi-svn