2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2021-01-07 19:47:16 +03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-02-11 22:05:08 +03:00
|
|
|
#include <LibCore/DateTime.h>
|
2021-11-28 00:12:29 +03:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2019-06-07 12:49:31 +03:00
|
|
|
#include <time.h>
|
2018-10-25 18:29:49 +03:00
|
|
|
|
2021-11-28 00:12:29 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2018-10-25 18:29:49 +03:00
|
|
|
{
|
2022-04-04 02:16:06 +03:00
|
|
|
TRY(Core::System::pledge("stdio settime rpath"));
|
2020-02-18 15:20:52 +03:00
|
|
|
|
2021-01-07 19:47:16 +03:00
|
|
|
bool print_unix_date = false;
|
|
|
|
bool print_iso_8601 = false;
|
|
|
|
bool print_rfc_3339 = false;
|
|
|
|
bool print_rfc_5322 = false;
|
2023-02-28 23:41:43 +03:00
|
|
|
StringView set_date;
|
2022-02-25 23:40:28 +03:00
|
|
|
StringView format_string;
|
2021-01-07 19:47:16 +03:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.add_option(set_date, "Set system date and time", "set", 's', "date");
|
|
|
|
args_parser.add_option(print_unix_date, "Print date as Unix timestamp", "unix", 'u');
|
|
|
|
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_5322, "Print date in RFC 5322 format", "rfc-5322", 'R');
|
2022-02-25 23:40:28 +03:00
|
|
|
args_parser.add_positional_argument(format_string, "Custom format to print the date in", "format-string", Core::ArgsParser::Required::No);
|
2021-11-28 00:12:29 +03:00
|
|
|
args_parser.parse(arguments);
|
2021-01-07 19:47:16 +03:00
|
|
|
|
2023-02-28 23:41:43 +03:00
|
|
|
if (!set_date.is_empty()) {
|
|
|
|
auto number = set_date.to_uint();
|
2019-05-04 17:50:36 +03:00
|
|
|
|
2020-06-12 22:07:52 +03:00
|
|
|
if (!number.has_value()) {
|
2021-05-31 17:43:25 +03:00
|
|
|
warnln("date: Invalid timestamp value");
|
2020-05-08 23:54:17 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2021-01-07 19:47:16 +03:00
|
|
|
|
2020-06-12 22:07:52 +03:00
|
|
|
timespec ts = { number.value(), 0 };
|
2021-11-28 00:12:29 +03:00
|
|
|
TRY(Core::System::clock_settime(CLOCK_REALTIME, &ts));
|
2021-01-07 19:47:16 +03:00
|
|
|
|
2020-05-08 23:54:17 +03:00
|
|
|
return 0;
|
2020-03-13 02:19:56 +03:00
|
|
|
}
|
2019-05-04 17:50:36 +03:00
|
|
|
|
2022-02-25 23:40:28 +03:00
|
|
|
if (print_unix_date + print_iso_8601 + print_rfc_3339 + print_rfc_5322 + !format_string.is_null() > 1) {
|
|
|
|
warnln("date: Multiple output formats specified");
|
2021-01-07 19:47:16 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-08-15 00:09:12 +03:00
|
|
|
auto date = Core::DateTime::now();
|
2022-02-25 23:40:28 +03:00
|
|
|
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;
|
|
|
|
}
|
2022-12-06 04:12:49 +03:00
|
|
|
outln("{}", date.to_deprecated_string(format_string.substring_view(1)));
|
2022-02-25 23:40:28 +03:00
|
|
|
} else if (print_unix_date) {
|
2021-08-15 00:09:12 +03:00
|
|
|
outln("{}", date.timestamp());
|
2021-01-07 19:47:16 +03:00
|
|
|
} else if (print_iso_8601) {
|
2022-12-06 04:12:49 +03:00
|
|
|
outln("{}", date.to_deprecated_string("%Y-%m-%dT%H:%M:%S%:z"sv));
|
2021-01-07 19:47:16 +03:00
|
|
|
} else if (print_rfc_5322) {
|
2022-12-06 04:12:49 +03:00
|
|
|
outln("{}", date.to_deprecated_string("%a, %d %b %Y %H:%M:%S %z"sv));
|
2021-01-07 19:47:16 +03:00
|
|
|
} else if (print_rfc_3339) {
|
2022-12-06 04:12:49 +03:00
|
|
|
outln("{}", date.to_deprecated_string("%Y-%m-%d %H:%M:%S%:z"sv));
|
2021-01-07 19:47:16 +03:00
|
|
|
} else {
|
2022-12-06 04:12:49 +03:00
|
|
|
outln("{}", date.to_deprecated_string("%Y-%m-%d %H:%M:%S %Z"sv));
|
2021-01-07 19:47:16 +03:00
|
|
|
}
|
2021-08-15 00:09:12 +03:00
|
|
|
return 0;
|
2018-10-25 18:29:49 +03:00
|
|
|
}
|