Fix warnings after chrono upgrade (#30165)

Some functions are now deprecated, so use the suggested ones and do a
general cleanup of the affected lines of code.
This commit is contained in:
Martin Robinson 2023-08-23 02:44:00 +02:00 committed by GitHub
parent 838e0f598c
commit bd2503ceac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 43 deletions

View file

@ -783,11 +783,14 @@ fn max_day_in_month(year_num: i32, month_num: u32) -> Result<u32, ()> {
/// https://html.spec.whatwg.org/multipage/#week-number-of-the-last-day /// https://html.spec.whatwg.org/multipage/#week-number-of-the-last-day
fn max_week_in_year(year: i32) -> u32 { fn max_week_in_year(year: i32) -> u32 {
match Utc.ymd(year as i32, 1, 1).weekday() { Utc.with_ymd_and_hms(year as i32, 1, 1, 0, 0, 0)
.earliest()
.map(|date_time| match date_time.weekday() {
Weekday::Thu => 53, Weekday::Thu => 53,
Weekday::Wed if is_leap_year(year) => 53, Weekday::Wed if is_leap_year(year) => 53,
_ => 52, _ => 52,
} })
.unwrap_or(52)
} }
#[inline] #[inline]

View file

@ -2139,14 +2139,13 @@ impl HTMLInputElement {
// https://html.spec.whatwg.org/multipage/#concept-input-value-string-number // https://html.spec.whatwg.org/multipage/#concept-input-value-string-number
fn convert_string_to_number(&self, value: &DOMString) -> Result<f64, ()> { fn convert_string_to_number(&self, value: &DOMString) -> Result<f64, ()> {
match self.input_type() { match self.input_type() {
InputType::Date => match value.parse_date_string() { InputType::Date => value
Ok((year, month, day)) => { .parse_date_string()
let d = NaiveDate::from_ymd(year, month, day); .ok()
let duration = d.signed_duration_since(NaiveDate::from_ymd(1970, 1, 1)); .and_then(|(year, month, day)| NaiveDate::from_ymd_opt(year, month, day))
Ok(duration.num_milliseconds() as f64) .and_then(|date| date.and_hms_opt(0, 0, 0))
}, .map(|time| Ok(time.timestamp_millis() as f64))
_ => Err(()), .unwrap_or(Err(())),
},
InputType::Month => match value.parse_month_string() { InputType::Month => match value.parse_month_string() {
// This one returns number of months, not milliseconds // This one returns number of months, not milliseconds
// (specification requires this, presumably because number of // (specification requires this, presumably because number of
@ -2155,30 +2154,32 @@ impl HTMLInputElement {
Ok((year, month)) => Ok(((year - 1970) * 12) as f64 + (month as f64 - 1.0)), Ok((year, month)) => Ok(((year - 1970) * 12) as f64 + (month as f64 - 1.0)),
_ => Err(()), _ => Err(()),
}, },
InputType::Week => match value.parse_week_string() { InputType::Week => value
Ok((year, weeknum)) => { .parse_week_string()
let d = NaiveDate::from_isoywd(year, weeknum, Weekday::Mon); .ok()
let duration = d.signed_duration_since(NaiveDate::from_ymd(1970, 1, 1)); .and_then(|(year, weeknum)| NaiveDate::from_isoywd_opt(year, weeknum, Weekday::Mon))
Ok(duration.num_milliseconds() as f64) .and_then(|date| date.and_hms_opt(0, 0, 0))
}, .map(|time| Ok(time.timestamp_millis() as f64))
_ => Err(()), .unwrap_or(Err(())),
},
InputType::Time => match value.parse_time_string() { InputType::Time => match value.parse_time_string() {
Ok((hours, minutes, seconds)) => { Ok((hours, minutes, seconds)) => {
Ok((seconds as f64 + 60.0 * minutes as f64 + 3600.0 * hours as f64) * 1000.0) Ok((seconds as f64 + 60.0 * minutes as f64 + 3600.0 * hours as f64) * 1000.0)
}, },
_ => Err(()), _ => Err(()),
}, },
InputType::DatetimeLocal => match value.parse_local_date_and_time_string() { InputType::DatetimeLocal => {
// Is this supposed to know the locale's daylight-savings-time rules? // Is this supposed to know the locale's daylight-savings-time rules?
Ok(((year, month, day), (hours, minutes, seconds))) => { value
let d = NaiveDate::from_ymd(year, month, day); .parse_local_date_and_time_string()
let ymd_duration = d.signed_duration_since(NaiveDate::from_ymd(1970, 1, 1)); .ok()
.and_then(|((year, month, day), (hours, minutes, seconds))| {
let hms_millis = let hms_millis =
(seconds + 60.0 * minutes as f64 + 3600.0 * hours as f64) * 1000.0; (seconds + 60.0 * minutes as f64 + 3600.0 * hours as f64) * 1000.0;
Ok(ymd_duration.num_milliseconds() as f64 + hms_millis) NaiveDate::from_ymd_opt(year, month, day)
}, .and_then(|date| date.and_hms_opt(0, 0, 0))
_ => Err(()), .map(|time| Ok(time.timestamp_millis() as f64 + hms_millis))
})
.unwrap_or(Err(()))
}, },
InputType::Number | InputType::Range => value.parse_floating_point_number(), InputType::Number | InputType::Range => value.parse_floating_point_number(),
// min/max/valueAsNumber/stepDown/stepUp do not apply to // min/max/valueAsNumber/stepDown/stepUp do not apply to
@ -2230,25 +2231,34 @@ impl HTMLInputElement {
match self.input_type() { match self.input_type() {
InputType::Date => value InputType::Date => value
.parse_date_string() .parse_date_string()
.and_then(|(y, m, d)| NaiveDate::from_ymd_opt(y, m, d).ok_or(())) .ok()
.map(|date| date.and_hms(0, 0, 0)), .and_then(|(y, m, d)| NaiveDate::from_ymd_opt(y, m, d))
InputType::Time => value.parse_time_string().and_then(|(h, m, s)| { .and_then(|date| date.and_hms_opt(0, 0, 0))
.ok_or(()),
InputType::Time => value
.parse_time_string()
.ok()
.and_then(|(h, m, s)| {
let whole_seconds = s.floor(); let whole_seconds = s.floor();
let nanos = ((s - whole_seconds) * 1e9).floor() as u32; let nanos = ((s - whole_seconds) * 1e9).floor() as u32;
NaiveDate::from_ymd(1970, 1, 1) NaiveDate::from_ymd_opt(1970, 1, 1)
.and_hms_nano_opt(h, m, whole_seconds as u32, nanos) .and_then(|date| date.and_hms_nano_opt(h, m, whole_seconds as u32, nanos))
.ok_or(()) })
}), .ok_or(()),
InputType::Week => value InputType::Week => value
.parse_week_string() .parse_week_string()
.ok()
.and_then(|(iso_year, week)| { .and_then(|(iso_year, week)| {
NaiveDate::from_isoywd_opt(iso_year, week, Weekday::Mon).ok_or(()) NaiveDate::from_isoywd_opt(iso_year, week, Weekday::Mon)
}) })
.map(|date| date.and_hms(0, 0, 0)), .and_then(|date| date.and_hms_opt(0, 0, 0))
.ok_or(()),
InputType::Month => value InputType::Month => value
.parse_month_string() .parse_month_string()
.and_then(|(y, m)| NaiveDate::from_ymd_opt(y, m, 1).ok_or(())) .ok()
.map(|date| date.and_hms(0, 0, 0)), .and_then(|(y, m)| NaiveDate::from_ymd_opt(y, m, 1))
.and_then(|date| date.and_hms_opt(0, 0, 0))
.ok_or(()),
// does not apply to other types // does not apply to other types
_ => Err(()), _ => Err(()),
} }