uptime命令

This commit is contained in:
2026-02-02 05:22:25 +08:00
parent 73f49c9dbc
commit db513a65a3
2 changed files with 53 additions and 1 deletions

View File

@@ -24,6 +24,9 @@ namespace CMLeonOS
// 修复模式变量(硬编码,用于控制是否启用修复模式) // 修复模式变量(硬编码,用于控制是否启用修复模式)
public static bool FixMode = false; public static bool FixMode = false;
// 系统启动时间用于uptime命令
public static DateTime SystemStartTime;
protected override void BeforeRun() protected override void BeforeRun()
{ {
// Console.Clear(); // Console.Clear();
@@ -38,6 +41,10 @@ namespace CMLeonOS
Console.WriteLine("CMLeonOS Project"); Console.WriteLine("CMLeonOS Project");
Console.WriteLine("By LeonOS 2 Developement Team"); Console.WriteLine("By LeonOS 2 Developement Team");
// 记录系统启动时间用于uptime命令
SystemStartTime = DateTime.Now;
Console.WriteLine($"System started at: {SystemStartTime.ToString("yyyy-MM-dd HH:mm:ss")}");
// 注册VFS // 注册VFS
try try
{ {

View File

@@ -176,9 +176,11 @@ namespace CMLeonOS
" env change <varname> <value> - Set variable value", " env change <varname> <value> - Set variable value",
" env delete <varname> - Delete variable", " env delete <varname> - Delete variable",
" beep - Play beep sound", " beep - Play beep sound",
" uptime - Show system uptime",
" branswe <filename> - Execute Branswe code file", " branswe <filename> - Execute Branswe code file",
" backup <name> - Backup system files", " backup <name> - Backup system files",
" restore <name> - Restore system files", " restore <name> - Restore system files",
" grep <pattern> <file> - Search text in file",
" version - Show OS version", " version - Show OS version",
" about - Show about information", " about - Show about information",
" help <page> - Show help page (1-3)", " help <page> - Show help page (1-3)",
@@ -412,6 +414,9 @@ namespace CMLeonOS
case "branswe": case "branswe":
ProcessBransweCommand(args); ProcessBransweCommand(args);
break; break;
case "uptime":
ShowUptime();
break;
case "grep": case "grep":
GrepFile(args); GrepFile(args);
break; break;
@@ -1444,5 +1449,45 @@ namespace CMLeonOS
ShowError($"Exception type: {ex.GetType().Name}"); ShowError($"Exception type: {ex.GetType().Name}");
} }
} }
private void ShowUptime()
{
try
{
Console.WriteLine("====================================");
Console.WriteLine(" System Uptime");
Console.WriteLine("====================================");
Console.WriteLine();
// 计算系统运行时间
if (Kernel.SystemStartTime != DateTime.MinValue)
{
TimeSpan uptime = DateTime.Now - Kernel.SystemStartTime;
Console.WriteLine("System started: " + Kernel.SystemStartTime.ToString("yyyy-MM-dd HH:mm:ss"));
Console.WriteLine("Current time: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
Console.WriteLine();
// 格式化运行时间
int days = uptime.Days;
int hours = uptime.Hours;
int minutes = uptime.Minutes;
int seconds = uptime.Seconds;
Console.WriteLine($"System uptime: {days} days, {hours} hours, {minutes} minutes, {seconds} seconds");
Console.WriteLine($"Total uptime: {uptime.TotalHours:F2} hours");
}
else
{
ShowWarning("System start time not available.");
ShowWarning("System may have been started before uptime tracking was implemented.");
}
Console.WriteLine();
}
catch (Exception ex)
{
ShowError($"Error showing uptime: {ex.Message}");
}
}
} }
} }