Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Datapart functions to be Timezone Invariant #3226

Merged
Merged
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions contrib/babelfishpg_tsql/runtime/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@ datepart_internal(char* field, Timestamp timestamp, float8 df_tz, bool general_i
Timestamp tsql_first_day, first_day;
struct pg_tm tt1, *tm = &tt1;
uint first_week_end, year, month, day, res = 0, day_of_year; /* for Zeller's Congruence */
int tz1;

/*
* This block is used when the second argument in datepart is not a
Expand All @@ -396,7 +395,7 @@ datepart_internal(char* field, Timestamp timestamp, float8 df_tz, bool general_i
}

/* Gets the date time related fields back from timestamp into struct tm pointer */
if (timestamp2tm(timestamp, &tz1, tm, &fsec1, NULL, NULL) != 0)
if (timestamp2tm(timestamp, NULL, tm, &fsec1, NULL, NULL) != 0)
{
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
Expand Down
3 changes: 3 additions & 0 deletions test/JDBC/expected/datepart-vu-cleanup.out
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ GO

DROP PROCEDURE date_part_vu_prepare_sys_day_proc
GO

DROP TABLE date_part_vu_prepare_TestDates, date_part_vu_prepare_TestTimezones, date_part_vu_prepare_TestResults, date_part_vu_prepare_DateParts;
GO
44 changes: 44 additions & 0 deletions test/JDBC/expected/datepart-vu-prepare.out
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,47 @@ CREATE PROCEDURE date_part_vu_prepare_sys_day_proc @a datetime
AS
SELECT DAY(@a)
GO

-- Test Case for Date Part Functions Timezone Invariance
CREATE TABLE date_part_vu_prepare_DateParts (DatePartName VARCHAR(20));
GO
CREATE TABLE date_part_vu_prepare_TestDates (
TestDateTime DATETIME,
TestDateTimeOffset DATETIMEOFFSET,
TestDateTime2 DATETIME2,
TestSmallDateTime SMALLDATETIME
);
GO
CREATE TABLE date_part_vu_prepare_TestTimezones (TimezoneName VARCHAR(50));
GO
CREATE TABLE date_part_vu_prepare_TestResults (
TestCase VARCHAR(100),
TimeZone VARCHAR(50),
DataType VARCHAR(20),
InputDate VARCHAR(50),
DatePart VARCHAR(20),
DatePartValue SQL_VARIANT,
DateName NVARCHAR(100)
);
GO

-- -- Populate tables
INSERT INTO date_part_vu_prepare_DateParts (DatePartName) VALUES
('year'), ('quarter'), ('month'), ('dayofyear'), ('day'),
('week'), ('weekday'), ('hour'), ('minute'), ('second'),
('millisecond'), ('microsecond'), ('nanosecond'),
('tzoffset'), ('iso_week');
GO
~~ROW COUNT: 15~~

INSERT INTO date_part_vu_prepare_TestDates VALUES
('2025-01-01 05:30:45', '2025-01-01 05:30:45 +00:00', '2025-01-01 05:30:45.1234567', '2025-01-01 05:31:00'),
('2025-06-15 23:59:59', '2025-06-15 23:59:59 +00:00', '2025-06-15 23:59:59.9876543', '2025-06-15 23:59:00');
GO
~~ROW COUNT: 2~~

INSERT INTO date_part_vu_prepare_TestTimezones (TimezoneName) VALUES
('UTC'),('America/New_York'), ('Europe/London'), ('Asia/Tokyo'), ('Africa/Nairobi');
GO
~~ROW COUNT: 5~~

Loading
Loading