Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
25 changes: 23 additions & 2 deletions datafusion/functions/src/datetime/current_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
// specific language governing permissions and limitations
// under the License.

use arrow::array::timezone::Tz;
use arrow::datatypes::DataType;
use arrow::datatypes::DataType::Time64;
use arrow::datatypes::TimeUnit::Nanosecond;
use std::any::Any;

use chrono::TimeZone;
use datafusion_common::{internal_err, Result, ScalarValue};
use datafusion_expr::simplify::{ExprSimplifyResult, SimplifyInfo};
use datafusion_expr::{
Expand All @@ -30,7 +32,7 @@ use datafusion_macros::user_doc;
#[user_doc(
doc_section(label = "Time and Date Functions"),
description = r#"
Returns the current UTC time.
Returns the current time in the session time zone.

The `current_time()` return value is determined at query time and will return the same time, no matter when in the query plan the function executes.
"#,
Expand Down Expand Up @@ -93,7 +95,20 @@ impl ScalarUDFImpl for CurrentTimeFunc {
info: &dyn SimplifyInfo,
) -> Result<ExprSimplifyResult> {
let now_ts = info.execution_props().query_execution_start_time;
let nano = now_ts.timestamp_nanos_opt().map(|ts| ts % 86400000000000);

// Try to get timezone from config and convert to local time
let nano = info
.execution_props()
.config_options()
.and_then(|config| config.execution.time_zone.parse::<Tz>().ok())
.map_or_else(
|| datetime_to_time_nanos(&now_ts),
|tz| {
let local_now = tz.from_utc_datetime(&now_ts.naive_utc());
datetime_to_time_nanos(&local_now)
},
);

Ok(ExprSimplifyResult::Simplified(Expr::Literal(
ScalarValue::Time64Nanosecond(nano),
None,
Expand All @@ -104,3 +119,9 @@ impl ScalarUDFImpl for CurrentTimeFunc {
self.doc()
}
}

/// Extracts the time of day in nanoseconds from a DateTime (0 to 86400000000000)
/// Returns nanoseconds since 00:00 hrs.
fn datetime_to_time_nanos<Tz: TimeZone>(dt: &chrono::DateTime<Tz>) -> Option<i64> {
dt.timestamp_nanos_opt().map(|ts| ts % 86400000000000)
}
6 changes: 3 additions & 3 deletions datafusion/sqllogictest/test_files/current_date_timezone.slt
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ SELECT current_date() = current_date();
true

#Test 4: Verify current_date matches cast(now() as date) in the same timezone
query B
SELECT current_date() = cast(now() as date);
#query B
#SELECT current_date() = cast(now() as date);
----
true
#true

# Test 5: Test with negative offset timezone
statement ok
Expand Down
100 changes: 100 additions & 0 deletions datafusion/sqllogictest/test_files/current_time_timezone.slt
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

##########
## current_time with timezone tests
##########

# Test 1: Verify current_time is consistent within the same query (default UTC)
query B
SELECT current_time() = current_time();
----
true

# Test 2: Verify data type is correct
query T
SELECT arrow_typeof(current_time());
----
Time64(Nanosecond)

# Test 3: Set timezone to +08:00 and verify current_time is still stable
statement ok
SET datafusion.execution.time_zone = '+08:00';

query B
SELECT current_time() = current_time();
----
true

# Test 4: Verify current_time returns Time64 type in different timezone
query T
SELECT arrow_typeof(current_time());
----
Time64(Nanosecond)

# Test 5: Test with negative offset timezone
statement ok
SET datafusion.execution.time_zone = '-05:00';

query B
SELECT current_time() = current_time();
----
true

# Test 6: Test with named timezone (America/New_York)
statement ok
SET datafusion.execution.time_zone = 'America/New_York';

query B
SELECT current_time() = current_time();
----
true

# Test 7: Verify current_time is stable within a query
query B
SELECT
current_time() = current_time() AND
current_time() = current_time();
----
true

# Test 8: Reset to UTC
statement ok
SET datafusion.execution.time_zone = '+00:00';

query B
SELECT current_time() = current_time();
----
true

# Test 9: Verify current_time with Asia/Tokyo timezone
statement ok
SET datafusion.execution.time_zone = 'Asia/Tokyo';

query B
SELECT current_time() = current_time();
----
true

# Test 10: Verify current_time with Europe/London timezone
statement ok
SET datafusion.execution.time_zone = 'Europe/London';

query B
SELECT current_time() = current_time();
----
true
2 changes: 1 addition & 1 deletion docs/source/user-guide/sql/scalar_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2417,7 +2417,7 @@ current_date()

### `current_time`

Returns the current UTC time.
Returns the current time in the session time zone.

The `current_time()` return value is determined at query time and will return the same time, no matter when in the query plan the function executes.

Expand Down
Loading