Dates.java [src/csip/utils] Revision:   Date:
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API and application suite.
 *
 * 2012-2022, Olaf David and others, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package csip.utils;

import csip.Config;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.format.DateTimeParseException;
import java.util.*;

/**
 * All kind of stuff
 *
 * @author od
 */
public class Dates {

  static final long NUM_100NS_INTERVALS_SINCE_UUID_EPOCH = 0x01b21dd213814000L;


  public static long diffInMillis(Date start, Date end) {
    return end.getTime() - start.getTime();
  }


  public static DateFormat newISOFormat() {
//        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
    return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  }


  public static Date parse(String iso) throws ParseException {
    return newISOFormat().parse(iso);
  }


  public static long getDurationSec(String key, long def) {
    String dur = Config.getString(key, Long.toString(def));
    try {
      return Duration.parse(dur).getSeconds();
    } catch (DateTimeParseException E) {
      return Config.getLong(key, def);
    }
  }


  public static long getDurationSec(String key) {
    String dur = Config.getString(key);
    try {
      return Duration.parse(dur).getSeconds();
    } catch (DateTimeParseException E) {
      return Config.getLong(key);
    }
  }


  public static String duration(Date start, Date end) {
    long millis = diffInMillis(start, end);
    return duration(millis);
  }


  public static String duration(long millis) {
    int h = (int) ((millis / 1000) / 3600);
    int m = (int) (((millis / 1000) / 60) % 60);
    int s = (int) ((millis / 1000) % 60);
    return String.format("%1$02d:%2$02d:%3$02d", h, m, s);
  }


  public static DateFormat newISOFormat(String tz) {
    DateFormat df = newISOFormat();
    df.setTimeZone(TimeZone.getTimeZone(tz));
    return df;
  }


  /**
   * Get the current date/time in ISO.
   *
   * @return
   */
  public static String nowISO() {
    return newISOFormat().format(new Date());
  }


  public static String nowISO(String tz) {
    DateFormat f = newISOFormat(tz);
    return f.format(new Date());
  }


  /**
   * get the future date for 'now' + $sec seconds
   *
   * @param sec
   * @return
   */
  public static Date futureDate(long sec) {
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(new Date());
    cal.add(Calendar.SECOND, (int) sec);
    return cal.getTime();
  }


  public static Date futureDate(Date from, long sec) {
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(from);
    cal.add(Calendar.SECOND, (int) sec);
    return cal.getTime();
  }


  /**
   * Get the long time for the uuid.
   *
   * @param uuid
   * @return
   */
  public static long getTimeFromUUID(UUID uuid) {
    return (uuid.timestamp() - NUM_100NS_INTERVALS_SINCE_UUID_EPOCH) / 10000;
  }

}