joestelmach / natty

Java natural language date parser
http://natty.joestelmach.com/
MIT License
520 stars 183 forks source link

"first monday in 2 months" gives one week too early (last monday of preceding month) #100

Closed coulas closed 9 years ago

coulas commented 9 years ago

works well on http://natty.joestelmach.com/try.jsp But I can't make it work with natty in version 0.9 from maven. today, asking for "first monday in 2 months" gives <2015-02-23> instead of <2015-03-02> with tree : (DATE_TIME_ALTERNATIVE (DATE_TIME (RELATIVE_DATE (SEEK > by_day 2 month) (EXPLICIT_SEEK 1 (DAY_OF_WEEK 2))))) wich seems to be same on your web site.

Do you use a TimeZone on your web site, or any kind of code incompatible from this naive one :

Parser p = new Parser();
List<DateGroup> dateGrps = p.parse("first monday in 2 months");
if (dateGrps != null && !dateGrps.isEmpty()) {
    DateGroup dateGrp = dateGrps.get(0);
    if (dateGrp != null) {
        List<Date> dates = dateGrp.getDates();
        if (dates != null && !dates.isEmpty()) {
            rval = new LocalDate(dates.get(0));
        }
    }
    System.err.println(dateGrp.getSyntaxTree().toStringTree());
}
joestelmach commented 9 years ago

Can you elaborate on the problem you're having? This example string seems to work OK for me.

coulas commented 9 years ago

Showing dates in my first comment may help. for a bette example, I have made a test case on my github : https://github.com/coulas/natty_100.git My initial need is to have the first monday of the month in one or several month. My test case does not contains all the trys I made, but I simply could not find an expression that works as I wanted.

coulas commented 9 years ago

The output of my test case is :

System Properties :
    java.runtime.name=Java(TM) SE Runtime Environment
    sun.boot.library.path=C:\Program Files\Java\jdk1.7.0_71\jre\bin
    java.vm.version=24.71-b01
    java.vm.vendor=Oracle Corporation
    java.vendor.url=http://java.oracle.com/
    java.vm.name=Java HotSpot(TM) 64-Bit Server VM
    sun.java.launcher=SUN_STANDARD
    sun.os.patch.level=
    java.vm.specification.name=Java Virtual Machine Specification
    java.runtime.version=1.7.0_71-b14
    java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
    java.endorsed.dirs=C:\Program Files\Java\jdk1.7.0_71\jre\lib\endorsed
    java.io.tmpdir=C:\Users\Nicolas\AppData\Local\Temp\
    java.vm.specification.vendor=Oracle Corporation
    sun.jnu.encoding=Cp1252
    java.library.path=C:\Program Files\Java\jdk1.7.0_71\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\HashiCorp\Vagrant\bin;C:\Program Files\Bandizip\7z;.
    java.specification.name=Java Platform API Specification
    java.class.version=51.0
    sun.management.compiler=HotSpot 64-Bit Tiered Compilers
    java.awt.printerjob=sun.awt.windows.WPrinterJob
    java.specification.version=1.7
    java.class.path=D:\projects\OSS\natty\target\test-classes;D:\projects\OSS\natty\target\classes;C:\Users\Nicolas\.m2\repository\com\joestelmach\natty\0.9\natty-0.9.jar;C:\Users\Nicolas\.m2\repository\org\antlr\antlr-runtime\3.2\antlr-runtime-3.2.jar;C:\Users\Nicolas\.m2\repository\org\antlr\stringtemplate\3.2\stringtemplate-3.2.jar;C:\Users\Nicolas\.m2\repository\antlr\antlr\2.7.7\antlr-2.7.7.jar;C:\Users\Nicolas\.m2\repository\org\mnode\ical4j\ical4j\1.0.2\ical4j-1.0.2.jar;C:\Users\Nicolas\.m2\repository\commons-logging\commons-logging\1.1.1\commons-logging-1.1.1.jar;C:\Users\Nicolas\.m2\repository\commons-codec\commons-codec\1.5\commons-codec-1.5.jar;C:\Users\Nicolas\.m2\repository\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;C:\Users\Nicolas\.m2\repository\backport-util-concurrent\backport-util-concurrent\3.1\backport-util-concurrent-3.1.jar;C:\Users\Nicolas\.m2\repository\joda-time\joda-time\2.3\joda-time-2.3.jar;C:\Users\Nicolas\.m2\repository\pl\pragmatists\JUnitParams\1.0.4\JUnitParams-1.0.4.jar;C:\Users\Nicolas\.m2\repository\junit\junit\4.11\junit-4.11.jar;C:\Users\Nicolas\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;/C:/eclipses/eclipse-luna/configuration/org.eclipse.osgi/205/0/.cp/;/C:/eclipses/eclipse-luna/configuration/org.eclipse.osgi/204/0/.cp/
    java.vm.specification.version=1.7
    sun.java.command=org.eclipse.jdt.internal.junit.runner.RemoteTestRunner -version 3 -port 62931 -testLoaderClass org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader -loaderpluginname org.eclipse.jdt.junit4.runtime -classNames org.coulas.natty_100.NattyParserTest
    java.home=C:\Program Files\Java\jdk1.7.0_71\jre
    sun.arch.data.model=64
    java.specification.vendor=Oracle Corporation
    java.vm.info=mixed mode
    java.version=1.7.0_71
    java.ext.dirs=C:\Program Files\Java\jdk1.7.0_71\jre\lib\ext;C:\Windows\Sun\Java\lib\ext
    sun.boot.class.path=C:\Program Files\Java\jdk1.7.0_71\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_71\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_71\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.7.0_71\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_71\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_71\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_71\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_71\jre\classes
    java.vendor=Oracle Corporation
    java.vendor.url.bug=http://bugreport.sun.com/bugreport/
    sun.io.unicode.encoding=UnicodeLittle
    sun.cpu.endian=little
    sun.desktop=windows
    sun.cpu.isalist=amd64
