package org.influxdb.impl;

import java.text.SimpleDateFormat;
import java.util.EnumSet;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

/**
 * Utils for time related methods.
 *
 * @author stefan.majer [at] gmail.com
 *
 */
public enum TimeUtil {
  INSTANCE;

  private static final EnumSet<TimeUnit> ALLOWED_TIMEUNITS = EnumSet.of(
      TimeUnit.HOURS,
      TimeUnit.MINUTES,
      TimeUnit.SECONDS,
      TimeUnit.MILLISECONDS,
      TimeUnit.MICROSECONDS,
      TimeUnit.NANOSECONDS);

  /**
   * Convert from a TimeUnit to a influxDB timeunit String.
   *
   * @param t
   * @return the String representation.
   */
  public static String toTimePrecision(final TimeUnit t) {
    switch (t) {
    case HOURS:
      return "h";
    case MINUTES:
      return "m";
    case SECONDS:
      return "s";
    case MILLISECONDS:
      return "ms";
    case MICROSECONDS:
      return "u";
    case NANOSECONDS:
      return "n";
    default:
      throw new IllegalArgumentException("time precision must be one of:" + ALLOWED_TIMEUNITS);
    }
  }

    /**
     * convert a unix epoch time to timestamp used by influxdb.
     * this can then be used in query expressions against influxdb's time column like so:
     * influxDB.query(new Query("SELECT * FROM some_measurement WHERE time >= '"
     *                          + toInfluxDBTimeFormat(timeStart) + "'", some_database))
     * influxdb time format example: 2016-10-31T06:52:20.020Z
     *
     * @param time timestamp to use, in unix epoch time
     * @return influxdb compatible date-tome string
     */
    public static String toInfluxDBTimeFormat(final long time) {
        SimpleDateFormat dateDF = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat timeDF = new SimpleDateFormat("HH:mm:ss.SSS");
        dateDF.setTimeZone(TimeZone.getTimeZone("UTC"));
        timeDF.setTimeZone(TimeZone.getTimeZone("UTC"));

        return dateDF.format(time) + "T" + timeDF.format(time) + "Z";
    }

    /**
     * convert an influxdb timestamp used by influxdb to unix epoch time.
     * influxdb time format example: 2016-10-31T06:52:20.020Z
     *
     * @param time timestamp to use, in influxdb datetime format
     * @return time in unix epoch time
     */
    public static long fromInfluxDBTimeFormat(final String time) {
        try {
            String[] parts = time.split("T");
            String datePart = parts[0];
            String timePart = parts[1].substring(0, parts[1].length() - 1);
            SimpleDateFormat dateDF = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat timeDF = new SimpleDateFormat("HH:mm:ss.SSS");
            dateDF.setTimeZone(TimeZone.getTimeZone("UTC"));
            timeDF.setTimeZone(TimeZone.getTimeZone("UTC"));

            return dateDF.parse(datePart).getTime() + timeDF.parse(timePart).getTime();
        } catch (Exception e) {
            throw new RuntimeException("unexpected date format", e);
        }
    }

}

@@ -259,16 +259,18 @@
 eUtil %7B%0A
+  
   INSTAN
@@ -274,16 +274,395 @@
 TANCE;%0A%0A
+    private static final ThreadLocal%3CSimpleDateFormat%3E formatter = new ThreadLocal%3CSimpleDateFormat%3E()%7B%0A        @Override%0A        protected SimpleDateFormat initialValue()%0A        %7B%0A            SimpleDateFormat dateDF = new SimpleDateFormat(%22yyyy-MM-dd'T'HH:mm:ss.SSS'Z'%22);%0A            dateDF.setTimeZone(TimeZone.getTimeZone(%22UTC%22));%0A            return dateDF;%0A        %7D%0A    %7D;%0A
   privat
@@ -2034,325 +2034,43 @@
     
-SimpleDateFormat dateDF = new SimpleDateFormat(%22yyyy-MM-dd%22);%0A        SimpleDateFormat timeDF = new SimpleDateFormat(%22HH:mm:ss.SSS%22);%0A        dateDF.setTimeZone(TimeZone.getTimeZone(%22UTC%22));%0A        timeDF.setTimeZone(TimeZone.getTimeZone(%22UTC%22));%0A%0A        return dateDF.format(time) + %22T%22 + timeDF.format(time) + %22Z%22
+return formatter.get().format(time)
 ;%0A  
@@ -2434,491 +2434,30 @@
     
-String%5B%5D parts = time.split(%22T%22);%0A            String datePart = parts%5B0%5D;%0A            String timePart = parts%5B1%5D.substring(0, parts%5B1%5D.length() - 1);%0A            SimpleDateFormat dateDF = new SimpleDateFormat(%22yyyy-MM-dd%22);%0A            SimpleDateFormat timeDF = new SimpleDateFormat(%22HH:mm:ss.SSS%22);%0A            dateDF.setTimeZone(TimeZone.getTimeZone(%22UTC%22));%0A            timeDF.setTimeZone(TimeZone.getTimeZone(%22UTC%22));%0A%0A            return dateDF.parse(datePart).getTime() + timeDF
+return formatter.get()
 .par
@@ -2467,12 +2467,8 @@
 time
-Part
 ).ge

Improve performance for parsing and formatting of timestamps while remaining thread safe