Skip to content

Commit

Permalink
Merge pull request uutils#6503 from CausingBrick/issue-6495
Browse files Browse the repository at this point in the history
date: fix date get timezone error while set utc time (uutils#6495)
  • Loading branch information
cakebaker committed Jun 28, 2024
2 parents 371ba28 + e4ec608 commit 69b603b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/uu/date/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions tests/by-util/test_date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!());
Expand Down

0 comments on commit 69b603b

Please sign in to comment.