System Environment :
first monday in 1 month expects 2015-02-02 and gives 2015-01-26
(DATE_TIME_ALTERNATIVE (DATE_TIME (RELATIVE_DATE (SEEK > by_day 1 month) (EXPLICIT_SEEK 1 (DAY_OF_WEEK 2)))))
first monday in 2 months expects 2015-03-02 and gives 2015-02-23
(DATE_TIME_ALTERNATIVE (DATE_TIME (RELATIVE_DATE (SEEK > by_day 2 month) (EXPLICIT_SEEK 1 (DAY_OF_WEEK 2)))))
first monday in 3 months expects 2015-04-06 and gives 2015-04-06
(DATE_TIME_ALTERNATIVE (DATE_TIME (RELATIVE_DATE (SEEK > by_day 3 month) (EXPLICIT_SEEK 1 (DAY_OF_WEEK 2)))))
first monday of month in 1 month expects 2015-02-02 and gives 2015-02-02
(DATE_TIME_ALTERNATIVE (DATE_TIME (RELATIVE_DATE (SEEK > by_day 0 (DAY_OF_WEEK 2)))))
first monday of month in 2 months expects 2015-03-02 and gives 2015-02-02
(DATE_TIME_ALTERNATIVE (DATE_TIME (RELATIVE_DATE (SEEK > by_day 0 (DAY_OF_WEEK 2)))))
(DATE_TIME_ALTERNATIVE (DATE_TIME (RELATIVE_DATE (SEEK > by_day 0 (DAY_OF_WEEK 2)))))
first monday of month in 3 months expects 2015-04-06 and gives 2015-02-02
first monday of 1 month expects 2015-02-02 and gives 2015-01-26
(DATE_TIME_ALTERNATIVE (DATE_TIME (RELATIVE_DATE (SEEK > by_day 1 month) (EXPLICIT_SEEK 1 (DAY_OF_WEEK 2)))))
first monday of 2 months expects 2015-03-02 and gives 2015-02-23
(DATE_TIME_ALTERNATIVE (DATE_TIME (RELATIVE_DATE (SEEK > by_day 2 month) (EXPLICIT_SEEK 1 (DAY_OF_WEEK 2)))))
first monday of 3 months expects 2015-04-06 and gives 2015-04-06
(DATE_TIME_ALTERNATIVE (DATE_TIME (RELATIVE_DATE (SEEK > by_day 3 month) (EXPLICIT_SEEK 1 (DAY_OF_WEEK 2)))))