Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 11 additions & 24 deletions datafusion/functions/src/datetime/date_part.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ use arrow::datatypes::DataType::{
use arrow::datatypes::TimeUnit::{Microsecond, Millisecond, Nanosecond, Second};
use arrow::datatypes::{
DataType, Date32Type, Date64Type, Field, FieldRef, IntervalUnit as ArrowIntervalUnit,
TimeUnit,
TimeUnit, TimestampMicrosecondType, TimestampMillisecondType,
TimestampNanosecondType, TimestampSecondType,
};
use chrono::{Datelike, NaiveDate, TimeZone, Utc};
use chrono::{Datelike, NaiveDate};
use datafusion_common::types::{NativeType, logical_date};

use datafusion_common::{
Expand Down Expand Up @@ -328,37 +329,23 @@ fn date_to_scalar(date: NaiveDate, target_type: &DataType) -> Option<ScalarValue

Timestamp(unit, tz_opt) => {
let naive_midnight = date.and_hms_opt(0, 0, 0)?;

let utc_dt = if let Some(tz_str) = tz_opt {
let tz: Tz = tz_str.parse().ok()?;

let local = tz.from_local_datetime(&naive_midnight);

let local_dt = match local {
chrono::offset::LocalResult::Single(dt) => dt,
chrono::offset::LocalResult::Ambiguous(dt1, _dt2) => dt1,
chrono::offset::LocalResult::None => local.earliest()?,
};

local_dt.with_timezone(&Utc)
} else {
Utc.from_utc_datetime(&naive_midnight)
};
let tz: Option<Tz> = tz_opt.and_then(|s| s.parse().ok());

match unit {
Second => {
ScalarValue::TimestampSecond(Some(utc_dt.timestamp()), tz_opt.clone())
}
Second => ScalarValue::TimestampSecond(
TimestampSecondType::from_naive_datetime(naive_midnight, tz_opt),
tz_opt.clone(),
),
Millisecond => ScalarValue::TimestampMillisecond(
Some(utc_dt.timestamp_millis()),
TimestampMillisecondType::from_naive_datetime(naive_midnight, tz_opt),
tz_opt.clone(),
),
Microsecond => ScalarValue::TimestampMicrosecond(
Some(utc_dt.timestamp_micros()),
TimestampMicrosecondType::from_naive_datetime(naive_midnight, tz_opt),
tz_opt.clone(),
),
Nanosecond => ScalarValue::TimestampNanosecond(
Some(utc_dt.timestamp_nanos_opt()?),
TimestampNanosecondType::from_naive_datetime(naive_midnight, tz_opt),
tz_opt.clone(),
),
}
Expand Down
Loading