2222 */
2323public class UptimeCommand implements Command {
2424 @ Override
25- public void execute (String [] args , ShellContext context ) {
26- long uptimeMillis = System .currentTimeMillis () - context .getStartTime ();
25+ public void execute (String [] args , ShellContext context ) throws IOException {
26+ Instant startTime = context .getStartTime ();
27+ Instant now = Instant .now ();
2728
28- long totalSeconds = uptimeMillis / 1000 ;
29- long hours = totalSeconds / 3600 ;
30- long minutes = (totalSeconds % 3600 ) / 60 ;
31- long seconds = totalSeconds % 60 ;
29+ // Calculate duration between start time and now
30+ Duration uptime = Duration .between (startTime , now );
3231
33- System .out .printf ("Up since %dh %dm %ds%n" , hours , minutes , seconds );
32+ long seconds = uptime .getSeconds ();
33+ long days = seconds / 86400 ;
34+ long hours = (seconds % 86400 ) / 3600 ;
35+ long minutes = (seconds % 3600 ) / 60 ;
36+ long secs = seconds % 60 ;
37+
38+ System .out .print ("Uptime: " );
39+ if (days > 0 ) {
40+ System .out .print (days + " day" + (days != 1 ? "s" : "" ) + ", " );
41+ }
42+ System .out .printf ("%02d:%02d:%02d%n" , hours , minutes , secs );
3443 }
3544
3645 @ Override
3746 public String description () {
38- return "Display how long the shell has been running since startup ." ;
47+ return "Display how long the shell has been running." ;
3948 }
4049
4150 @ Override
4251 public String usage () {
4352 return "uptime" ;
4453 }
45- }
54+ }
0 commit comments