Examples of DateMathParser


Examples of org.apache.solr.util.DateMathParser

        return "Hello World";
      }
  }

  private void resetEvaluatorBagDateMathParser() {
    EvaluatorBag.dateMathParser = new DateMathParser(TimeZone
            .getDefault(), Locale.getDefault()){
      @Override
      public Date getNow() {
        return new Date();
      }
View Full Code Here

Examples of org.apache.solr.util.DateMathParser

   * @param now an optional fixed date to use as "NOW" in the DateMathParser
   * @param val the string to parse
   */
  public Date parseMath(Date now, String val) {
    String math = null;
    final DateMathParser p = new DateMathParser(MATH_TZ, MATH_LOCALE);
   
    if (null != now) p.setNow(now);
   
    if (val.startsWith(NOW)) {
      math = val.substring(NOW.length());
    } else {
      final int zz = val.indexOf(Z);
      if (0 < zz) {
        math = val.substring(zz+1);
        try {
          // p.setNow(toObject(val.substring(0,zz)));
          p.setNow(parseDate(val.substring(0,zz+1)));
        } catch (ParseException e) {
          throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
                                   "Invalid Date in Date Math String:'"
                                   +val+'\'',e);
        }
      } else {
        throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
                                 "Invalid Date String:'" +val+'\'');
      }
    }

    if (null == math || math.equals("")) {
      return p.getNow();
    }
   
    try {
      return p.parseMath(math);
    } catch (ParseException e) {
      throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
                               "Invalid Date Math String:'" +val+'\'',e);
    }
  }
View Full Code Here

Examples of org.apache.solr.util.DateMathParser

   * @param now an optional fixed date to use as "NOW" in the DateMathParser
   * @param val the string to parse
   */
  public Date parseMathLenient(Date now, String val, SolrQueryRequest req) {
    String math = null;
    final DateMathParser p = new DateMathParser(MATH_TZ, MATH_LOCALE);

    if (null != now) p.setNow(now);

    if (val.startsWith(NOW)) {
      math = val.substring(NOW.length());
    } else {
      final int zz = val.indexOf(Z);
      if (0 < zz) {
        math = val.substring(zz+1);
        try {
          // p.setNow(toObject(val.substring(0,zz)));
          p.setNow(parseDateLenient(val.substring(0,zz+1), req));
        } catch (ParseException e) {
          throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
                                   "Invalid Date in Date Math String:'"
                                   +val+'\'',e);
        }
      } else {
        throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
                                 "Invalid Date String:'" +val+'\'');
      }
    }

    if (null == math || math.equals("")) {
      return p.getNow();
    }

    try {
      return p.parseMath(math);
    } catch (ParseException e) {
      throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
                               "Invalid Date Math String:'" +val+'\'',e);
    }
  }
View Full Code Here

Examples of org.apache.solr.util.DateMathParser

   
  }
 
  public void testParseStatelessness() throws Exception {

    DateMathParser p = new DateMathParser(UTC, Locale.US);
    p.setNow(parser.parse("2001-07-04T12:08:56.235"));

    String e = fmt.format(p.parseMath(""));
   
    Date trash = p.parseMath("+7YEARS");
    trash = p.parseMath("/MONTH");
    trash = p.parseMath("-5DAYS+20MINUTES");
    Thread.currentThread().sleep(5);
   
    String a = fmt.format(p.parseMath(""));
    assertEquals("State of DateMathParser changed", e, a);
  }
View Full Code Here

