package nxm.sys.intr;

import nxm.sys.lib.Data;
import nxm.sys.lib.Intrinsic;

/**
  Implements various timer functions

  @author Jeff Schoen
*/
public class Timer extends Intrinsic {

  String func,label;

  /** default constructor */
  public Timer () {}

  public int process() {

    // Get parameters
    func  = MA.getU("FUNC");
    label = MA.getU("LABEL");

    if (func.equals("ON"))
      M.timer = System.currentTimeMillis();
    else if (func.equals("OFF"))
      M.timer = 0;
    else if (func.equals("SET"))
      M.elapse = System.currentTimeMillis();
    else if (func.startsWith("ELAPSE")) {
      double time = 1.e-3 * (double)(System.currentTimeMillis()-M.elapse);
      if (label.length()>0) M.results.put(label,new Data(time));
      else M.println("Elapsed time = "+time+" seconds");  // since 3.7.1 using M.println instead of M.terminal.writeln to allow logging - NXM-1813
    }
    else M.warning("Bad argument to timer: "+func);

    return (FINISH);
  }

}
