/**
 * @license
 * Copyright 2017 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.lib.json;

import foam.lib.parse.*;

public class DateParser extends ProxyParser {
  public DateParser() {
    super(new Seq(
                  new Literal("\""),
                  new IntParser(),
                  new Literal("-"),
                  new IntParser(),
                  new Literal("-"),
                  new IntParser(),
                  new Literal("T"),
                  new IntParser(),
                  new Literal(":"),
                  new IntParser(),
                  new Literal(":"),
                  new IntParser(),
                  new Literal("."),
                  new Repeat(new Chars("0123456789")),
                  new Literal("Z"),
                  new Literal("\"")));
  }

  public PStream parse(PStream ps, ParserContext x) {
    System.out.println("Parsing date");
    ps = super.parse(ps, x);

    if ( ps == null ) return null;

    System.out.println("Succcess.");

    Object[] result = (Object[])ps.value();

    // TODO: Handle sub-milisecond accuracy, either with java 8 java.time pacakge or some custom type
    // to support java 7

    java.util.Calendar c = new java.util.GregorianCalendar(java.util.TimeZone.getTimeZone("UTC"));
    c.clear();
    c.set(
          (Integer)result[1],
          (Integer)result[3] - 1, // Java calendar uses zero-indexed months
          (Integer)result[5],
          (Integer)result[7],
          (Integer)result[9],
          (Integer)result[11]);

    // TODO: There has to be a better way to do this.
    StringBuilder nanoseconds = new StringBuilder();
    nanoseconds.append("0.");
    Object[] nanos = (Object[])result[13];
    for ( int i = 0 ; i < nanos.length ; i++ ) {
      nanoseconds.append(String.valueOf(nanos[i]));
    }

    c.add(c.MILLISECOND, (int)(Float.parseFloat(nanoseconds.toString()) * 1000));

    return ps.setValue(c.getTime());
  }
}

@@ -1084,16 +1084,17 @@
 bject%5B%5D)
+ 
 ps.value
@@ -1125,16 +1125,17 @@
  sub-mil
+l
 isecond 
@@ -1180,10 +1180,10 @@
  pac
-a
 k
+a
 ge o
@@ -1363,32 +1363,33 @@
        (Integer)
+ 
 result%5B1%5D,%0A     
@@ -1398,24 +1398,25 @@
    (Integer)
+ 
 result%5B3%5D - 
@@ -1475,24 +1475,25 @@
    (Integer)
+ 
 result%5B5%5D,%0A 
@@ -1506,24 +1506,25 @@
    (Integer)
+ 
 result%5B7%5D,%0A 
@@ -1537,24 +1537,25 @@
    (Integer)
+ 
 result%5B9%5D,%0A 
@@ -1572,16 +1572,17 @@
 Integer)
+ 
 result%5B1

Fix formatting and typos.