Author: bpoussin Date: 2013-04-03 22:44:50 +0200 (Wed, 03 Apr 2013) New Revision: 1565 Url: http://nuiton.org/projects/wikitty/repository/revisions/1565 Log: fixes #2620: Support relative date for String date representation Modified: trunk/wikitty-api/src/main/java/org/nuiton/wikitty/DateMathParser.java trunk/wikitty-api/src/main/java/org/nuiton/wikitty/WikittyUtil.java trunk/wikitty-api/src/test/java/org/nuiton/wikitty/WikittyUtilTest.java trunk/wikitty-solr/src/license/THIRD-PARTY.properties Modified: trunk/wikitty-api/src/main/java/org/nuiton/wikitty/DateMathParser.java =================================================================== --- trunk/wikitty-api/src/main/java/org/nuiton/wikitty/DateMathParser.java 2013-03-26 09:10:14 UTC (rev 1564) +++ trunk/wikitty-api/src/main/java/org/nuiton/wikitty/DateMathParser.java 2013-04-03 20:44:50 UTC (rev 1565) @@ -340,6 +340,12 @@ } String[] ops = splitter.split(math); + // si 0 n'est pas vide, alors il y a une erreur, car on ne doit pas avoir de + // valeur dans 0 et un operateur (:=/+-) dans 1 + if (StringUtils.length(ops[0]) != 0) { + throw new ParseException + ("Math expression must began with operator: \"" + ops[0] + "\"", 0); + } int pos = 1; // on passe le 0 qui est vide vu la regex utilisee while ( pos < ops.length ) { Modified: trunk/wikitty-api/src/main/java/org/nuiton/wikitty/WikittyUtil.java =================================================================== --- trunk/wikitty-api/src/main/java/org/nuiton/wikitty/WikittyUtil.java 2013-03-26 09:10:14 UTC (rev 1564) +++ trunk/wikitty-api/src/main/java/org/nuiton/wikitty/WikittyUtil.java 2013-04-03 20:44:50 UTC (rev 1565) @@ -135,7 +135,7 @@ solrDateFormat.getPattern(), DATE_FORMAT, SOLR_DATE_FORMAT, - new SimpleDateFormat().toPattern(), + new SimpleDateFormat().toPattern(), // local dependent "dd/MM/yy", "dd/MM/yy hh:mm", "dd/MM/yyyy", @@ -147,7 +147,8 @@ "yyyyMMdd", "yyyy-MM-dd", "yyyy-MM-dd hh:mm", - "yyyy-MM-dd'T'hh:mm:ss'Z'" + "yyyy-MM-dd'T'hh:mm:ss'Z'", + "yyMMdd", }; /** no break space, used to parse BigDecimal */ @@ -693,40 +694,45 @@ result = ((Calendar)value).getTime(); } else { // try to convert to Date - try { - String v = String.valueOf(value); - if (StringUtils.startsWithIgnoreCase(v, DATE_NOW)) { - result = new Date(); - v = StringUtils.substringAfter(v, DATE_NOW); - } else if (StringUtils.startsWithIgnoreCase(v, DATE_TODAY)) { - result = new Date(); - result = DateUtil.setMinTimeOfDay(result); - v = StringUtils.substringAfter(v, DATE_TODAY); - } else { - for (String pattern : DATE_FORMAT_ALLOWED) { - String tmp = StringUtils.substring(v, 0, pattern.length()); - try { - result = DateUtils.parseDate(tmp, pattern); - // parse success - v = StringUtils.substring(v, pattern.length()); - break; - } catch (Exception eee) { - if (log.isDebugEnabled()) { - log.debug(String.format("Can't parse date '%s'", tmp), eee); - } + String v = String.valueOf(value); + if (StringUtils.startsWithIgnoreCase(v, DATE_NOW)) { + result = new Date(); + v = StringUtils.substringAfter(v, DATE_NOW); + } else if (StringUtils.startsWithIgnoreCase(v, DATE_TODAY)) { + result = new Date(); + result = DateUtil.setMinTimeOfDay(result); + v = StringUtils.substringAfter(v, DATE_TODAY); + } else { + for (String pattern : DATE_FORMAT_ALLOWED) { + if (StringUtils.length(v) < StringUtils.length(pattern)) { + // if pattern longer than v then parse is impossble + continue; + } + String tmp = StringUtils.substring(v, 0, pattern.length()); + try { + result = DateUtils.parseDate(tmp, pattern); + // parse success + String ajust = StringUtils.substring(v, pattern.length()); + + // try to interpret and of date (ex: +3Years) + if (StringUtils.length(ajust) > 0) { + DateMathParser parser = new DateMathParser(TimeZone.getDefault(), Locale.getDefault()); + parser.setNow(result); + result = parser.parseMath(ajust); } + break; + } catch (ParseException eee) { + if (log.isDebugEnabled()) { + log.debug(String.format( + "Can't convert value '%s' of type '%s' to Date because math date is wrong", + value, getClass(value)), eee); + } + } catch (Exception eee) { + if (log.isDebugEnabled()) { + log.debug(String.format("Can't parse date '%s'", tmp), eee); + } } } - // try to interpret and of date (ex: +3Years) - if (StringUtils.length(v) > 0) { - DateMathParser parser = new DateMathParser(TimeZone.getDefault(), Locale.getDefault()); - parser.setNow(result); - result = parser.parseMath(v); - } - } catch (ParseException eee) { - throw new WikittyException(String.format( - "Can't convert value '%s' of type '%s' to Date because math date is wrong", - value, getClass(value)), eee); } if (result == null) { Modified: trunk/wikitty-api/src/test/java/org/nuiton/wikitty/WikittyUtilTest.java =================================================================== --- trunk/wikitty-api/src/test/java/org/nuiton/wikitty/WikittyUtilTest.java 2013-03-26 09:10:14 UTC (rev 1564) +++ trunk/wikitty-api/src/test/java/org/nuiton/wikitty/WikittyUtilTest.java 2013-04-03 20:44:50 UTC (rev 1565) @@ -96,7 +96,8 @@ { SimpleDateFormat format = new SimpleDateFormat("dd/MM/yy hh:mm"); Date d = format.parse("26/03/13 20:51"); - Assert.assertEquals(d, WikittyUtil.toDate("26/03/2013 20:51")); + Date result = WikittyUtil.toDate("26/03/2013 20:51"); + Assert.assertEquals(d, result); } { SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy"); @@ -133,21 +134,25 @@ Date d = format.parse("130326"); Assert.assertEquals(d, WikittyUtil.toDate("130326")); } - { - SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd"); - Date d = format.parse("13-03-26"); - Assert.assertEquals(d, WikittyUtil.toDate("13-03-26")); - } - { - SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd hh:mm"); - Date d = format.parse("13-03-26 20:51"); - Assert.assertEquals(d, WikittyUtil.toDate("13-03-26 20:51")); - } - { - SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd'T' hh:mm:ss'Z'"); - Date d = format.parse("13-03-26'T' 20:51:00'Z'"); - Assert.assertEquals(d, WikittyUtil.toDate("13-03-26'T' 20:51:00'Z'")); - } + + // les syntaxes suivantes sont impossible car on ne peut pas faire + // la difference entre dd-MM-yy et yy-MM-dd. On prend le parti de + // supporter le 1er et pas le 2eme +// { +// SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd"); +// Date d = format.parse("13-03-26"); +// Assert.assertEquals(d, WikittyUtil.toDate("13-03-26")); +// } +// { +// SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd hh:mm"); +// Date d = format.parse("13-03-26 20:51"); +// Assert.assertEquals(d, WikittyUtil.toDate("13-03-26 20:51")); +// } +// { +// SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd'T' hh:mm:ss'Z'"); +// Date d = format.parse("13-03-26'T' 20:51:00'Z'"); +// Assert.assertEquals(d, WikittyUtil.toDate("13-03-26'T' 20:51:00'Z'")); +// } } @Test Modified: trunk/wikitty-solr/src/license/THIRD-PARTY.properties =================================================================== --- trunk/wikitty-solr/src/license/THIRD-PARTY.properties 2013-03-26 09:10:14 UTC (rev 1564) +++ trunk/wikitty-solr/src/license/THIRD-PARTY.properties 2013-04-03 20:44:50 UTC (rev 1565) @@ -7,11 +7,13 @@ # - BSD style # - COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0 # - Common Public License Version 1.0 +# - GNU Lesser General Public License, version 2.1 # - Indiana University Extreme! Lab Software License, vesion 1.1.1 # - Lesser General Public License (LGPL) v 3.0 # - Lesser General Public License (LPGL) # - Lesser General Public License (LPGL) v 2.1 # - MIT License +# - New BSD License # - Public Domain # - Public domain # - TECHNOLOGY LICENSE FROM SUN MICROSYSTEMS, INC. @@ -20,10 +22,10 @@ # Please fill the missing licenses for dependencies : # # -#Wed Feb 01 07:00:14 CET 2012 +#Sat Mar 23 01:47:37 CET 2013 commons-primitives--commons-primitives--1.0=The Apache Software License, Version 2.0 concurrent--concurrent--1.0=TECHNOLOGY LICENSE FROM SUN MICROSYSTEMS, INC. -jakarta-regexp--jakarta-regexp--1.4=The Apache Software License, Version 2.0 javax.servlet--servlet-api--2.5=COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0 javax.transaction--jta--1.1=COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0 +org.apache.zookeeper--zookeeper--3.4.5= xpp3--xpp3_min--1.1.3.4.O=Indiana University Extreme\! Lab Software License, vesion 1.1.1