-
-
Notifications
You must be signed in to change notification settings - Fork 2k
date: add support for dot-separated dates #14614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,10 +35,10 @@ | |
|
|
||
| /// OHOS helper: pass through the system time zone ID returned by | ||
| /// TimeService (OH_TimeService_GetTimeZone, e.g. "Asia/Shanghai") and | ||
| /// resolve it against the embedded IANA tzdata (jiff-tzdb) so that | ||
| /// historial DST rules and transitions are preserved. jiff's | ||
| /// `try_system()` is useless on OHOS because both `/etc/localtime` and | ||
| /// the zoneinfo dirs are absent. | ||
| #[cfg(target_env = "ohos")] | ||
| fn ohos_system_zone() -> jiff::tz::TimeZone { | ||
| use core::ffi::{CStr, c_char}; | ||
|
|
@@ -55,7 +55,7 @@ | |
| let id = unsafe { CStr::from_ptr(buf.as_ptr() as *const c_char) } | ||
| .to_string_lossy() | ||
| .into_owned(); | ||
| if let Some((name, tzif)) = jiff_tzdb::get(&id) { | ||
| if let Ok(tz) = jiff::tz::TimeZone::tzif(name, tzif) { | ||
| return tz; | ||
| } | ||
|
|
@@ -1176,6 +1176,32 @@ | |
| })) | ||
| } | ||
|
|
||
| /// Convert a date string to iso if it is seperated by dots. | ||
| fn convert_to_iso(date_str: &str) -> Option<String> { | ||
| // Seperate the date from anything else, in case we have something like "01.01.2008 03:00 p.m." | ||
| let mut parts = date_str.splitn(2, ' '); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is one exception where this approach will fail: GNU |
||
| let date_part = parts.next()?; | ||
| let time_part = parts.next(); | ||
|
|
||
| // Get the year, month and the day | ||
| let date_subparts: Vec<&str> = date_part.split('.').collect(); | ||
| if date_subparts.len() != 3 { | ||
| return None; | ||
| } | ||
|
Comment on lines
+1187
to
+1190
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With GNU |
||
|
|
||
| // Format in iso style | ||
| let new_date = format!( | ||
| "{}-{}-{}", | ||
| date_subparts[2], date_subparts[1], date_subparts[0] | ||
| ); | ||
|
|
||
| // Concat the date and the time back | ||
| match time_part { | ||
| Some(time) => Some(format!("{new_date} {time}")), | ||
| None => Some(new_date), | ||
| } | ||
| } | ||
|
|
||
| /// Parse a string into either an in-range [`Zoned`] value or an extended date. | ||
| fn parse_date<S: AsRef<str>>( | ||
| s: S, | ||
|
|
@@ -1209,7 +1235,9 @@ | |
| return Ok(ParsedDateTime::InRange(zoned)); | ||
| } | ||
|
|
||
| match parse_datetime::parse_datetime_at_date(now.clone(), input_str) { | ||
| let input_str = convert_to_iso(input_str).unwrap_or(input_str.to_string()); | ||
|
|
||
| match parse_datetime::parse_datetime_at_date(now.clone(), &input_str) { | ||
| // Convert to system timezone for display | ||
| // (parse_datetime returns a value in the input's timezone) | ||
| Ok(ParsedDateTime::InRange(date)) => { | ||
|
|
@@ -1248,11 +1276,10 @@ | |
| Ok(ParsedDateTime::InRange(result)) | ||
| } | ||
| Ok(ParsedDateTime::Extended(date)) if allow_extended => Ok(ParsedDateTime::Extended(date)), | ||
| Ok(ParsedDateTime::Extended(_)) => Err(( | ||
| input_str.into(), | ||
| parse_datetime::ParseDateTimeError::InvalidInput, | ||
| )), | ||
| Err(e) => Err((input_str.into(), e)), | ||
| Ok(ParsedDateTime::Extended(_)) => { | ||
| Err((input_str, parse_datetime::ParseDateTimeError::InvalidInput)) | ||
| } | ||
| Err(e) => Err((input_str, e)), | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might make sense to move the functionality over to https://github.com/uutils/parse_datetime . There we already use a parser library which could be helpful for the functionality I mentioned in the other comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I was just looking for a such function there, anyways, should we add tests for dates like
01.01. 2008 03:00 p.m.and01.01. 03:00 p.m.?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, please :)