2023-08-21 08:49:27 +00:00
|
|
|
// This file is part of the uutils coreutils package.
|
|
|
|
//
|
|
|
|
// For the full copyright and license information, please view the LICENSE
|
|
|
|
// file that was distributed with this source code.
|
2023-03-20 13:51:19 +00:00
|
|
|
use crate::common::util::TestScenario;
|
2023-04-10 06:31:31 +00:00
|
|
|
use regex::Regex;
|
2021-03-19 08:54:01 +00:00
|
|
|
#[cfg(all(unix, not(target_os = "macos")))]
|
2023-06-04 12:55:16 +00:00
|
|
|
use uucore::process::geteuid;
|
2020-05-01 23:03:01 +00:00
|
|
|
|
2022-09-10 16:38:14 +00:00
|
|
|
#[test]
|
|
|
|
fn test_invalid_arg() {
|
|
|
|
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
|
|
|
|
}
|
|
|
|
|
2020-05-01 23:03:01 +00:00
|
|
|
#[test]
|
|
|
|
fn test_date_email() {
|
2022-04-02 08:47:37 +00:00
|
|
|
for param in ["--rfc-email", "--rfc-e", "-R"] {
|
2022-01-29 00:03:28 +00:00
|
|
|
new_ucmd!().arg(param).succeeds();
|
|
|
|
}
|
2020-05-01 23:03:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_date_rfc_3339() {
|
|
|
|
let scene = TestScenario::new(util_name!());
|
|
|
|
|
2021-04-22 20:37:44 +00:00
|
|
|
let rfc_regexp = concat!(
|
|
|
|
r#"(\d+)-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])\s([01]\d|2[0-3]):"#,
|
|
|
|
r#"([0-5]\d):([0-5]\d|60)(\.\d+)?(([Zz])|([\+|\-]([01]\d|2[0-3])))"#
|
|
|
|
);
|
2020-05-01 23:03:01 +00:00
|
|
|
let re = Regex::new(rfc_regexp).unwrap();
|
|
|
|
|
|
|
|
// Check that the output matches the regexp
|
2022-04-02 08:47:37 +00:00
|
|
|
for param in ["--rfc-3339", "--rfc-3"] {
|
2022-01-29 00:03:28 +00:00
|
|
|
scene
|
|
|
|
.ucmd()
|
2023-01-27 09:29:45 +00:00
|
|
|
.arg(format!("{param}=ns"))
|
2022-01-29 00:03:28 +00:00
|
|
|
.succeeds()
|
|
|
|
.stdout_matches(&re);
|
2021-04-22 20:37:44 +00:00
|
|
|
|
2022-01-29 00:03:28 +00:00
|
|
|
scene
|
|
|
|
.ucmd()
|
2023-01-27 09:29:45 +00:00
|
|
|
.arg(format!("{param}=seconds"))
|
2022-01-29 00:03:28 +00:00
|
|
|
.succeeds()
|
|
|
|
.stdout_matches(&re);
|
|
|
|
}
|
2020-05-01 23:03:01 +00:00
|
|
|
}
|
|
|
|
|
2023-03-19 07:54:45 +00:00
|
|
|
#[test]
|
|
|
|
fn test_date_rfc_3339_invalid_arg() {
|
|
|
|
for param in ["--iso-3339", "--rfc-3"] {
|
|
|
|
new_ucmd!().arg(format!("{param}=foo")).fails();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 17:50:08 +00:00
|
|
|
#[test]
|
|
|
|
fn test_date_rfc_8601_default() {
|
|
|
|
let re = Regex::new(r"^\d{4}-\d{2}-\d{2}\n$").unwrap();
|
|
|
|
for param in ["--iso-8601", "--i"] {
|
|
|
|
new_ucmd!().arg(param).succeeds().stdout_matches(&re);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-01 23:03:01 +00:00
|
|
|
#[test]
|
|
|
|
fn test_date_rfc_8601() {
|
2023-03-16 14:53:43 +00:00
|
|
|
let re = Regex::new(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2},\d{9}[+-]\d{2}:\d{2}\n$").unwrap();
|
2022-04-02 08:47:37 +00:00
|
|
|
for param in ["--iso-8601", "--i"] {
|
2023-03-16 14:53:43 +00:00
|
|
|
new_ucmd!()
|
|
|
|
.arg(format!("{param}=ns"))
|
|
|
|
.succeeds()
|
|
|
|
.stdout_matches(&re);
|
2022-01-29 00:03:28 +00:00
|
|
|
}
|
2020-05-01 23:03:01 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 19:50:37 +00:00
|
|
|
#[test]
|
|
|
|
fn test_date_rfc_8601_invalid_arg() {
|
|
|
|
for param in ["--iso-8601", "--i"] {
|
|
|
|
new_ucmd!().arg(format!("{param}=@")).fails();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-01 23:03:01 +00:00
|
|
|
#[test]
|
|
|
|
fn test_date_rfc_8601_second() {
|
2023-03-16 14:53:43 +00:00
|
|
|
let re = Regex::new(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{2}:\d{2}\n$").unwrap();
|
2022-04-02 08:47:37 +00:00
|
|
|
for param in ["--iso-8601", "--i"] {
|
2023-03-16 14:53:43 +00:00
|
|
|
new_ucmd!()
|
|
|
|
.arg(format!("{param}=second"))
|
|
|
|
.succeeds()
|
|
|
|
.stdout_matches(&re);
|
|
|
|
new_ucmd!()
|
|
|
|
.arg(format!("{param}=seconds"))
|
|
|
|
.succeeds()
|
|
|
|
.stdout_matches(&re);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_date_rfc_8601_minute() {
|
|
|
|
let re = Regex::new(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}[+-]\d{2}:\d{2}\n$").unwrap();
|
|
|
|
for param in ["--iso-8601", "--i"] {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg(format!("{param}=minute"))
|
|
|
|
.succeeds()
|
|
|
|
.stdout_matches(&re);
|
|
|
|
new_ucmd!()
|
|
|
|
.arg(format!("{param}=minutes"))
|
|
|
|
.succeeds()
|
|
|
|
.stdout_matches(&re);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_date_rfc_8601_hour() {
|
|
|
|
let re = Regex::new(r"^\d{4}-\d{2}-\d{2}T\d{2}[+-]\d{2}:\d{2}\n$").unwrap();
|
|
|
|
for param in ["--iso-8601", "--i"] {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg(format!("{param}=hour"))
|
|
|
|
.succeeds()
|
|
|
|
.stdout_matches(&re);
|
|
|
|
new_ucmd!()
|
|
|
|
.arg(format!("{param}=hours"))
|
|
|
|
.succeeds()
|
|
|
|
.stdout_matches(&re);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_date_rfc_8601_date() {
|
|
|
|
let re = Regex::new(r"^\d{4}-\d{2}-\d{2}\n$").unwrap();
|
|
|
|
for param in ["--iso-8601", "--i"] {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg(format!("{param}=date"))
|
|
|
|
.succeeds()
|
|
|
|
.stdout_matches(&re);
|
2022-01-29 00:03:28 +00:00
|
|
|
}
|
2020-05-01 23:03:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_date_utc() {
|
2022-04-02 08:47:37 +00:00
|
|
|
for param in ["--universal", "--utc", "--uni", "--u"] {
|
2022-01-29 00:03:28 +00:00
|
|
|
new_ucmd!().arg(param).succeeds();
|
|
|
|
}
|
2020-05-01 23:03:01 +00:00
|
|
|
}
|
|
|
|
|
2024-06-28 14:01:00 +00:00
|
|
|
#[test]
|
|
|
|
fn test_date_utc_issue_6495() {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg("-u")
|
|
|
|
.arg("-d")
|
|
|
|
.arg("@0")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is("Thu Jan 1 00:00:00 1970\n");
|
|
|
|
}
|
|
|
|
|
2020-05-01 23:03:01 +00:00
|
|
|
#[test]
|
|
|
|
fn test_date_format_y() {
|
|
|
|
let scene = TestScenario::new(util_name!());
|
|
|
|
|
2023-02-16 14:33:33 +00:00
|
|
|
let mut re = Regex::new(r"^\d{4}\n$").unwrap();
|
2021-04-22 20:37:44 +00:00
|
|
|
scene.ucmd().arg("+%Y").succeeds().stdout_matches(&re);
|
2020-05-01 23:03:01 +00:00
|
|
|
|
2023-02-16 14:33:33 +00:00
|
|
|
re = Regex::new(r"^\d{2}\n$").unwrap();
|
2021-04-22 20:37:44 +00:00
|
|
|
scene.ucmd().arg("+%y").succeeds().stdout_matches(&re);
|
2020-05-01 23:03:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_date_format_m() {
|
|
|
|
let scene = TestScenario::new(util_name!());
|
|
|
|
|
|
|
|
let mut re = Regex::new(r"\S+").unwrap();
|
2021-04-22 20:37:44 +00:00
|
|
|
scene.ucmd().arg("+%b").succeeds().stdout_matches(&re);
|
2020-05-01 23:03:01 +00:00
|
|
|
|
2023-02-16 14:33:33 +00:00
|
|
|
re = Regex::new(r"^\d{2}\n$").unwrap();
|
2021-04-22 20:37:44 +00:00
|
|
|
scene.ucmd().arg("+%m").succeeds().stdout_matches(&re);
|
2020-05-01 23:03:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_date_format_day() {
|
|
|
|
let scene = TestScenario::new(util_name!());
|
|
|
|
|
|
|
|
let mut re = Regex::new(r"\S+").unwrap();
|
2021-04-22 20:37:44 +00:00
|
|
|
scene.ucmd().arg("+%a").succeeds().stdout_matches(&re);
|
2020-05-01 23:03:01 +00:00
|
|
|
|
|
|
|
re = Regex::new(r"\S+").unwrap();
|
2021-04-22 20:37:44 +00:00
|
|
|
scene.ucmd().arg("+%A").succeeds().stdout_matches(&re);
|
2020-05-01 23:03:01 +00:00
|
|
|
|
2023-02-16 14:33:33 +00:00
|
|
|
re = Regex::new(r"^\d{1}\n$").unwrap();
|
2021-04-22 20:37:44 +00:00
|
|
|
scene.ucmd().arg("+%u").succeeds().stdout_matches(&re);
|
2020-05-01 23:03:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_date_format_full_day() {
|
|
|
|
let re = Regex::new(r"\S+ \d{4}-\d{2}-\d{2}").unwrap();
|
2021-04-22 20:37:44 +00:00
|
|
|
new_ucmd!()
|
|
|
|
.arg("+'%a %Y-%m-%d'")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_matches(&re);
|
2020-05-01 23:03:01 +00:00
|
|
|
}
|
2021-03-19 08:54:01 +00:00
|
|
|
|
2022-11-18 20:32:59 +00:00
|
|
|
#[test]
|
|
|
|
fn test_date_issue_3780() {
|
|
|
|
new_ucmd!().arg("+%Y-%m-%d %H-%M-%S%:::z").succeeds();
|
|
|
|
}
|
|
|
|
|
2021-05-11 21:03:59 +00:00
|
|
|
#[test]
|
|
|
|
fn test_date_nano_seconds() {
|
|
|
|
// %N nanoseconds (000000000..999999999)
|
2023-02-16 14:33:33 +00:00
|
|
|
let re = Regex::new(r"^\d{1,9}\n$").unwrap();
|
2021-05-11 21:03:59 +00:00
|
|
|
new_ucmd!().arg("+%N").succeeds().stdout_matches(&re);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_date_format_without_plus() {
|
|
|
|
// [+FORMAT]
|
|
|
|
new_ucmd!()
|
|
|
|
.arg("%s")
|
|
|
|
.fails()
|
2021-06-21 22:22:30 +00:00
|
|
|
.stderr_contains("date: invalid date '%s'")
|
2021-05-11 21:03:59 +00:00
|
|
|
.code_is(1);
|
|
|
|
}
|
|
|
|
|
2021-05-12 08:21:24 +00:00
|
|
|
#[test]
|
|
|
|
fn test_date_format_literal() {
|
|
|
|
new_ucmd!().arg("+%%s").succeeds().stdout_is("%s\n");
|
|
|
|
new_ucmd!().arg("+%%N").succeeds().stdout_is("%N\n");
|
|
|
|
}
|
|
|
|
|
2021-03-19 08:54:01 +00:00
|
|
|
#[test]
|
|
|
|
#[cfg(all(unix, not(target_os = "macos")))]
|
|
|
|
fn test_date_set_valid() {
|
2023-06-04 12:55:16 +00:00
|
|
|
if geteuid() == 0 {
|
2021-04-22 20:37:44 +00:00
|
|
|
new_ucmd!()
|
2021-03-19 17:01:43 +00:00
|
|
|
.arg("--set")
|
|
|
|
.arg("2020-03-12 13:30:00+08:00")
|
2021-04-22 20:37:44 +00:00
|
|
|
.succeeds()
|
|
|
|
.no_stdout()
|
|
|
|
.no_stderr();
|
2021-03-19 08:54:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(any(windows, all(unix, not(target_os = "macos"))))]
|
|
|
|
fn test_date_set_invalid() {
|
2021-04-22 20:37:44 +00:00
|
|
|
let result = new_ucmd!().arg("--set").arg("123abcd").fails();
|
|
|
|
result.no_stdout();
|
|
|
|
assert!(result.stderr_str().starts_with("date: invalid date "));
|
2021-03-19 08:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-02-09 18:08:28 +00:00
|
|
|
#[cfg(all(unix, not(any(target_os = "android", target_os = "macos"))))]
|
2021-03-19 08:54:01 +00:00
|
|
|
fn test_date_set_permissions_error() {
|
2023-06-04 12:55:16 +00:00
|
|
|
if !(geteuid() == 0 || uucore::os::is_wsl_1()) {
|
2021-04-22 20:37:44 +00:00
|
|
|
let result = new_ucmd!()
|
|
|
|
.arg("--set")
|
|
|
|
.arg("2020-03-11 21:45:00+08:00")
|
|
|
|
.fails();
|
|
|
|
result.no_stdout();
|
|
|
|
assert!(result.stderr_str().starts_with("date: cannot set date: "));
|
2021-03-19 08:54:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
fn test_date_set_mac_unavailable() {
|
2021-04-22 20:37:44 +00:00
|
|
|
let result = new_ucmd!()
|
|
|
|
.arg("--set")
|
|
|
|
.arg("2020-03-11 21:45:00+08:00")
|
|
|
|
.fails();
|
|
|
|
result.no_stdout();
|
2021-03-19 17:01:43 +00:00
|
|
|
assert!(result
|
2021-04-22 20:37:44 +00:00
|
|
|
.stderr_str()
|
2021-03-19 17:01:43 +00:00
|
|
|
.starts_with("date: setting the date is not supported by macOS"));
|
2021-03-19 08:54:01 +00:00
|
|
|
}
|
2021-03-20 07:17:18 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(all(unix, not(target_os = "macos")))]
|
2024-06-30 14:27:08 +00:00
|
|
|
/// TODO: expected to fail currently; change to `succeeds()` when required.
|
2021-03-20 07:17:18 +00:00
|
|
|
fn test_date_set_valid_2() {
|
2023-06-04 12:55:16 +00:00
|
|
|
if geteuid() == 0 {
|
2021-04-22 20:37:44 +00:00
|
|
|
let result = new_ucmd!()
|
2021-03-20 07:17:18 +00:00
|
|
|
.arg("--set")
|
2021-05-31 03:55:28 +00:00
|
|
|
.arg("Sat 20 Mar 2021 14:53:01 AWST") // spell-checker:disable-line
|
2021-03-24 07:33:11 +00:00
|
|
|
.fails();
|
2021-04-22 20:37:44 +00:00
|
|
|
result.no_stdout();
|
|
|
|
assert!(result.stderr_str().starts_with("date: invalid date "));
|
2021-03-20 07:17:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-18 13:19:57 +00:00
|
|
|
#[test]
|
|
|
|
fn test_date_for_invalid_file() {
|
|
|
|
let result = new_ucmd!().arg("--file").arg("invalid_file").fails();
|
|
|
|
result.no_stdout();
|
|
|
|
assert_eq!(
|
|
|
|
result.stderr_str().trim(),
|
|
|
|
"date: invalid_file: No such file or directory",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-29 05:49:14 +00:00
|
|
|
#[test]
|
2023-05-10 16:40:58 +00:00
|
|
|
#[cfg(unix)]
|
|
|
|
fn test_date_for_no_permission_file() {
|
2024-06-30 14:27:08 +00:00
|
|
|
use std::os::unix::fs::PermissionsExt;
|
2023-05-10 16:40:58 +00:00
|
|
|
const FILE: &str = "file-no-perm-1";
|
|
|
|
|
2024-06-30 14:27:08 +00:00
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
|
|
|
|
2023-05-10 16:40:58 +00:00
|
|
|
let file = std::fs::OpenOptions::new()
|
|
|
|
.create(true)
|
2024-03-09 21:30:22 +00:00
|
|
|
.truncate(true)
|
2023-05-10 16:40:58 +00:00
|
|
|
.write(true)
|
|
|
|
.open(at.plus(FILE))
|
|
|
|
.unwrap();
|
|
|
|
file.set_permissions(std::fs::Permissions::from_mode(0o222))
|
|
|
|
.unwrap();
|
|
|
|
let result = ucmd.arg("--file").arg(FILE).fails();
|
|
|
|
result.no_stdout();
|
|
|
|
assert_eq!(
|
|
|
|
result.stderr_str().trim(),
|
|
|
|
format!("date: {FILE}: Permission denied")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-22 21:29:55 +00:00
|
|
|
#[test]
|
2023-03-29 05:49:14 +00:00
|
|
|
fn test_date_for_dir_as_file() {
|
|
|
|
let result = new_ucmd!().arg("--file").arg("/").fails();
|
|
|
|
result.no_stdout();
|
|
|
|
assert_eq!(
|
|
|
|
result.stderr_str().trim(),
|
|
|
|
"date: expected file, got directory '/'",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-18 13:19:57 +00:00
|
|
|
#[test]
|
|
|
|
fn test_date_for_file() {
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
|
|
|
let file = "test_date_for_file";
|
|
|
|
at.touch(file);
|
|
|
|
ucmd.arg("--file").arg(file).succeeds();
|
|
|
|
}
|
|
|
|
|
2021-03-20 07:17:18 +00:00
|
|
|
#[test]
|
|
|
|
#[cfg(all(unix, not(target_os = "macos")))]
|
2024-06-30 14:27:08 +00:00
|
|
|
/// TODO: expected to fail currently; change to `succeeds()` when required.
|
2021-03-20 07:17:18 +00:00
|
|
|
fn test_date_set_valid_3() {
|
2023-06-04 12:55:16 +00:00
|
|
|
if geteuid() == 0 {
|
2021-04-22 20:37:44 +00:00
|
|
|
let result = new_ucmd!()
|
2021-03-20 07:17:18 +00:00
|
|
|
.arg("--set")
|
|
|
|
.arg("Sat 20 Mar 2021 14:53:01") // Local timezone
|
2021-03-24 07:33:11 +00:00
|
|
|
.fails();
|
2021-04-22 20:37:44 +00:00
|
|
|
result.no_stdout();
|
|
|
|
assert!(result.stderr_str().starts_with("date: invalid date "));
|
2021-03-20 07:17:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(all(unix, not(target_os = "macos")))]
|
2024-06-30 14:27:08 +00:00
|
|
|
/// TODO: expected to fail currently; change to `succeeds()` when required.
|
2021-03-20 07:17:18 +00:00
|
|
|
fn test_date_set_valid_4() {
|
2023-06-04 12:55:16 +00:00
|
|
|
if geteuid() == 0 {
|
2021-04-22 20:37:44 +00:00
|
|
|
let result = new_ucmd!()
|
2021-03-20 07:17:18 +00:00
|
|
|
.arg("--set")
|
|
|
|
.arg("2020-03-11 21:45:00") // Local timezone
|
2021-03-24 07:33:11 +00:00
|
|
|
.fails();
|
2021-04-22 20:37:44 +00:00
|
|
|
result.no_stdout();
|
|
|
|
assert!(result.stderr_str().starts_with("date: invalid date "));
|
2021-03-20 07:17:18 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-25 20:11:34 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_format_string() {
|
|
|
|
let result = new_ucmd!().arg("+%!").fails();
|
|
|
|
result.no_stdout();
|
|
|
|
assert!(result.stderr_str().starts_with("date: invalid format "));
|
|
|
|
}
|
2023-03-17 20:49:26 +00:00
|
|
|
|
2023-05-27 13:41:12 +00:00
|
|
|
#[test]
|
|
|
|
fn test_unsupported_format() {
|
|
|
|
let result = new_ucmd!().arg("+%#z").fails();
|
|
|
|
result.no_stdout();
|
|
|
|
assert!(result.stderr_str().starts_with("date: invalid format %#z"));
|
|
|
|
}
|
|
|
|
|
2023-04-25 22:11:11 +00:00
|
|
|
#[test]
|
|
|
|
fn test_date_string_human() {
|
|
|
|
let date_formats = vec![
|
|
|
|
"1 year ago",
|
|
|
|
"1 year",
|
|
|
|
"2 months ago",
|
|
|
|
"15 days ago",
|
|
|
|
"1 week ago",
|
|
|
|
"5 hours ago",
|
|
|
|
"30 minutes ago",
|
|
|
|
"10 seconds",
|
2024-07-02 18:07:06 +00:00
|
|
|
"last day",
|
|
|
|
"last week",
|
|
|
|
"last month",
|
|
|
|
"last year",
|
|
|
|
"next day",
|
|
|
|
"next week",
|
|
|
|
"next month",
|
|
|
|
"next year",
|
2023-04-25 22:11:11 +00:00
|
|
|
];
|
|
|
|
let re = Regex::new(r"^\d{4}-\d{2}-\d{2} \d{2}:\d{2}\n$").unwrap();
|
|
|
|
for date_format in date_formats {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg("-d")
|
|
|
|
.arg(date_format)
|
|
|
|
.arg("+%Y-%m-%d %S:%M")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_matches(&re);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 20:49:26 +00:00
|
|
|
#[test]
|
|
|
|
fn test_invalid_date_string() {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg("-d")
|
|
|
|
.arg("foo")
|
|
|
|
.fails()
|
|
|
|
.no_stdout()
|
|
|
|
.stderr_contains("invalid date");
|
|
|
|
}
|
2023-08-20 14:55:38 +00:00
|
|
|
|
2024-06-15 11:18:18 +00:00
|
|
|
#[test]
|
|
|
|
fn test_date_one_digit_date() {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg("-d")
|
|
|
|
.arg("2000-1-1")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_contains("Sat Jan 1 00:00:00 2000");
|
|
|
|
|
|
|
|
new_ucmd!()
|
|
|
|
.arg("-d")
|
|
|
|
.arg("2000-1-4")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_contains("Tue Jan 4 00:00:00 2000");
|
|
|
|
}
|
|
|
|
|
2023-08-20 14:55:38 +00:00
|
|
|
#[test]
|
|
|
|
fn test_date_overflow() {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg("-d68888888888888sms")
|
|
|
|
.fails()
|
|
|
|
.no_stdout()
|
|
|
|
.stderr_contains("invalid date");
|
|
|
|
}
|
2024-03-30 14:17:10 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_date_parse_from_format() {
|
|
|
|
const FILE: &str = "file-with-dates";
|
2024-06-30 14:27:08 +00:00
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2024-03-30 14:17:10 +00:00
|
|
|
|
|
|
|
at.write(
|
|
|
|
FILE,
|
|
|
|
"2023-03-27 08:30:00\n\
|
|
|
|
2023-04-01 12:00:00\n\
|
|
|
|
2023-04-15 18:30:00",
|
|
|
|
);
|
|
|
|
ucmd.arg("-f")
|
|
|
|
.arg(at.plus(FILE))
|
|
|
|
.arg("+%Y-%m-%d %H:%M:%S")
|
|
|
|
.succeeds();
|
|
|
|
}
|
2024-03-31 19:15:46 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_date_from_stdin() {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg("-f")
|
|
|
|
.arg("-")
|
|
|
|
.pipe_in(
|
|
|
|
"2023-03-27 08:30:00\n\
|
|
|
|
2023-04-01 12:00:00\n\
|
|
|
|
2023-04-15 18:30:00\n",
|
|
|
|
)
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is(
|
|
|
|
"Mon Mar 27 08:30:00 2023\n\
|
|
|
|
Sat Apr 1 12:00:00 2023\n\
|
|
|
|
Sat Apr 15 18:30:00 2023\n",
|
|
|
|
);
|
|
|
|
}
|