diff --git a/Base/usr/share/man/man1/uptime.md b/Base/usr/share/man/man1/uptime.md index b3b61cc0a6..9c038e04ca 100644 --- a/Base/usr/share/man/man1/uptime.md +++ b/Base/usr/share/man/man1/uptime.md @@ -16,6 +16,7 @@ This information includes when the system came online and how long it has been u ## Options * `-p`, `--pretty`: Output only the uptime, in human-readable format. +* `-s`, `--since`: Output only the datetime when the system came online. ## Examples @@ -28,3 +29,8 @@ $ uptime $ uptime -p Up 2 minutes, 20 seconds ``` + +```sh +$ uptime -s +2024-01-24 06:23:27 +``` diff --git a/Userland/Utilities/uptime.cpp b/Userland/Utilities/uptime.cpp index ad3c318590..0f92842bea 100644 --- a/Userland/Utilities/uptime.cpp +++ b/Userland/Utilities/uptime.cpp @@ -18,9 +18,11 @@ ErrorOr serenity_main(Main::Arguments arguments) TRY(Core::System::pledge("stdio rpath")); bool pretty_output = false; + bool output_since = false; Core::ArgsParser args_parser; args_parser.add_option(pretty_output, "Output only the uptime, in human-readable format", "pretty", 'p'); + args_parser.add_option(output_since, "Show when the system is up since, in yyyy-mm-dd HH:MM:SS format", "since", 's'); args_parser.parse(arguments); auto file = TRY(Core::File::open("/sys/kernel/uptime"sv, Core::File::OpenMode::Read)); @@ -34,7 +36,11 @@ ErrorOr serenity_main(Main::Arguments arguments) return Error::from_string_literal("Couldn't convert to number"); auto seconds = maybe_seconds.release_value(); - if (pretty_output) { + if (output_since) { + auto since_timestamp = Core::DateTime::now().timestamp() - seconds; + auto since_time = TRY(Core::DateTime::from_timestamp(since_timestamp).to_string()); + outln("{}", since_time); + } else if (pretty_output) { outln("Up {}", human_readable_time(seconds)); } else { auto current_time = TRY(Core::DateTime::now().to_string());