Skip to content

update human-date-parser to 3.0#15426

Merged
fdncred merged 3 commits intonushell:mainfrom
fdncred:update_human_date_parser
Apr 1, 2025
Merged

update human-date-parser to 3.0#15426
fdncred merged 3 commits intonushell:mainfrom
fdncred:update_human_date_parser

Conversation

@fdncred
Copy link
Copy Markdown
Contributor

@fdncred fdncred commented Mar 26, 2025

Description

There's been much debate about whether to keep human-date-parser in into datetime. We saw recently that a new version of the crate was released that addressed some of our concerns. This PR is to make it easier to test those fixes.

User-Facing Changes

Tests + Formatting

After Submitting

Comment on lines +307 to +335
let local_offset = *Local::now().offset();
let datetime = match local_offset.from_local_datetime(&date) {
chrono::LocalResult::Single(dt) => dt,
chrono::LocalResult::Ambiguous(_, _) => {
return Value::error(
ShellError::DatetimeParseError {
msg: "Ambiguous datetime".to_string(),
span,
},
span,
);
}
chrono::LocalResult::None => {
return Value::error(
ShellError::DatetimeParseError {
msg: "Invalid datetime".to_string(),
span,
},
span,
);
}
};
let dt_fixed = TimeZone::from_local_datetime(
&local_offset,
&datetime.naive_local(),
)
.single()
.unwrap_or_default();
return Value::date(dt_fixed, span);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you explain what exactly this is doing, was this prompted by a change in human-date-parser 3.0?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

  1. Yes, the api to human-date-parser changed so the way we call it has to change.
  2. The key to this match here is, if it's a date use local time + provided date, if it's a time, use local date + provided time, if it's a datetime use that.
  3. The match returns a NativeDate, NativeTime, or NativeDateTime. None have a tz. So, the idea is to use the local tz. That's what local_offset is about.
  4. Then local_offset.from_local_datetime() returns LocalResult which is what that match is about.
  5. Then I cleanup some left over code, oops. Let me fix that now. Thanks for making me explain it.

@fdncred fdncred merged commit a23e96c into nushell:main Apr 1, 2025
16 checks passed
@github-actions github-actions bot added this to the v0.104.0 milestone Apr 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants