From db513a65a38228dfda9a0e9e4dd080c60c92df1f Mon Sep 17 00:00:00 2001 From: Leonmmcoset Date: Mon, 2 Feb 2026 05:22:25 +0800 Subject: [PATCH] =?UTF-8?q?uptime=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Kernel.cs | 9 ++++++++- Shell.cs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/Kernel.cs b/Kernel.cs index 52eea51..830e604 100644 --- a/Kernel.cs +++ b/Kernel.cs @@ -23,7 +23,10 @@ namespace CMLeonOS // 修复模式变量(硬编码,用于控制是否启用修复模式) public static bool FixMode = false; - + + // 系统启动时间(用于uptime命令) + public static DateTime SystemStartTime; + protected override void BeforeRun() { // Console.Clear(); @@ -38,6 +41,10 @@ namespace CMLeonOS Console.WriteLine("CMLeonOS Project"); 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 try { diff --git a/Shell.cs b/Shell.cs index e094506..ef417f5 100644 --- a/Shell.cs +++ b/Shell.cs @@ -176,9 +176,11 @@ namespace CMLeonOS " env change - Set variable value", " env delete - Delete variable", " beep - Play beep sound", + " uptime - Show system uptime", " branswe - Execute Branswe code file", " backup - Backup system files", " restore - Restore system files", + " grep - Search text in file", " version - Show OS version", " about - Show about information", " help - Show help page (1-3)", @@ -412,6 +414,9 @@ namespace CMLeonOS case "branswe": ProcessBransweCommand(args); break; + case "uptime": + ShowUptime(); + break; case "grep": GrepFile(args); break; @@ -1444,5 +1449,45 @@ namespace CMLeonOS 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}"); + } + } } } \ No newline at end of file