Skip to content

Commit

Permalink
uptime: Add -s/--since option to output only when the system came online
Browse files Browse the repository at this point in the history
This also matches Linux and the BSDs.
  • Loading branch information
AtkinsSJ authored and awesomekling committed Jan 25, 2024
1 parent 8faeb13 commit 7bcb560
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Base/usr/share/man/man1/uptime.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -28,3 +29,8 @@ $ uptime
$ uptime -p
Up 2 minutes, 20 seconds
```

```sh
$ uptime -s
2024-01-24 06:23:27
```
8 changes: 7 additions & 1 deletion Userland/Utilities/uptime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ ErrorOr<int> 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));
Expand All @@ -34,7 +36,11 @@ ErrorOr<int> 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());
Expand Down

0 comments on commit 7bcb560

Please sign in to comment.