//
// DateTools.java
//

/*
LOCI Common package: utilities for I/O, reflection and miscellaneous tasks.
Copyright (C) 2005-@year@ Melissa Linkert and Curtis Rueden.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package loci.common;

import java.text.FieldPosition;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * A utility class with convenience methods for working with dates.
 *
 * <dl><dt><b>Source code:</b></dt>
 * <dd><a href="https://skyking.microscopy.wisc.edu/trac/java/browser/trunk/components/common/src/loci/common/DateTools.java">Trac</a>,
 * <a href="https://skyking.microscopy.wisc.edu/svn/java/trunk/components/common/src/loci/common/DateTools.java">SVN</a></dd></dl>
 *
 * @author Curtis Rueden ctrueden at wisc.edu
 * @author Chris Allan callan at blackcat.ca
 * @author Melissa Linkert linkert at wisc.edu
 */
public final class DateTools {

  // -- Constants --

  /** Timestamp formats. */
  public static final int UNIX = 0;  // January 1, 1970
  public static final int COBOL = 1;  // January 1, 1601
  public static final int MICROSOFT = 2; // December 30, 1899
  public static final int ZVI = 3;

  /** Milliseconds until UNIX epoch. */
  public static final long UNIX_EPOCH = 0;
  public static final long COBOL_EPOCH = 11644473600000L;
  public static final long MICROSOFT_EPOCH = 2209143600000L;
  public static final long ZVI_EPOCH = 2921084975759000L;

  /** ISO 8601 date format string. */
  public static final String ISO8601_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";

  // -- Constructor --

  private DateTools() { }

  // -- Date handling --

  /** Converts the given timestamp into an ISO 8601 date. */
  public static String convertDate(long stamp, int format) {
    // see http://www.merlyn.demon.co.uk/critdate.htm for more information on
    // dates than you will ever need (or want)

    long ms = stamp;

    switch (format) {
      case UNIX:
        ms -= UNIX_EPOCH;
        break;
      case COBOL:
        ms -= COBOL_EPOCH;
        break;
      case MICROSOFT:
        ms -= MICROSOFT_EPOCH;
        break;
      case ZVI:
        ms -= ZVI_EPOCH;
        break;
    }

    SimpleDateFormat fmt = new SimpleDateFormat(ISO8601_FORMAT);
    StringBuffer sb = new StringBuffer();

    Date d = new Date(ms);

    fmt.format(d, sb, new FieldPosition(0));
    return sb.toString();
  }

  /**
   * Formats the given date as an ISO 8601 date.
   *
   * @param date The date to format as ISO 8601.
   * @param format The date's input format.
   */
  public static String formatDate(String date, String format) {
    if (date == null) return null;
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    sdf.setLenient(false);
    Date d = sdf.parse(date, new ParsePosition(0));
    if (d == null) return null;
    sdf = new SimpleDateFormat(ISO8601_FORMAT);
    return sdf.format(d);
  }

  /**
   * Formats the given date as an ISO 8601 date.
   *
   * @param date The date to format as ISO 8601.
   * @param formats The date's possible input formats.
   */
  public static String formatDate(String date, String[] formats) {
    for (int i=0; i<formats.length; i++) {
      String result = formatDate(date, formats[i]);
      if (result != null) return result;
    }
    return null;
  }

  /**
   * Converts a string date in the given format to a long timestamp
   * (in Unix format: milliseconds since January 1, 1970).
   */
  public static long getTime(String date, String format) {
    SimpleDateFormat f = new SimpleDateFormat(format);
    Date d = f.parse(date, new ParsePosition(0));
    if (d == null) return -1;
    return d.getTime();
  }

}

@@ -2244,24 +2244,312 @@
 andling --%0A%0A
+  /**%0A   * Converts from two-word tick representation to milliseconds.%0A   * Mainly useful in conjunction with COBOL date conversion.%0A   */%0A  public static long getMillisFromTicks(long hi, long lo) %7B%0A    long ticks = ((hi %3C%3C 32) %7C lo);%0A    return ticks / 10000; // 100 ns = 0.0001 ms%0A  %7D%0A%0A
   /** Conver

Add convenience method for working with split dates in ticks (COBOL).