mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:28:11 +00:00
date: Allow using a custom format string
This commit adds an optional string positional argument which is used a custom format string for the date.
This commit is contained in:
parent
01dc718f07
commit
7bd68c86d3
2 changed files with 21 additions and 6 deletions
|
@ -5,7 +5,7 @@ date - print or set the system date and time
|
||||||
## Synopsis
|
## Synopsis
|
||||||
|
|
||||||
```**sh
|
```**sh
|
||||||
$ date [--set date] [--unix] [--iso-8601] [--rfc-3339] [--rfc-5322]
|
$ date [--set date] [--unix] [--iso-8601] [--rfc-3339] [--rfc-5322] [format-string]
|
||||||
```
|
```
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
@ -21,12 +21,19 @@ or print the system date and time in various formats.
|
||||||
* `-r`, `--rfc-3339`: Print date in RFC 3339 format
|
* `-r`, `--rfc-3339`: Print date in RFC 3339 format
|
||||||
* `-R`, `--rfc-5322`: Print date in RFC 5322 format
|
* `-R`, `--rfc-5322`: Print date in RFC 5322 format
|
||||||
|
|
||||||
|
## Arguments
|
||||||
|
|
||||||
|
* `format-string`: Custom format to print the date in. Must start with a '+' character.
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# Print the current date and time in ISO 8601 format
|
# Print the current date and time in ISO 8601 format
|
||||||
$ date --iso-8601
|
$ date --iso-8601
|
||||||
|
|
||||||
|
# Print the current date in a custom format
|
||||||
|
$ date +%Y-%m-%d
|
||||||
|
|
||||||
# Set date to 1610017485 (UNIX time)
|
# Set date to 1610017485 (UNIX time)
|
||||||
$ date -s 1610017485
|
$ date -s 1610017485
|
||||||
```
|
```
|
||||||
|
|
|
@ -20,6 +20,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
bool print_rfc_3339 = false;
|
bool print_rfc_3339 = false;
|
||||||
bool print_rfc_5322 = false;
|
bool print_rfc_5322 = false;
|
||||||
const char* set_date = nullptr;
|
const char* set_date = nullptr;
|
||||||
|
StringView format_string;
|
||||||
|
|
||||||
Core::ArgsParser args_parser;
|
Core::ArgsParser args_parser;
|
||||||
args_parser.add_option(set_date, "Set system date and time", "set", 's', "date");
|
args_parser.add_option(set_date, "Set system date and time", "set", 's', "date");
|
||||||
|
@ -27,6 +28,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
args_parser.add_option(print_iso_8601, "Print date in ISO 8601 format", "iso-8601", 'i');
|
args_parser.add_option(print_iso_8601, "Print date in ISO 8601 format", "iso-8601", 'i');
|
||||||
args_parser.add_option(print_rfc_3339, "Print date in RFC 3339 format", "rfc-3339", 'r');
|
args_parser.add_option(print_rfc_3339, "Print date in RFC 3339 format", "rfc-3339", 'r');
|
||||||
args_parser.add_option(print_rfc_5322, "Print date in RFC 5322 format", "rfc-5322", 'R');
|
args_parser.add_option(print_rfc_5322, "Print date in RFC 5322 format", "rfc-5322", 'R');
|
||||||
|
args_parser.add_positional_argument(format_string, "Custom format to print the date in", "format-string", Core::ArgsParser::Required::No);
|
||||||
args_parser.parse(arguments);
|
args_parser.parse(arguments);
|
||||||
|
|
||||||
if (set_date != nullptr) {
|
if (set_date != nullptr) {
|
||||||
|
@ -43,15 +45,21 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: this should be improved and will need to be cleaned up
|
if (print_unix_date + print_iso_8601 + print_rfc_3339 + print_rfc_5322 + !format_string.is_null() > 1) {
|
||||||
// when additional output formats and formatting is supported
|
warnln("date: Multiple output formats specified");
|
||||||
if (print_unix_date && print_iso_8601 && print_rfc_3339 && print_rfc_5322) {
|
|
||||||
warnln("date: multiple output formats specified");
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto date = Core::DateTime::now();
|
auto date = Core::DateTime::now();
|
||||||
if (print_unix_date) {
|
if (!format_string.is_null()) {
|
||||||
|
// FIXME: If the string argument does not start with a '+' sign, POSIX says
|
||||||
|
// we should parse that as a date, and set the system time to it.
|
||||||
|
if (format_string.length() == 0 || format_string[0] != '+') {
|
||||||
|
warnln("date: Format string must start with '+'");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
outln("{}", date.to_string(format_string.substring_view(1)));
|
||||||
|
} else if (print_unix_date) {
|
||||||
outln("{}", date.timestamp());
|
outln("{}", date.timestamp());
|
||||||
} else if (print_iso_8601) {
|
} else if (print_iso_8601) {
|
||||||
outln("{}", date.to_string("%Y-%m-%dT%H:%M:%S%:z"));
|
outln("{}", date.to_string("%Y-%m-%dT%H:%M:%S%:z"));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue