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