Skip to content

Commit 5c4eaf7

Browse files
committed
Updates from code review.
1 parent 7c00e07 commit 5c4eaf7

5 files changed

Lines changed: 20 additions & 17 deletions

File tree

datafusion/functions/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ workspace = true
4040
[features]
4141
crypto_expressions = ["md-5", "sha2", "blake2", "blake3"]
4242
# enable datetime functions
43-
datetime_expressions = []
43+
datetime_expressions = ["chrono-tz"]
4444
# Enable encoding by default so the doctests work. In general don't automatically enable all packages.
4545
default = [
4646
"datetime_expressions",
@@ -71,7 +71,7 @@ base64 = { version = "0.22", optional = true }
7171
blake2 = { version = "^0.10.2", optional = true }
7272
blake3 = { version = "1.8", optional = true }
7373
chrono = { workspace = true }
74-
chrono-tz = "0.10.4"
74+
chrono-tz = { version = "0.10.4", optional = true }
7575
datafusion-common = { workspace = true }
7676
datafusion-doc = { workspace = true }
7777
datafusion-execution = { workspace = true }

datafusion/functions/src/datetime/common.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,23 @@ pub(crate) fn string_to_datetime_formatted<T: TimeZone>(
116116
// custom parser (or switching to Jiff)
117117
let tz: Option<chrono_tz::Tz> = if format.ends_with(" %Z") {
118118
// grab the string after the last space as the named timezone
119-
let parts: Vec<&str> = datetime_str.rsplitn(2, ' ').collect();
120-
let timezone_name = parts[0];
121-
datetime_str = parts[1];
122-
123-
// attempt to parse the timezone name
124-
let result: Result<chrono_tz::Tz, chrono_tz::ParseError> = timezone_name.parse();
125-
let Ok(tz) = result else {
126-
return Err(err(&result.unwrap_err().to_string()));
127-
};
119+
if let Some((dt_str, timezone_name)) = datetime_str.rsplit_once(' ') {
120+
datetime_str = dt_str;
121+
122+
// attempt to parse the timezone name
123+
let result: Result<chrono_tz::Tz, chrono_tz::ParseError> =
124+
timezone_name.parse();
125+
let Ok(tz) = result else {
126+
return Err(err(&result.unwrap_err().to_string()));
127+
};
128128

129-
// successfully parsed the timezone name, remove the ' %Z' from the format
130-
format = format.trim_end_matches(" %Z");
129+
// successfully parsed the timezone name, remove the ' %Z' from the format
130+
format = &format[..format.len() - 3];
131131

132-
Some(tz)
132+
Some(tz)
133+
} else {
134+
None
135+
}
133136
} else if format.contains("%Z") {
134137
return Err(err(
135138
"'%Z' is only supported at the end of the format string preceded by a space",

datafusion/functions/src/datetime/to_timestamp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ pub struct ToTimestampMicrosFunc {
239239
#[user_doc(
240240
doc_section(label = "Time and Date Functions"),
241241
description = r#"
242-
Converts a value to a timestamp (`YYYY-MM-DDT00:00:00.000000000Z<TZ>`) in the session time zone. Supports strings,
242+
Converts a value to a timestamp (`YYYY-MM-DDT00:00:00.000000000<TZ>`) in the session time zone. Supports strings,
243243
integer, unsigned integer, and double types as input. Strings are parsed as RFC3339 (e.g. '2023-07-20T05:44:00')
244244
if no [Chrono formats] are provided. Strings that parse without a time zone are treated as if they are in the
245245
session time zone. Integers, unsigned integers, and doubles are interpreted as nanoseconds since the unix epoch (`1970-01-01T00:00:00Z`).

datafusion/sqllogictest/test_files/to_timestamp_timezone.slt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ SET datafusion.execution.time_zone = 'America/New_York';
6363

6464
## Test 7: Naive timestamp with configured timezone
6565
## '2020-09-08T13:42:29' in America/New_York is EDT (UTC-4)
66-
## So this should become '2020-09-08T13:42:29Z-04:00'
66+
## So this should become '2020-09-08T13:42:29-04:00'
6767
query P
6868
SELECT to_timestamp('2020-09-08T13:42:29');
6969
----

docs/source/user-guide/sql/scalar_functions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2909,7 +2909,7 @@ Additional examples can be found [here](https://github.com/apache/datafusion/blo
29092909

29102910
### `to_timestamp_nanos`
29112911

2912-
Converts a value to a timestamp (`YYYY-MM-DDT00:00:00.000000000Z<TZ>`) in the session time zone. Supports strings,
2912+
Converts a value to a timestamp (`YYYY-MM-DDT00:00:00.000000000<TZ>`) in the session time zone. Supports strings,
29132913
integer, unsigned integer, and double types as input. Strings are parsed as RFC3339 (e.g. '2023-07-20T05:44:00')
29142914
if no [Chrono formats] are provided. Strings that parse without a time zone are treated as if they are in the
29152915
session time zone. Integers, unsigned integers, and doubles are interpreted as nanoseconds since the unix epoch (`1970-01-01T00:00:00Z`).

0 commit comments

Comments
 (0)