Examples of org.apache.solr.util.DateMathParser

    assertEquals("State of DateMathParser changed", e, a);
  }
   
  public void testParseMath() throws Exception {

    DateMathParser p = new DateMathParser(UTC, Locale.US);
    p.setNow(parser.parse("2001-07-04T12:08:56.235"));

    // No-Op
    assertMath("2001-07-04T12:08:56.235", p, "");
   
    // simple round
    assertMath("2001-07-04T12:08:56.000", p, "/SECOND");
    assertMath("2001-07-04T12:08:00.000", p, "/MINUTE");
    assertMath("2001-07-04T12:00:00.000", p, "/HOUR");
    assertMath("2001-07-04T00:00:00.000", p, "/DAY");
    assertMath("2001-07-01T00:00:00.000", p, "/MONTH");
    assertMath("2001-01-01T00:00:00.000", p, "/YEAR");

    // simple addition
    assertMath("2001-07-04T12:08:56.236", p, "+1MILLISECOND");
    assertMath("2001-07-04T12:08:57.235", p, "+1SECOND");
    assertMath("2001-07-04T12:09:56.235", p, "+1MINUTE");
    assertMath("2001-07-04T13:08:56.235", p, "+1HOUR");
    assertMath("2001-07-05T12:08:56.235", p, "+1DAY");
    assertMath("2001-08-04T12:08:56.235", p, "+1MONTH");
    assertMath("2002-07-04T12:08:56.235", p, "+1YEAR");

    // simple subtraction
    assertMath("2001-07-04T12:08:56.234", p, "-1MILLISECOND");
    assertMath("2001-07-04T12:08:55.235", p, "-1SECOND");
    assertMath("2001-07-04T12:07:56.235", p, "-1MINUTE");
    assertMath("2001-07-04T11:08:56.235", p, "-1HOUR");
    assertMath("2001-07-03T12:08:56.235", p, "-1DAY");
    assertMath("2001-06-04T12:08:56.235", p, "-1MONTH");
    assertMath("2000-07-04T12:08:56.235", p, "-1YEAR");

    // simple '+/-'
    assertMath("2001-07-04T12:08:56.235", p, "+1MILLISECOND-1MILLISECOND");
    assertMath("2001-07-04T12:08:56.235", p, "+1SECOND-1SECOND");
    assertMath("2001-07-04T12:08:56.235", p, "+1MINUTE-1MINUTE");
    assertMath("2001-07-04T12:08:56.235", p, "+1HOUR-1HOUR");
    assertMath("2001-07-04T12:08:56.235", p, "+1DAY-1DAY");
    assertMath("2001-07-04T12:08:56.235", p, "+1MONTH-1MONTH");
    assertMath("2001-07-04T12:08:56.235", p, "+1YEAR-1YEAR");

    // simple '-/+'
    assertMath("2001-07-04T12:08:56.235", p, "-1MILLISECOND+1MILLISECOND");
    assertMath("2001-07-04T12:08:56.235", p, "-1SECOND+1SECOND");
    assertMath("2001-07-04T12:08:56.235", p, "-1MINUTE+1MINUTE");
    assertMath("2001-07-04T12:08:56.235", p, "-1HOUR+1HOUR");
    assertMath("2001-07-04T12:08:56.235", p, "-1DAY+1DAY");
    assertMath("2001-07-04T12:08:56.235", p, "-1MONTH+1MONTH");
    assertMath("2001-07-04T12:08:56.235", p, "-1YEAR+1YEAR");

    // more complex stuff
    assertMath("2000-07-04T12:08:56.236", p, "+1MILLISECOND-1YEAR");
    assertMath("2000-07-04T12:08:57.235", p, "+1SECOND-1YEAR");
    assertMath("2000-07-04T12:09:56.235", p, "+1MINUTE-1YEAR");
    assertMath("2000-07-04T13:08:56.235", p, "+1HOUR-1YEAR");
    assertMath("2000-07-05T12:08:56.235", p, "+1DAY-1YEAR");
    assertMath("2000-08-04T12:08:56.235", p, "+1MONTH-1YEAR");
    assertMath("2000-07-04T12:08:56.236", p, "-1YEAR+1MILLISECOND");
    assertMath("2000-07-04T12:08:57.235", p, "-1YEAR+1SECOND");
    assertMath("2000-07-04T12:09:56.235", p, "-1YEAR+1MINUTE");
    assertMath("2000-07-04T13:08:56.235", p, "-1YEAR+1HOUR");
    assertMath("2000-07-05T12:08:56.235", p, "-1YEAR+1DAY");
    assertMath("2000-08-04T12:08:56.235", p, "-1YEAR+1MONTH");
    assertMath("2000-07-01T00:00:00.000", p, "-1YEAR+1MILLISECOND/MONTH");
    assertMath("2000-07-04T00:00:00.000", p, "-1YEAR+1SECOND/DAY");
    assertMath("2000-07-04T00:00:00.000", p, "-1YEAR+1MINUTE/DAY");
    assertMath("2000-07-04T13:00:00.000", p, "-1YEAR+1HOUR/HOUR");
    assertMath("2000-07-05T12:08:56.000", p, "-1YEAR+1DAY/SECOND");
    assertMath("2000-08-04T12:08:56.000", p, "-1YEAR+1MONTH/SECOND");

    // "tricky" cases
    p.setNow(parser.parse("2006-01-31T17:09:59.999"));
    assertMath("2006-02-28T17:09:59.999", p, "+1MONTH");
    assertMath("2008-02-29T17:09:59.999", p, "+25MONTH");
    assertMath("2006-02-01T00:00:00.000", p, "/MONTH+35DAYS/MONTH");
    assertMath("2006-01-31T17:10:00.000", p, "+3MILLIS/MINUTE");
View Full Code Here

Examples of org.apache.solr.util.DateMathParser

   
  }
 
  public void testParseMathExceptions() throws Exception {
   
    DateMathParser p = new DateMathParser(UTC, Locale.US);
    p.setNow(parser.parse("2001-07-04T12:08:56.235"));
   
    Map<String,Integer> badCommands = new HashMap<String,Integer>();
    badCommands.put("/", 1);
    badCommands.put("+", 1);
    badCommands.put("-", 1);
    badCommands.put("/BOB", 1);
    badCommands.put("+SECOND", 1);
    badCommands.put("-2MILLI/", 4);
    badCommands.put(" +BOB", 0);
    badCommands.put("+2SECONDS ", 3);
    badCommands.put("/4", 1);
    badCommands.put("?SECONDS", 0);

    for (String command : badCommands.keySet()) {
      try {
        Date out = p.parseMath(command);
        fail("Didn't generate ParseException for: " + command);
      } catch (ParseException e) {
        assertEquals("Wrong pos for: " + command + " => " + e.getMessage(),
                     badCommands.get(command).intValue(), e.getErrorOffset());
View Full Code Here

Examples of org.apache.solr.util.DateMathParser

        // delete anything too old, regardless of other policies
        try {
          if (maxCommitAge != null) {
            if (maxCommitAgeTimeStamp==-1) {
              DateMathParser dmp = new DateMathParser(DateField.UTC, Locale.US);
              maxCommitAgeTimeStamp = dmp.parseMath(maxCommitAge).getTime();
            }
            if (commit.getTimestamp() < maxCommitAgeTimeStamp) {
              commit.delete();
              continue;
            }
View Full Code Here

Examples of org.apache.solr.util.DateMathParser

    vri.addNamespace("dataimporter.functions", EvaluatorBag
            .getFunctionsNamespace(Collections.EMPTY_LIST,null));

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    format.setTimeZone(TimeZone.getTimeZone("UTC"));
    DateMathParser dmp = new DateMathParser(TimeZone.getDefault(), Locale.getDefault());

    String s = vri.replaceTokens("${dataimporter.functions.formatDate('NOW/DAY','yyyy-MM-dd HH:mm')}");
    Assert.assertEquals(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(dmp.parseMath("/DAY")), s);
  }
View Full Code Here

Examples of org.apache.solr.util.DateMathParser

    m.put("class",E.class.getName());
    l.add(m);

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    format.setTimeZone(TimeZone.getTimeZone("UTC"));
    DateMathParser dmp = new DateMathParser(TimeZone.getDefault(), Locale.getDefault());

    resolver.addNamespace("dataimporter.functions", EvaluatorBag
            .getFunctionsNamespace(l,null));
    String s = resolver
            .replaceTokens("${dataimporter.functions.formatDate('NOW/DAY','yyyy-MM-dd HH:mm')}");
    Assert.assertEquals(new SimpleDateFormat("yyyy-MM-dd HH:mm")
            .format(dmp.parseMath("/DAY")), s);
    Assert.assertEquals("Hello World", resolver
            .replaceTokens("${dataimporter.functions.test('TEST')}"));
  }
View Full Code Here

Examples of org.apache.solr.util.DateMathParser

  protected DateField f = null;
  protected DateMathParser p = null;
 
  public void setUp() throws Exception {
    super.setUp();
    p = new DateMathParser(UTC, Locale.US);
    f = new DateField();
    // so test can be run against Solr 1.2...
    try {
      Class clazz = Class.forName("org.apache.solr.schema.LegacyDateField");
      f = (DateField) clazz.newInstance();
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.