package net.hyperic.sigar.cmd;

import net.hyperic.sigar.Sigar;
import net.hyperic.sigar.SigarException;
import net.hyperic.sigar.ThreadCpu;

public class Time extends SigarCommandBase {

    public Time(Shell shell) {
        super(shell);
    }

    public Time() {
        super();
    }

    protected boolean validateArgs(String[] args) {
        return args.length >= 1;
    }

    public String getSyntaxArgs() {
        return "[command] [...]";
    }

    public String getUsageShort() {
        return "Time command";
    }

    public void output(String[] args) throws SigarException {
        ThreadCpu cpu = new ThreadCpu();
        cpu.gather(this.sigar, 0);
        long user = cpu.getUser();
        long sys = cpu.getSys();
        long start = System.currentTimeMillis();
        boolean isInteractive = this.shell.isInteractive();
        //turn off paging.
        this.shell.setInteractive(false);

        try {
            this.shell.handleCommand("time " + args[0], args);
        } finally {
            this.shell.setInteractive(isInteractive);
        }

        cpu.gather(this.sigar, 0);
        println("real....." +
                format((System.currentTimeMillis() - start)));
        println("user....." + format(toMillis(cpu.getUser() - user)));
        println("sys......" + format(toMillis(cpu.getSys() - sys)));
    }

    private static long toMillis(long nano) {
        return nano / 1000000;
    }

    private static String format(long elap) {
        String fraction = (elap % 1000) + "";
        int pad = 3 - fraction.length();

        StringBuffer buf = new StringBuffer()
            .append(elap / 1000).append('.');

        //for example, 15 millseconds formatted as ".015" rather than ".15"
        while (pad-- > 0) {
            buf.append("0");
        }
        buf.append(fraction).append(" seconds");
        return buf.toString();
    }

    public static void main(String[] args) throws Exception {
        new Time().processCommand(args);
    }
}

@@ -123,25 +123,24 @@
 c.sigar.
-ThreadCpu
+CpuTimer
 ;%0A%0Apubli
@@ -593,201 +593,8 @@
 n %7B%0A
-        ThreadCpu cpu = new ThreadCpu();%0A        cpu.gather(this.sigar, 0);%0A        long user = cpu.getUser();%0A        long sys = cpu.getSys();%0A        long start = System.currentTimeMillis();%0A
     
@@ -717,16 +717,86 @@
 (false);
+%0A        CpuTimer cpu = new CpuTimer(this.sigar);%0A        cpu.start();
 %0A%0A      
@@ -967,802 +967,41 @@
 cpu.
-gather(this.sigar, 0);%0A        println(%22real.....%22 +%0A                format((System.currentTimeMillis() - start)));%0A        println(%22user.....%22 + format(toMillis(cpu.getUser() - user)));%0A        println(%22sys......%22 + format(toMillis(cpu.getSys() - sys)));%0A    %7D%0A%0A    private static long toMillis(long nano) %7B%0A        return nano / 1000000;%0A    %7D%0A%0A    private static String format(long elap) %7B%0A        String fraction = (elap %25 1000) + %22%22;%0A        int pad = 3 - fraction.length();%0A%0A        StringBuffer buf = new StringBuffer()%0A            .append(elap / 1000).append('.');%0A%0A        //for example, 15 millseconds formatted as %22.015%22 rather than %22.15%22%0A        while (pad-- %3E 0) %7B%0A            buf.append(%220%22);%0A        %7D%0A        buf.append(fraction).append(%22 seconds%22);%0A        return buf.toString(
+stop();%0A        cpu.list(this.out
 );%0A 

use CpuTimer