I had to debug a bit, but @Kevin Wallace was on the right path.
First, @@DateFirst has nothing to do with it. Even if @@datefirst is 1 - as in europe - the code will works, because we are not using any DateTime function relying on start of the week.
There are somehow 2 catchs:
- AX uses an enum [0-6] for [Sunday-Saturday] while SQL use [1-7] for [Monday-Sunday]. So we need to translate AX 0 to SQL 7.
- if there's a DST, the end time in TIMEZONERULESDATA is in DST Time, not in Winter Time.
So here's a working code:
ALTER FUNCTION [dbo].[ConvertUTCDateTime] (@DateTime DATETIME, @TZID INT)
RETURNS DATETIME
AS
BEGIN
-- TZID est 60001 pour la France avec DST actuellement.
DECLARE @AdjustedDateTime DATETIME
SET @AdjustedDateTime=@DateTime
-- From: https://stackoverflow.com/questions/1205612/axapta-dynamicsax-utc-datetime-conversion?noredirect=1&lq=1
-- Modified / Errors corrected by ELB on 05/2022 (in this fonction and in GetDSTDateTime)
-- Fields to be extracted from TIMEZONESRULESDATA record for TZID
DECLARE @Bias INT, @DBias INT
DECLARE @DMonth INT, @DDayOfWeek INT, @DDay INT, @DHour INT, @DMinute INT, @DSecond INT -- Start of Daylight Saving
DECLARE @SMonth INT, @SDayOfWeek INT, @SDay INT, @SHour INT, @SMinute INT, @SSecond INT -- End of Daylight Saving
-- Daylight Saving Date/Time ranges
DECLARE @DSTFromDateTime1 DATETIME, @DSTToDateTime1 DATETIME, @DSTFromDateTime2 DATETIME, @DSTToDateTime2 DATETIME
SELECT
@Bias=tzr.BIAS,@DBias=tzr.DBIAS,
@DMonth=tzr.DMONTH, @DDayOfWeek=tzr.DDAYOFWEEK, @DDay=tzr.DDAY, @DHour=tzr.DHOUR, @DMinute=tzr.DMINUTE, @DSecond=tzr.DSECOND,
@SMonth=tzr.SMONTH, @SDayOfWeek=tzr.SDAYOFWEEK, @SDay=tzr.SDAY, @SHour=tzr.SHOUR, @SMinute=tzr.SMINUTE, @SSecond=tzr.SSECOND
FROM TIMEZONESRULESDATA tzr
WHERE tzr.RULEID=@TZID
IF @Bias IS NOT NULL
BEGIN
SET @AdjustedDateTime=DATEADD(MINUTE, (-1)*@Bias, @DateTime ) -- Standard Time Zone Adjustment from UTC
IF @DMonth>0 -- If there is Daylight Saving
BEGIN
SET @DSTFromDateTime1=dbo.GetDSTDateTime(@AdjustedDateTime, @DMonth, @DDayOfWeek, @DDay, @DHour, @DMinute, @DSecond ) -- Get DS Start date in year
--SET @DSTToDateTime2=dbo.GetDSTDateTime(@AdjustedDateTime, @SMonth, @SDayOfWeek, @SDay, @SHour, @SMinute, @SSecond ) -- Get DS End date in year -- WARNING: Time is in DST !
-- So let's substract (
SET @DSTToDateTime2=DATEADD(MINUTE, @DBias, dbo.GetDSTDateTime(@AdjustedDateTime, @SMonth, @SDayOfWeek, @SDay, @SHour, @SMinute, @SSecond ))
IF @DSTFromDateTime1>@DSTToDateTime2
BEGIN
SET @DSTToDateTime1= DATEADD(SECOND, -1, CAST(DATEFROMPARTS( YEAR(@AdjustedDateTime)+1, 1, 1) as DATETIME)) -- End of Current Year
SET @DSTFromDateTime2= DATEFROMPARTS( YEAR(@AdjustedDateTime), 1, 1) -- Start of Current Year
END
ELSE
BEGIN
SET @DSTToDateTime1=@DSTToDateTime2
SET @DSTFromDateTime2=@DSTFromDateTime1
END
IF @AdjustedDateTime BETWEEN @DSTFromDateTime1 AND @DSTToDateTime1
OR @AdjustedDateTime BETWEEN @DSTFromDateTime2 AND @DSTToDateTime2
SET @AdjustedDateTime=DATEADD(MINUTE, (-1)*@DBias, @AdjustedDateTime ) -- Make Daylight Saving adjustment if in DST date range
END
END
RETURN @AdjustedDateTime
END
and :
ALTER FUNCTION [dbo].[GetDSTDateTime](
@DateTime DATETIME, -- Base Date
@Month INT, -- Month for Start/End of DST
@DayOfWeek INT, -- Day of Week 0=Sun..6=Sat (based on coding in TIMEZONESRULESDATA table)
@Day INT, -- Week of the Month (confusing?!) 1-5 ; 5 means last week
@Hour INT,
@Minute INT,
@Second INT
) RETURNS DATETIME
AS
BEGIN
DECLARE @MyDateTime DATETIME
SET @MyDateTime=DATEFROMPARTS( YEAR(@DateTime), @Month, 1) -- First day of DST Start/End Month from @BaseDate year
--SET @DayOfWeek=@DayOfWeek + 1 -- NOT !! Adjust to tie in with SQL DoW 1-7
if @DayOfWeek=0 set @DayOfWeek=7 -- Ax is [0-6] (0=Sunday,6=Saturday), while SQL is [1-7] [1=Monday, 7=sunday]
--
-- Establish first selected DayOfWeek in the month
IF @DayOfWeek >= DATEPART(WEEKDAY, @MyDateTime )
-- The day we are looking for is in the first week
SET @MyDateTime=DATEADD(DAY, @DayOfWeek - DATEPART(WEEKDAY, @MyDateTime), @MyDateTime)
ELSE
-- the day we are looking for is the second week, so we need to add 1 week and datepart(weekday) -dayofweek is <0
SET @MyDateTime=DATEADD(DAY, 7-(DATEPART(WEEKDAY, @MyDateTime) - @DayOfWeek), @MyDateTime)
-- Add the appropriate number of weeks
SET @MyDateTime=DATEADD(DAY, 7*(@Day-1), @MyDateTime)
-- For last week of month ensure that date is in correct month
WHILE MONTH(@MyDateTime)<>@Month
BEGIN
SET @MyDateTime=DATEADD(DAY, -7, @MyDateTime)
END;
-- Add on Hours, Minutes and Seconds
SET @MyDateTime=DATEADD(SECOND, @Second, DATEADD(MINUTE, @Minute, DATEADD(HOUR, @Hour, @MyDateTime)))
RETURN @MyDateTime