diff --git a/crates/polars-time/src/chunkedarray/utf8/infer.rs b/crates/polars-time/src/chunkedarray/utf8/infer.rs index 074f428eda8f..7ac33e85545d 100644 --- a/crates/polars-time/src/chunkedarray/utf8/infer.rs +++ b/crates/polars-time/src/chunkedarray/utf8/infer.rs @@ -231,97 +231,34 @@ impl TryFromWithUnit for DatetimeInfer { fn try_from_with_unit(value: Pattern, time_unit: Option) -> PolarsResult { let time_unit = time_unit.expect("time_unit must be provided for datetime"); - match (value, time_unit) { - (Pattern::DatetimeDMY | Pattern::DateDMY, TimeUnit::Milliseconds) => { - Ok(DatetimeInfer { - pattern: Pattern::DatetimeDMY, - patterns: patterns::DATETIME_D_M_Y, - latest_fmt: patterns::DATETIME_D_M_Y[0], - transform: transform_datetime_ms, - transform_bytes: StrpTimeState::default(), - fmt_len: 0, - logical_type: DataType::Datetime(TimeUnit::Milliseconds, None), - }) - }, - (Pattern::DatetimeDMY | Pattern::DateDMY, TimeUnit::Microseconds) => { - Ok(DatetimeInfer { - pattern: Pattern::DatetimeDMY, - patterns: patterns::DATETIME_D_M_Y, - latest_fmt: patterns::DATETIME_D_M_Y[0], - transform: transform_datetime_us, - transform_bytes: StrpTimeState::default(), - fmt_len: 0, - logical_type: DataType::Datetime(TimeUnit::Microseconds, None), - }) - }, - (Pattern::DatetimeDMY | Pattern::DateDMY, TimeUnit::Nanoseconds) => Ok(DatetimeInfer { - pattern: Pattern::DatetimeDMY, - patterns: patterns::DATETIME_D_M_Y, - latest_fmt: patterns::DATETIME_D_M_Y[0], - transform: transform_datetime_ns, - transform_bytes: StrpTimeState::default(), - fmt_len: 0, - logical_type: DataType::Datetime(TimeUnit::Nanoseconds, None), - }), - (Pattern::DatetimeYMD | Pattern::DateYMD, TimeUnit::Milliseconds) => { - Ok(DatetimeInfer { - pattern: Pattern::DatetimeYMD, - patterns: patterns::DATETIME_Y_M_D, - latest_fmt: patterns::DATETIME_Y_M_D[0], - transform: transform_datetime_ms, - transform_bytes: StrpTimeState::default(), - fmt_len: 0, - logical_type: DataType::Datetime(TimeUnit::Milliseconds, None), - }) + + let transform = match (time_unit, value) { + (TimeUnit::Milliseconds, Pattern::DatetimeYMDZ) => transform_tzaware_datetime_ms, + (TimeUnit::Milliseconds, _) => transform_datetime_ms, + (TimeUnit::Microseconds, Pattern::DatetimeYMDZ) => transform_tzaware_datetime_us, + (TimeUnit::Microseconds, _) => transform_datetime_us, + (TimeUnit::Nanoseconds, Pattern::DatetimeYMDZ) => transform_tzaware_datetime_ns, + (TimeUnit::Nanoseconds, _) => transform_datetime_ns, + }; + let (pattern, patterns) = match value { + Pattern::DatetimeDMY | Pattern::DateDMY => { + (Pattern::DatetimeDMY, patterns::DATETIME_D_M_Y) }, - (Pattern::DatetimeYMD | Pattern::DateYMD, TimeUnit::Microseconds) => { - Ok(DatetimeInfer { - pattern: Pattern::DatetimeYMD, - patterns: patterns::DATETIME_Y_M_D, - latest_fmt: patterns::DATETIME_Y_M_D[0], - transform: transform_datetime_us, - transform_bytes: StrpTimeState::default(), - fmt_len: 0, - logical_type: DataType::Datetime(TimeUnit::Microseconds, None), - }) + Pattern::DatetimeYMD | Pattern::DateYMD => { + (Pattern::DatetimeYMD, patterns::DATETIME_Y_M_D) }, - (Pattern::DatetimeYMD | Pattern::DateYMD, TimeUnit::Nanoseconds) => Ok(DatetimeInfer { - pattern: Pattern::DatetimeYMD, - patterns: patterns::DATETIME_Y_M_D, - latest_fmt: patterns::DATETIME_Y_M_D[0], - transform: transform_datetime_ns, - transform_bytes: StrpTimeState::default(), - fmt_len: 0, - logical_type: DataType::Datetime(TimeUnit::Nanoseconds, None), - }), - (Pattern::DatetimeYMDZ, TimeUnit::Milliseconds) => Ok(DatetimeInfer { - pattern: Pattern::DatetimeYMDZ, - patterns: patterns::DATETIME_Y_M_D_Z, - latest_fmt: patterns::DATETIME_Y_M_D_Z[0], - transform: transform_tzaware_datetime_ms, - transform_bytes: StrpTimeState::default(), - fmt_len: 0, - logical_type: DataType::Datetime(TimeUnit::Milliseconds, None), - }), - (Pattern::DatetimeYMDZ, TimeUnit::Microseconds) => Ok(DatetimeInfer { - pattern: Pattern::DatetimeYMDZ, - patterns: patterns::DATETIME_Y_M_D_Z, - latest_fmt: patterns::DATETIME_Y_M_D_Z[0], - transform: transform_tzaware_datetime_us, - transform_bytes: StrpTimeState::default(), - fmt_len: 0, - logical_type: DataType::Datetime(TimeUnit::Microseconds, None), - }), - (Pattern::DatetimeYMDZ, TimeUnit::Nanoseconds) => Ok(DatetimeInfer { - pattern: Pattern::DatetimeYMDZ, - patterns: patterns::DATETIME_Y_M_D_Z, - latest_fmt: patterns::DATETIME_Y_M_D_Z[0], - transform: transform_tzaware_datetime_ns, - transform_bytes: StrpTimeState::default(), - fmt_len: 0, - logical_type: DataType::Datetime(TimeUnit::Nanoseconds, None), - }), - } + Pattern::DatetimeYMDZ => (Pattern::DatetimeYMDZ, patterns::DATETIME_Y_M_D_Z), + }; + + Ok(DatetimeInfer { + pattern, + patterns, + latest_fmt: patterns[0], + transform, + transform_bytes: StrpTimeState::default(), + fmt_len: 0, + logical_type: DataType::Datetime(time_unit, None), + }) } }