Skip to content

Commit

Permalink
Merge pull request #368 from Inxton/203-bug-days-in-leap-year-are-not…
Browse files Browse the repository at this point in the history
…-correctly-retrieved-from-the-controller

[BUG] Days in leap year are not correctly retrieved from the controller
  • Loading branch information
PTKu authored Jan 24, 2025
2 parents 1ea5456 + c5e1446 commit a907ddd
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private DateOnly GetFromBinary(long value)

case eTargetProjectPlatform.SIMATICAX:
var valAx = value / 100;
return valAx.AdjustForLeapDate();
return valAx.GetDateOnly();

default:
var valdef = value / 100;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private DateTime GetFromBinary(string value)
private DateTime GetFromBinary(long val)
{
var dt = val / 100;
return dt.AdjustForLeapDateTime(); // DateTime.FromBinary(dt).AddYears(1969);
return dt.ToUtcDateTime(); // DateTime.FromBinary(dt).AddYears(1969);
}

private string GetFromDate(DateTime dateTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private DateOnly GetFromBinary(string value)
private DateOnly GetFromBinary(long value)
{
var val = value / 100;
return val.AdjustForLeapDate(); // DateOnly.FromDateTime(DateTime.FromBinary(val).AddYears(1969));
return val.GetDateOnly(); // DateOnly.FromDateTime(DateTime.FromBinary(val).AddYears(1969));
}

private string GetFromDate(DateOnly date)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private DateTime GetFromBinary(string value)
private DateTime GetFromBinary(long val)
{
var dt = val / 100;
return dt.AdjustForLeapDateTime(); // DateTime.FromBinary(dt).AddYears(1969);
return dt.ToUtcDateTime(); // DateTime.FromBinary(dt).AddYears(1969);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,19 @@ public static ConnectorAdapter CreateWebApi(this ConnectorAdapterBuilder adapter
{ Parameters = new object[] { ipAddress, userName, password, customServerCertHandler, ignoreSslErrors, platform, dbName } };
}

public static DateOnly AdjustForLeapDate(this long value)
{
var noLeap = DateOnly.FromDateTime(DateTime.FromBinary(value).AddYears(1969));
var leapDays = DateTime.IsLeapYear(noLeap.Year) && ((noLeap.Month == 2 && noLeap.Day == 29) || noLeap.Month >= 3) ? -1 : 0;
return noLeap.AddDays(leapDays);
public static DateOnly GetDateOnly(this long value)
{
// 1 tick = 100 ns
// Use DateTimeKind.Utc for correct interpretation
var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
.AddTicks(value);

// Return the date portion in UTC
return DateOnly.FromDateTime(dateTime.ToUniversalTime());
}

public static DateTime AdjustForLeapDateTime(this long value)
public static DateTime ToUtcDateTime(this long value)
{
var noLeap = DateTime.FromBinary(value).AddYears(1969);
var leapDays = DateTime.IsLeapYear(noLeap.Year) && ((noLeap.Month == 2 && noLeap.Day == 29) || noLeap.Month >= 3) ? -1 : 0;
return noLeap.AddDays(leapDays);
return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddTicks(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,16 @@ public WebApiDateTests(ITestOutputHelper output) : base(output)
[Fact]
public virtual async void should_synchron_write_leap_value_check_leap()
{
var testDate = new DateOnly(2025, 1, 1);
for (int i = 0; i < 100; i++)
var testDate = new DateOnly(2024, 1, 1);
for (int i = 0; i < 365*5; i++)
{
testDate = testDate.AddDays(1);
TestConnector.TestApiConnector.ClearPeriodicReadSet();
await webApiPrimitive!.SetAsync(testDate);
var actual = await webApiPrimitive.GetAsync();
if(testDate != actual)
Output.WriteLine($"Expected: {testDate} - Actual: {actual} {testDate == actual}");
// Assert.Equal(testDate, actual);
Assert.Equal(testDate, actual);
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/AXSharp.connectors/tests/ax-test-project/ax-test-project.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.2.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ax_test_project", "ix\ax_test_project.csproj", "{C821E27A-DD87-A1A7-0B15-842E89FDA2A2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C821E27A-DD87-A1A7-0B15-842E89FDA2A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C821E27A-DD87-A1A7-0B15-842E89FDA2A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C821E27A-DD87-A1A7-0B15-842E89FDA2A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C821E27A-DD87-A1A7-0B15-842E89FDA2A2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {550C4796-5273-4560-BB73-CED0252EDF8C}
EndGlobalSection
EndGlobal

0 comments on commit a907ddd

Please sign in to comment.