diff --git a/README.md b/README.md
index f52a9b6..88ea876 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,9 @@
# Mini-Parsers - An API for parsing discrete types
+[![Maven Central](https://img.shields.io/maven-central/v/io.heretical/mini-parsers-core)](https://search.maven.org/search?q=g:io.heretical+mini-parsers)
+[![javadoc](https://javadoc.io/badge2/io.heretical/mini-parsers-core/javadoc.svg?label=javadoc+mini-parsers-core)](https://javadoc.io/doc/io.heretical/mini-parsers-core)
+[![javadoc](https://javadoc.io/badge2/io.heretical/mini-parsers-temporal/javadoc.svg?label=javadoc+mini-parsers-temporal)](https://javadoc.io/doc/io.heretical/mini-parsers-temporal)
+
## Overview
Mini-Parsers is a Java API for parsing short discrete text strings into native types where a single type may have
@@ -11,29 +15,22 @@ validation, or normalizing data in a column during data cleansing and ETL.
For example, the same instant in time (`java.time.Instant`) may have multiple formats. The two strings `1423526400000`
and `2015-02-10T02:04:30+00:00` are equivalent if the first is interpreted as the milliseconds since the epoch.
-Only absolute and duration text representation disambiguation is currently supported. But with plans for handling more
-complex relative temporal representations like `10 days ago` or `last tuesday`. Also support for common units of measure
-are being considered.
+Absolute, duration, and relative adjuster text representation disambiguation is currently supported.
+
+But with plans for handling more complex relative temporal representations like `10 days ago` or `last tuesday`. Also
+support for common units of measure are being considered.
Final Releases are available on Maven Central:
```gradle
-implementation 'io.heretical:mini-parsers-core:1.1.0'
-implementation 'io.heretical:mini-parsers-temporal:1.1.0'
+implementation 'io.heretical:mini-parsers-temporal:2.0.0'
```
```xml
-
+ * Where common formats supported look like: + *
+ * 02/10/15 02:04 + * February/10/15 02:04 + * 02/10/15 02:04AM + * February 10th 2015, 02 + * November 09th 2018, 20:00:00 + * 02/10/15 02 + * 20150210 + * 20150210T020430Z + * 2015-02-10T02:04:30+00:00 + * 2015-02-10 02:04:30+00:00 + **/ public class AbsoluteDateTimeParser extends DateTimeParser { + /** + * Creates a new AbsoluteDateTimeParser instance. + */ + public AbsoluteDateTimeParser() + { + } + + /** + * Creates a new AbsoluteDateTimeParser instance. + * + * @param context of type Context + */ public AbsoluteDateTimeParser( Context context ) { super( context ); diff --git a/mini-parsers-temporal/src/main/java/heretical/parser/temporal/BaseTemporalExpressionParser.java b/mini-parsers-temporal/src/main/java/heretical/parser/temporal/BaseTemporalExpressionParser.java index bb788b1..0e5217a 100644 --- a/mini-parsers-temporal/src/main/java/heretical/parser/temporal/BaseTemporalExpressionParser.java +++ b/mini-parsers-temporal/src/main/java/heretical/parser/temporal/BaseTemporalExpressionParser.java @@ -27,9 +27,15 @@ public abstract class BaseTemporalExpressionParser
+ * This grammar can distinguish between either ISO-8601 duration strings, like `PT20.345S`, or simplified + * natural language duration strings, like `10 days` or `15min`, and resolve them into `java.time.Duration` instances. + *
+ * See {@link ISODurationParser} or {@link NaturalDurationParser} for more specific parsers. + *
* This class is not thread-safe.
*/
public class DurationParser extends BaseTemporalExpressionParser
+ * Where a natural duration format would look like the following:
*
+ *
+ *
+ * Where `number` is any natural integer (with commas).
+ *
+ * And `unit` is either the Unit name or Abbreviation, case-insensitive:
+ *
+ *
+ * This class is not thread-safe.
*/
public class NaturalDurationParser extends DurationParser
{
+ public NaturalDurationParser()
+ {
+ }
+
public NaturalDurationParser( Context context )
{
super( context );
diff --git a/mini-parsers-temporal/src/main/java/heretical/parser/temporal/RelativeDateTimeAdjusterParser.java b/mini-parsers-temporal/src/main/java/heretical/parser/temporal/RelativeDateTimeAdjusterParser.java
new file mode 100644
index 0000000..c330ade
--- /dev/null
+++ b/mini-parsers-temporal/src/main/java/heretical/parser/temporal/RelativeDateTimeAdjusterParser.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2018 Chris K Wensel
+ * The syntax is adopted from Splunk Docs.
+ *
+ * Where a time adjuster format would look like the following:
+ *
+ *
+ * This class is not thread-safe.
+ */
+public class RelativeDateTimeAdjusterParser extends BaseTemporalExpressionParser
+ * http://docs.splunk.com/Documentation/SplunkCloud/7.0.3/SearchReference/SearchTimeModifiers
+ * http://docs.splunk.com/Documentation/Splunk/7.1.3/Search/Specifytimemodifiersinyoursearch
+ */
+public class AdjusterExp extends DateTimeExp
+ {
+ final Locale locale = Locale.US; // controls which day is the first day of the week
+ final TemporalField weekField = WeekFields.of( locale ).dayOfWeek();
+
+ BinaryOp amountOp = BinaryOp.MINUS; // querying towards the past is assumed
+ int amount = 1; // 1 is assumed
+ TimeUnit amountUnit;
+
+ TimeUnit snapUnit;
+ int snapOrdinal = 0;
+
+ BinaryOp offsetOp;
+ int offset = 0;
+ TimeUnit offsetUnit;
+
+ public AdjusterExp()
+ {
+ }
+
+ public BinaryOp getAmountOp()
+ {
+ return amountOp;
+ }
+
+ public boolean setAmountOp( String amountOp )
+ {
+ return setAmountOp( BinaryOp.lookup( amountOp ) );
+ }
+
+ public boolean setAmountOp( BinaryOp amountOp )
+ {
+ if( amountOp != null )
+ {
+ this.amountOp = amountOp;
+ }
+
+ return true;
+ }
+
+ public int getAmount()
+ {
+ return amount;
+ }
+
+ public boolean setAmount( String amount )
+ {
+ return setAmount( Integer.parseInt( amount ) );
+ }
+
+ public boolean setAmount( int amount )
+ {
+ this.amount = amount;
+
+ return true;
+ }
+
+ public TimeUnit getAmountUnit()
+ {
+ return amountUnit;
+ }
+
+ public boolean setAmountUnit( TimeUnit amountUnit )
+ {
+ this.amountUnit = amountUnit;
+
+ return true;
+ }
+
+ public TimeUnit getSnapUnit()
+ {
+ return snapUnit;
+ }
+
+ public boolean setSnapUnit( TimeUnit snapUnit )
+ {
+ this.snapUnit = snapUnit;
+
+ return true;
+ }
+
+ public int getSnapOrdinal()
+ {
+ return snapOrdinal;
+ }
+
+ public boolean setSnapOrdinal( String ordinal )
+ {
+ return setSnapOrdinal( Integer.parseInt( ordinal ) );
+ }
+
+ public boolean setSnapOrdinal( int snapOrdinal )
+ {
+ this.snapOrdinal = snapOrdinal;
+
+ return true;
+ }
+
+ public BinaryOp getOffsetOp()
+ {
+ return offsetOp;
+ }
+
+ public boolean setOffsetOp( String offsetOp )
+ {
+ return setOffsetOp( BinaryOp.lookup( offsetOp ) );
+ }
+
+ public boolean setOffsetOp( BinaryOp offsetOp )
+ {
+ if( offsetOp != null )
+ this.offsetOp = offsetOp;
+
+ return true;
+ }
+
+ public int getOffset()
+ {
+ return offset;
+ }
+
+ public boolean setOffset( String offset )
+ {
+ return setOffset( Integer.parseInt( offset ) );
+ }
+
+ public boolean setOffset( int offset )
+ {
+ this.offset = offset;
+
+ return true;
+ }
+
+ public TimeUnit getOffsetUnit()
+ {
+ return offsetUnit;
+ }
+
+ public boolean setOffsetUnit( TimeUnit offsetUnit )
+ {
+ this.offsetUnit = offsetUnit;
+
+ return true;
+ }
+
+ public Instant toInstant( Context context )
+ {
+
+ LocalDateTime dateTime = LocalDateTime.now( context.getClock() );
+
+ if( amountUnit != null )
+ dateTime = applyBinaryOp( dateTime, amountOp, amount, amountUnit );
+
+ if( snapUnit != null )
+ {
+ Supplier
+ * PT20.345S
+ * +PT20.345S
+ * -PT20.345S // negated duration
+ *
+ * This class is not thread-safe.
*/
public class ISODurationParser extends DurationParser
{
+ /**
+ * Creates a new ISODurationParser instance.
+ */
+ public ISODurationParser()
+ {
+ }
+
+ /**
+ * Creates a new ISODurationParser instance.
+ *
+ * @param context of type Context
+ */
public ISODurationParser( Context context )
{
super( context );
diff --git a/mini-parsers-temporal/src/main/java/heretical/parser/temporal/NaturalDurationParser.java b/mini-parsers-temporal/src/main/java/heretical/parser/temporal/NaturalDurationParser.java
index 865fa25..b51bdcc 100644
--- a/mini-parsers-temporal/src/main/java/heretical/parser/temporal/NaturalDurationParser.java
+++ b/mini-parsers-temporal/src/main/java/heretical/parser/temporal/NaturalDurationParser.java
@@ -12,10 +12,46 @@
import org.parboiled.Rule;
/**
+ * The NaturalDurationParser class parses strings that represent durations a more natural language.
+ *
+ * 90 seconds
+ * 10,000 seconds
+ * 3 months
+ *
+ * A natural duration string is of the format:
+ *
+ * number[:space:]unit
+ *
>
+ *
+ * | Unit | Abbreviation | Example |
+ * |--------------|--------------|-----------|
+ * | Milliseconds | ms | 300ms |
+ * | Seconds | s, sec | 30s 30sec |
+ * | Minutes | m, min | 20m 20min |
+ * | Hours | h, hrs | 3h 3hrs |
+ * | Days | d, days | 5d 5 days |
+ * | Weeks | w, wks | 2w 2wks |
+ * | Months | mos | 3mos |
+ * | Years | y, yrs | 2y 2rs |
+ *
+ *
+ * -1min
+ * 10days
+ * 1y
+ *
+ *