/* JTimerCommand.java: a Tcl command for timing things.
 *
 * Copyright (c) 1998 The Regents of the University of California.
 *       All Rights Reserved.  See the COPYRIGHT file for details.
 */

package tutorial.tcltk98;
import tcl.lang.*;
import java.util.*;

public class JTimerCommand implements Command {
  long startTime = 0;
  public void cmdProc(Interp interp, TclObject argv[])
    throws TclException {
    if ( argv.length != 2 ) {
      throw new TclNumArgsException(interp, 0, argv, 
				    "jtimer \"start\" | \"stop\"");
    }
    String mode = argv[1].toString();
    if (mode.equals("start")) {
      GregorianCalendar cal = new GregorianCalendar();
      startTime = cal.getTime().getTime();
    } else if (mode.equals("stop")) {
      GregorianCalendar cal = new GregorianCalendar();
      long elapsedTime = cal.getTime().getTime() - startTime;
      interp.setResult(Long.toString(elapsedTime));
    } else {
      throw new TclException(interp, "Expected \"start\" or \"stop\"");
    }
  }
}
