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

[BUG] Leap year has strange behavior #234

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,19 @@ private DateOnly GetFromBinary(string value)
private DateOnly GetFromBinary(long value)
{
var val = value / 100;
return DateOnly.FromDateTime(DateTime.FromBinary(val).AddYears(1969));
var pd = DateOnly.FromDateTime(DateTime.FromBinary(val).AddYears(1969));

// temporary and not full fix for leap years (27.february and 28.february returns same value in year 2001)
// TODO (should be deleted)
// if is a leap year and is after a leap day, correct date
// if are first 2 months after leap year, correct date
if ((DateTime.IsLeapYear(pd.Year) && pd.Month > 2) ||
((DateTime.IsLeapYear(pd.Year - 1) && pd.Month < 3)))
{
pd = pd.AddDays(-1);
}

return pd;
}

private string GetFromDate(DateOnly date)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,18 @@ private DateTime GetFromBinary(string value)
private DateTime GetFromBinary(long val)
{
var dt = val / 100;
return DateTime.FromBinary(dt).AddYears(1969);
var pd = DateTime.FromBinary(dt).AddYears(1969);
// temporary and not full fix for leap years (27.february and 28.february returns same value in year 2001)
// TODO (should be deleted)
// if is a leap year and is after a leap day, correct date
// if are first 2 months after leap year, correct date
if ((DateTime.IsLeapYear(pd.Year) && pd.Month > 2) ||
((DateTime.IsLeapYear(pd.Year - 1) && pd.Month < 3)))
{
pd = pd.AddDays(-1);
}

return pd;
}

private string GetFromDate(DateTime dateTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,18 @@ private DateOnly GetFromBinary(string value)
private DateOnly GetFromBinary(long value)
{
var val = value / 100;
return DateOnly.FromDateTime(DateTime.FromBinary(val).AddYears(1969));
var pd = DateOnly.FromDateTime(DateTime.FromBinary(val).AddYears(1969));
// temporary and not full fix for leap years (27.february and 28.february returns same value in year 2001)
// TODO (should be deleted)
// if is a leap year and is after a leap day, correct date
// if are first 2 months after leap year, correct date
if ((DateTime.IsLeapYear(pd.Year) && pd.Month > 2) ||
((DateTime.IsLeapYear(pd.Year - 1) && pd.Month < 3)))
{
pd = pd.AddDays(-1);
}

return pd;
}

private string GetFromDate(DateOnly date)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,18 @@ private DateTime GetFromBinary(string value)
private DateTime GetFromBinary(long val)
{
var dt = val / 100;
return DateTime.FromBinary(dt).AddYears(1969);
var pd = DateTime.FromBinary(dt).AddYears(1969);
// temporary and not full fix for leap years (27.february and 28.february returns same value in year 2001)
// TODO (should be deleted)
// if is a leap year and is after a leap day, correct date
// if are first 2 months after leap year, correct date
if ((DateTime.IsLeapYear(pd.Year) && pd.Month > 2) ||
((DateTime.IsLeapYear(pd.Year - 1) && pd.Month < 3)))
{
pd = pd.AddDays(-1);
}

return pd;
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using AXSharp.Connector.S71500.WebApi;
using AXSharp.Connector.S71500.WebAPITests.Primitives;
using AXSharp.Connector.ValueTypes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace AXSharp.Connector.S71500.WebAPITests.issues
{
public class AX_Sharp_203
{
private readonly ITestOutputHelper report;
protected static WebApiConnector Connector { get; } = TestConnector.TestApiConnector as WebApiConnector;
public AX_Sharp_203(ITestOutputHelper output)
{
report = output;
TestConnector.TestApiConnector.ReadWriteCycleDelay = 2;
}

[Fact()]
//if we have a date in leap year after february, not correct date is provided from webapi
public async Task reproduction_leap_year()
{
var expected = new DateOnly(2000, 3, 20);
var baseTypeMember = new WebApiDate(Connector, "", $"myLeapDATE");
var readDate = await baseTypeMember.GetAsync();


report.WriteLine(readDate.ToString());

Assert.Equal(expected, readDate);
}


[Fact()]
//if we have a date after leap year before february, not correct date is provided from webapi
public async Task reproduction_after_leap_year()
{
var expected = new DateOnly(2001,2, 10);
var baseTypeMember = new WebApiDate(Connector, "", $"myAfterLeapDATE");
var readDate = await baseTypeMember.GetAsync();


report.WriteLine(readDate.ToString());
Assert.Equal(expected, readDate);
}

[Fact()]
//dates should be different, but are same
public async Task reproduction_leap_year_edge_case()
{
var leap1 = new WebApiDate(Connector, "", $"myEdgeCaseLeapDATE1");
var leap2 = new WebApiDate(Connector, "", $"myEdgeCaseLeapDATE2");
var readLeap1 = await leap1.GetAsync();
var readLeap2 = await leap2.GetAsync();

report.WriteLine($"Expected : 02-27-2001 Actual: {readLeap1.ToString()}");
report.WriteLine($"Expected : 02-28-2001 Actual: {readLeap2.ToString()}");

// are same, but should be different
Assert.Equal(readLeap1, readLeap2);
}

[Fact()]
//last date in december is within leap year read as a new year
public async Task reproduction_leap_end_year()
{
var expected = new DateOnly(2000, 12, 31);
var leapEnd = new WebApiDate(Connector, "", $"myLeapEndDATE");
var readLeap = await leapEnd.GetAsync();

report.WriteLine($"Expected : 12-31-2000 Actual: {readLeap.ToString()}");
Assert.Equal(expected, readLeap);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,11 @@ CONFIGURATION MyConfiguration
// GH_PKTu_ix_56_FirstInheritance : GH.PKTu.ix_56.FirstInheritance;

GH_PKTu_ix_56_SecondInheritance : GH.PKTu.ix_56.SecondInheritance;

myLeapDATE : DATE;
myAfterLeapDATE : DATE;
myEdgeCaseLeapDATE1 : DATE;
myEdgeCaseLeapDATE2 : DATE;
myLeapEndDATE : DATE;
END_VAR
END_CONFIGURATION
15 changes: 12 additions & 3 deletions src/AXSharp.connectors/tests/ax-test-project/src/program.st
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ PROGRAM MyProgram
myLREAL : LREAL ;
myTIME : TIME ;
myLTIME : LTIME ;
myDATE : DATE ;
myDATE : DATE ;
myLDATE : LDATE ;
myTIME_OF_DAY : TIME_OF_DAY ;
myLTIME_OF_DAY : LTIME_OF_DAY ;
Expand All @@ -41,7 +41,11 @@ PROGRAM MyProgram

Hierarchy : hierarchy;


myLeapDATE : DATE;
myAfterLeapDATE : DATE;
myEdgeCaseLeapDATE1 : DATE;
myEdgeCaseLeapDATE2 : DATE;
myLeapEndDATE : DATE;
//myArrayString: ARRAY[0..10] OF STRING ;
//myArrayComplex: ARRAY[0..10] OF complexArrayItem;

Expand All @@ -60,7 +64,12 @@ PROGRAM MyProgram



//myDATE := DATE#1970-01-01;
myLeapDATE := DATE#2000-03-20;
myLeapEndDATE := DATE#2000-12-31;
myAfterLeapDATE := DATE#2001-02-10;
myEdgeCaseLeapDATE1 := DATE#2001-02-27;
myEdgeCaseLeapDATE2 := DATE#2001-02-28;

//myColors := Colors#GREEN;
//mins.myBOOL := false ;
mins.myBYTE := BYTE#2#0 ;
Expand Down