date: fix date get timezone error while set utc time (#6495)

This commit is contained in:
MengshengWu 2024-06-28 22:01:00 +08:00
parent 11e8476456
commit e4ec608781
2 changed files with 12 additions and 4 deletions

View file

@ -227,11 +227,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
Box::new(iter)
}
DateSource::Human(relative_time) => {
// Get the current DateTime<FixedOffset> for things like "1 year ago"
let current_time = DateTime::<FixedOffset>::from(Local::now());
// double check the result is overflow or not of the current_time + relative_time
// Double check the result is overflow or not of the current_time + relative_time
// it may cause a panic of chrono::datetime::DateTime add
match current_time.checked_add_signed(relative_time) {
match now.checked_add_signed(relative_time) {
Some(date) => {
let iter = std::iter::once(Ok(date));
Box::new(iter)

View file

@ -141,6 +141,16 @@ fn test_date_utc() {
}
}
#[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");
}
#[test]
fn test_date_format_y() {
let scene = TestScenario::new(util_name!());