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
|
|
|
*/
|
|
|
|
|
|
2020-02-15 00:44:02 +01:00
|
|
|
#include <AK/String.h>
|
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-01-20 12:27:56 -05:00
|
|
|
TRY(Core::System::pledge("stdio settime rpath", nullptr));
|
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;
|
|
|
|
|
const char* set_date = nullptr;
|
|
|
|
|
|
|
|
|
|
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');
|
2021-11-27 22:12:29 +01:00
|
|
|
args_parser.parse(arguments);
|
2021-01-07 16:47:16 +00:00
|
|
|
|
|
|
|
|
if (set_date != nullptr) {
|
|
|
|
|
auto number = String(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
|
|
|
|
2021-01-07 16:47:16 +00:00
|
|
|
// FIXME: this should be improved and will need to be cleaned up
|
|
|
|
|
// when additional output formats and formatting is supported
|
|
|
|
|
if (print_unix_date && print_iso_8601 && print_rfc_3339 && print_rfc_5322) {
|
2021-05-31 15:43:25 +01:00
|
|
|
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();
|
2021-01-07 16:47:16 +00:00
|
|
|
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-01-25 16:09:51 -05:00
|
|
|
outln("{}", date.to_string("%Y-%m-%dT%H:%M:%S%:z"));
|
2021-01-07 16:47:16 +00:00
|
|
|
} else if (print_rfc_5322) {
|
2022-01-25 16:09:51 -05:00
|
|
|
outln("{}", date.to_string("%a, %d %b %Y %H:%M:%S %z"));
|
2021-01-07 16:47:16 +00:00
|
|
|
} else if (print_rfc_3339) {
|
2022-01-25 16:09:51 -05:00
|
|
|
outln("{}", date.to_string("%Y-%m-%d %H:%M:%S%:z"));
|
2021-01-07 16:47:16 +00:00
|
|
|
} else {
|
2022-01-28 11:26:01 -05:00
|
|
|
outln("{}", date.to_string("%Y-%m-%d %H:%M:%S %Z"));
|
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
|
|
|
}
|