forked from Drive-for-Java/MyCMD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUptimeCommand.java
More file actions
45 lines (39 loc) · 1.32 KB
/
Copy pathUptimeCommand.java
File metadata and controls
45 lines (39 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.mycmd.commands;
import com.mycmd.Command;
import com.mycmd.ShellContext;
/**
* Displays how long the shell has been running since startup.
*
* This command shows the uptime of the MyCMD shell session, displaying
* the time elapsed since the shell was started. The uptime is calculated
* from the shell start time stored in the ShellContext.
*
* Usage:
* - uptime : Display shell uptime in hours, minutes, and seconds
*
* The output format shows the uptime as "Up since Xh Ym Zs" where:
* - X is hours
* - Y is minutes
* - Z is seconds
*
* This is useful for monitoring how long a shell session has been active.
*/
public class UptimeCommand implements Command {
@Override
public void execute(String[] args, ShellContext context) {
long uptimeMillis = System.currentTimeMillis() - context.getStartTime();
long totalSeconds = uptimeMillis / 1000;
long hours = totalSeconds / 3600;
long minutes = (totalSeconds % 3600) / 60;
long seconds = totalSeconds % 60;
System.out.printf("Up since %dh %dm %ds%n", hours, minutes, seconds);
}
@Override
public String description() {
return "Display how long the shell has been running since startup.";
}
@Override
public String usage() {
return "uptime";
}
}