Skip to content

Commit cfc65c3

Browse files
Fix AM/PM and timezone parsing for DATETIMEOFFSET (#3775)
The clean_input_str() function, while removing leading spaces, incorrectly removes the space between AM/PM and timezone indicators (e.g., converting "PM -07:00" to "PM-07:00"), causing the subsequent date parser function `ParseDateTime` to fail to recognize the components completely. Modified the space-handling logic in clean_input_str() to preserve spaces before timezone indicators in datetimeoffset context, provided that the time information has already been seen (after encountering colons in the input string). This ensures the timezone offset remains properly separated from the preceding time information, allowing `ParseDateTime` to correctly parse both components. Task: BABEL-5832 Signed-off-by: Manisha Deshpande <mmdeshp@amazon.com>
1 parent 84ad3c2 commit cfc65c3

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

contrib/babelfishpg_common/src/datetime.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,10 @@ clean_input_str(char *str, bool *contains_extra_spaces, DateTimeContext context)
290290
(context != DATE_TIME && (str[i] == '.' || str[i] == '/'
291291
|| str[i] == '+' || str[i] == '-')))
292292
{
293+
/* For datetimeoffset, preserve space before timezone indicator (+/-) */
294+
if (context == DATE_TIME_OFFSET && (str[i] == '+' || str[i] == '-') && num_colons > 0)
295+
result[j++] = ' ';
296+
293297
result[j] = str[i];
294298
j++;
295299
}

test/JDBC/expected/babel_datetimeoffset.out

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,43 @@ datetimeoffset
330330
~~END~~
331331

332332

333+
-- Test AM/PM notation
334+
select CAST('2025-05-20 02:30:20.123456 PM -07:00' as DATETIMEOFFSET);
335+
go
336+
~~START~~
337+
datetimeoffset
338+
2025-05-20 14:30:20.1234560 -07:00
339+
~~END~~
340+
341+
select CAST('2025-05-20 02:30:20.123456 PM-07:0' as DATETIMEOFFSET);
342+
go
343+
~~START~~
344+
datetimeoffset
345+
2025-05-20 14:30:20.1234560 -07:00
346+
~~END~~
347+
348+
select CAST('2025-05- 20 02:30:20.123456 PM -07:00' as DATETIMEOFFSET(4));
349+
go
350+
~~START~~
351+
datetimeoffset
352+
2025-05-20 14:30:20.1235 -07:00
353+
~~END~~
354+
355+
select CAST('2025-05-20 02:30:20.123456 AM +07:59' as DATETIMEOFFSET);
356+
go
357+
~~START~~
358+
datetimeoffset
359+
2025-05-20 02:30:20.1234560 +07:59
360+
~~END~~
361+
362+
-- out of range
363+
select CAST('2025-05-20 02:30:20.123456 AM +07:79' as DATETIMEOFFSET);
364+
go
365+
~~ERROR (Code: 33557097)~~
366+
367+
~~ERROR (Message: time zone displacement out of range: "2025-05-20 02:30:20.123456 AM +07:79")~~
368+
369+
333370
-- Test type cast to/from other time formats
334371
-- Test datetime/dateime2
335372
select CAST(CAST('2020-03-15 23:59:29.99' AS datetime) AS datetimeoffset);

test/JDBC/input/babel_datetimeoffset.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ go
110110
select CAST('2079-06-06 23:59:29.123456 -9:30' AS datetimeoffset(7));
111111
go
112112

113+
-- Test AM/PM notation
114+
select CAST('2025-05-20 02:30:20.123456 PM -07:00' as DATETIMEOFFSET);
115+
go
116+
select CAST('2025-05-20 02:30:20.123456 PM-07:0' as DATETIMEOFFSET);
117+
go
118+
select CAST('2025-05- 20 02:30:20.123456 PM -07:00' as DATETIMEOFFSET(4));
119+
go
120+
select CAST('2025-05-20 02:30:20.123456 AM +07:59' as DATETIMEOFFSET);
121+
go
122+
-- out of range
123+
select CAST('2025-05-20 02:30:20.123456 AM +07:79' as DATETIMEOFFSET);
124+
go
125+
113126
-- Test type cast to/from other time formats
114127
-- Test datetime/dateime2
115128
select CAST(CAST('2020-03-15 23:59:29.99' AS datetime) AS datetimeoffset);

0 commit comments

Comments
 (0)