Skip to content

Commit

Permalink
[BUG] When reading Date related value past first leap day we get wron…
Browse files Browse the repository at this point in the history
…g value (#290)

* Create draft PR for #285

* addresses an issue with leap days

* Create draft PR for #289

---------

Co-authored-by: PTKu <61538034+PTKu@users.noreply.github.com>
  • Loading branch information
IX-BOT and PTKu authored Mar 6, 2024
1 parent facb919 commit 79b10c6
Show file tree
Hide file tree
Showing 8 changed files with 358 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ private DateOnly GetFromBinary(long value)

case eTargetProjectPlatform.SIMATICAX:
var valAx = value / 100;
return DateOnly.FromDateTime(DateTime.FromBinary(valAx).AddYears(1969));

return valAx.AdjustForLeapDate();
default:
var valdef = value / 100;
return DateOnly.FromDateTime(DateTime.FromBinary(valdef).AddYears(1969));
Expand All @@ -128,8 +128,7 @@ private string GetFromDate(DateOnly date)
{
if (date <= MinValue)
date = MinValue;

var retval = date.ToDateTime(TimeOnly.MinValue) - MinValue.ToDateTime(TimeOnly.MinValue);
var retval = date.ToDateTime(TimeOnly.MinValue) - MinValue.ToDateTime(TimeOnly.MinValue);
return (new DateTime().AddDays(retval.TotalDays).ToBinary() * 100).ToString();
}

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 DateTime.FromBinary(dt).AddYears(1969);
return dt.AdjustForLeapDateTime(); // 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 DateOnly.FromDateTime(DateTime.FromBinary(val).AddYears(1969));
return val.AdjustForLeapDate(); // 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 DateTime.FromBinary(dt).AddYears(1969);
return dt.AdjustForLeapDateTime(); // DateTime.FromBinary(dt).AddYears(1969);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,18 @@ public static ConnectorAdapter CreateWebApi(this ConnectorAdapterBuilder adapter
return new ConnectorAdapter(typeof(WebApiConnectorFactory))
{ Parameters = new object[] { ipAddress, userName, password, customServerCertHandler, 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 DateTime AdjustForLeapDateTime(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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,80 @@ public class WebApiDateTests : WebApiPrimitiveTests<WebApiDate, DateOnly>
protected override DateOnly Max { get; } = WebApiDate.MaxValue;
protected override DateOnly Mid { get; } = DateOnly.FromDateTime(DateTime.Today);
protected override DateOnly Min { get; } = WebApiDate.MinValue;

[Fact]
public virtual async void should_synchron_write_leap_value()
{
var expected = new DateOnly(2024, 3, 4);
TestConnector.TestApiConnector.ClearPeriodicReadSet();
await webApiPrimitive!.SetAsync(expected);
Assert.Equal(expected, await webApiPrimitive.GetAsync());
}

[Fact]
public virtual async void should_synchron_write_no_leap_value()
{
var expected = new DateOnly(2023, 3, 4);
TestConnector.TestApiConnector.ClearPeriodicReadSet();
await webApiPrimitive!.SetAsync(expected);
Assert.Equal(expected, await webApiPrimitive.GetAsync());
}

[Fact]
public virtual async void should_synchron_write_first_leap_day_value()
{
var expected = new DateOnly(2024, 2, 29);
TestConnector.TestApiConnector.ClearPeriodicReadSet();
await webApiPrimitive!.SetAsync(expected);
Assert.Equal(expected, await webApiPrimitive.GetAsync());
}

[Fact]
public virtual async void should_write_cyclic_leap_value()
{
var expected = new DateOnly(2024, 3, 4);
TestConnector.TestApiConnector.ClearPeriodicReadSet();
await webApiPrimitive.SetAsync(Max);
webApiPrimitive!.Cyclic = expected;
webApiPrimitive!.AddToPeriodicQueue();
await Task.Delay(WaitTimeForCyclicOperations);
Assert.Equal(expected, webApiPrimitive.Cyclic);
Assert.Equal(expected, await webApiPrimitive.GetAsync());
}


[Fact]
public virtual async void should_write_cyclic_no_leap_value()
{
var expected = new DateOnly(2023, 3, 4);
TestConnector.TestApiConnector.ClearPeriodicReadSet();
await webApiPrimitive.SetAsync(Max);
webApiPrimitive!.Cyclic = expected;
webApiPrimitive!.AddToPeriodicQueue();
await Task.Delay(WaitTimeForCyclicOperations);
Assert.Equal(expected, webApiPrimitive.Cyclic);
Assert.Equal(expected, await webApiPrimitive.GetAsync());
}

[Fact]
public virtual async void should_synchron_read_first_leap_day_value()
{
var primitive = Activator.CreateInstance(typeof(WebApiDate), Connector, "", "myDATE_leap_febr") as WebApiDate;
var expected = new DateOnly(2024, 2, 29);
TestConnector.TestApiConnector.ClearPeriodicReadSet();

Assert.Equal(expected, await primitive.GetAsync());
}

[Fact]
public virtual async void should_synchron_read_leap_day_value()
{
var primitive = Activator.CreateInstance(typeof(WebApiDate), Connector, "", "myDATE_leap_late") as WebApiDate;
var expected = new DateOnly(2024, 3, 5);
TestConnector.TestApiConnector.ClearPeriodicReadSet();

Assert.Equal(expected, await primitive.GetAsync());
}
}

public class WebApiDateTimeTest : WebApiPrimitiveTests<WebApiDateTime, DateTime>
Expand All @@ -175,6 +249,80 @@ public class WebApiDateTimeTest : WebApiPrimitiveTests<WebApiDateTime, DateTime>
protected override DateTime Max { get; } = WebApiDateTime.MaxValue;
protected override DateTime Mid { get; } = DateTime.Today;
protected override DateTime Min { get; } = WebApiDateTime.MinValue;

[Fact]
public virtual async void should_synchron_write_leap_value()
{
var expected = new DateTime(2024, 3, 4);
TestConnector.TestApiConnector.ClearPeriodicReadSet();
await webApiPrimitive!.SetAsync(expected);
Assert.Equal(expected, await webApiPrimitive.GetAsync());
}

[Fact]
public virtual async void should_synchron_write_no_leap_value()
{
var expected = new DateTime(2023, 3, 4);
TestConnector.TestApiConnector.ClearPeriodicReadSet();
await webApiPrimitive!.SetAsync(expected);
Assert.Equal(expected, await webApiPrimitive.GetAsync());
}

[Fact]
public virtual async void should_synchron_write_first_leap_day_value()
{
var expected = new DateTime(2024, 2, 29);
TestConnector.TestApiConnector.ClearPeriodicReadSet();
await webApiPrimitive!.SetAsync(expected);
Assert.Equal(expected, await webApiPrimitive.GetAsync());
}

[Fact]
public virtual async void should_write_cyclic_leap_value()
{
var expected = new DateTime(2024, 3, 4);
TestConnector.TestApiConnector.ClearPeriodicReadSet();
await webApiPrimitive.SetAsync(Max);
webApiPrimitive!.Cyclic = expected;
webApiPrimitive!.AddToPeriodicQueue();
await Task.Delay(WaitTimeForCyclicOperations);
Assert.Equal(expected, webApiPrimitive.Cyclic);
Assert.Equal(expected, await webApiPrimitive.GetAsync());
}


[Fact]
public virtual async void should_write_cyclic_no_leap_value()
{
var expected = new DateTime(2023, 3, 4);
TestConnector.TestApiConnector.ClearPeriodicReadSet();
await webApiPrimitive.SetAsync(Max);
webApiPrimitive!.Cyclic = expected;
webApiPrimitive!.AddToPeriodicQueue();
await Task.Delay(WaitTimeForCyclicOperations);
Assert.Equal(expected, webApiPrimitive.Cyclic);
Assert.Equal(expected, await webApiPrimitive.GetAsync());
}

[Fact]
public virtual async void should_synchron_read_first_leap_day_value()
{
var primitive = Activator.CreateInstance(typeof(WebApiDateTime), Connector, "", "myDATE_AND_TIME_leap_febr") as WebApiDateTime;
var expected = new DateTime(2024, 2, 29);
TestConnector.TestApiConnector.ClearPeriodicReadSet();

Assert.Equal(expected, await primitive.GetAsync());
}

[Fact]
public virtual async void should_synchron_read_leap_day_value()
{
var primitive = Activator.CreateInstance(typeof(WebApiDateTime), Connector, "", "myDATE_AND_TIME_leap_late") as WebApiDateTime;
var expected = new DateTime(2024, 3, 5);
TestConnector.TestApiConnector.ClearPeriodicReadSet();

Assert.Equal(expected, await primitive.GetAsync());
}
}

public class WebApiDIntTest : WebApiPrimitiveTests<WebApiDInt, int>
Expand Down Expand Up @@ -489,6 +637,83 @@ public class WebApiLDateTests : WebApiPrimitiveTests<WebApiLDate, DateOnly>
protected override DateOnly Max { get; } = WebApiLDate.MaxValue;
protected override DateOnly Mid { get; } = DateOnly.FromDateTime(DateTime.Today);
protected override DateOnly Min { get; } = WebApiLDate.MinValue;

[Fact]
public virtual async void should_synchron_write_leap_value()
{
var expected = new DateOnly(2024, 3, 4);
TestConnector.TestApiConnector.ClearPeriodicReadSet();
await webApiPrimitive!.SetAsync(expected);
Assert.Equal(expected, await webApiPrimitive.GetAsync());
}

[Fact]
public virtual async void should_synchron_write_no_leap_value()
{
var expected = new DateOnly(2023, 3, 4);
TestConnector.TestApiConnector.ClearPeriodicReadSet();
await webApiPrimitive!.SetAsync(expected);
Assert.Equal(expected, await webApiPrimitive.GetAsync());
}

[Fact]
public virtual async void should_synchron_write_first_leap_day_value()
{
var expected = new DateOnly(2024, 2, 29);
TestConnector.TestApiConnector.ClearPeriodicReadSet();
await webApiPrimitive!.SetAsync(expected);
Assert.Equal(expected, await webApiPrimitive.GetAsync());
}

[Fact]
public virtual async void should_write_cyclic_leap_value()
{
var expected = new DateOnly(2024, 3, 4);
TestConnector.TestApiConnector.ClearPeriodicReadSet();
await webApiPrimitive.SetAsync(Max);
webApiPrimitive!.Cyclic = expected;
webApiPrimitive!.AddToPeriodicQueue();
await Task.Delay(WaitTimeForCyclicOperations);
Assert.Equal(expected, webApiPrimitive.Cyclic);
Assert.Equal(expected, await webApiPrimitive.GetAsync());
}


[Fact]
public virtual async void should_write_cyclic_no_leap_value()
{
var expected = new DateOnly(2023, 3, 4);
TestConnector.TestApiConnector.ClearPeriodicReadSet();
await webApiPrimitive.SetAsync(Max);
webApiPrimitive!.Cyclic = expected;
webApiPrimitive!.AddToPeriodicQueue();
await Task.Delay(WaitTimeForCyclicOperations);
Assert.Equal(expected, webApiPrimitive.Cyclic);
Assert.Equal(expected, await webApiPrimitive.GetAsync());
}

[Fact]
public virtual async void should_synchron_read_first_leap_day_value()
{
var primitive = Activator.CreateInstance(typeof(WebApiDate), Connector, "", "myLDATE_leap_febr") as WebApiDate;
var expected = new DateOnly(2024, 2, 29);
TestConnector.TestApiConnector.ClearPeriodicReadSet();

Assert.Equal(expected, await primitive.GetAsync());
}

[Fact]
public virtual async void should_synchron_read_leap_day_value()
{
var primitive = Activator.CreateInstance(typeof(WebApiDate), Connector, "", "myLDATE_leap_late") as WebApiDate;
var expected = new DateOnly(2024, 3, 5);
TestConnector.TestApiConnector.ClearPeriodicReadSet();

Assert.Equal(expected, await primitive.GetAsync());
}



}

public class WebApiLDateTimeTest : WebApiPrimitiveTests<WebApiLDateTime, DateTime>
Expand All @@ -497,6 +722,81 @@ public class WebApiLDateTimeTest : WebApiPrimitiveTests<WebApiLDateTime, DateTim
protected override DateTime Max { get; } = WebApiLDateTime.MaxValue;
protected override DateTime Mid { get; } = DateTime.Today;
protected override DateTime Min { get; } = WebApiLDateTime.MinValue;

[Fact]
public virtual async void should_synchron_write_leap_value()
{
var expected = new DateTime(2024, 3, 4);
TestConnector.TestApiConnector.ClearPeriodicReadSet();
await webApiPrimitive!.SetAsync(expected);
Assert.Equal(expected, await webApiPrimitive.GetAsync());
}

[Fact]
public virtual async void should_synchron_write_no_leap_value()
{
var expected = new DateTime(2023, 3, 4);
TestConnector.TestApiConnector.ClearPeriodicReadSet();
await webApiPrimitive!.SetAsync(expected);
Assert.Equal(expected, await webApiPrimitive.GetAsync());
}

[Fact]
public virtual async void should_synchron_write_first_leap_day_value()
{
var expected = new DateTime(2024, 2, 29);
TestConnector.TestApiConnector.ClearPeriodicReadSet();
await webApiPrimitive!.SetAsync(expected);
Assert.Equal(expected, await webApiPrimitive.GetAsync());
}

[Fact]
public virtual async void should_write_cyclic_leap_value()
{
var expected = new DateTime(2024, 3, 4);
TestConnector.TestApiConnector.ClearPeriodicReadSet();
await webApiPrimitive.SetAsync(Max);
webApiPrimitive!.Cyclic = expected;
webApiPrimitive!.AddToPeriodicQueue();
await Task.Delay(WaitTimeForCyclicOperations);
Assert.Equal(expected, webApiPrimitive.Cyclic);
Assert.Equal(expected, await webApiPrimitive.GetAsync());
}


[Fact]
public virtual async void should_write_cyclic_no_leap_value()
{
var expected = new DateTime(2023, 3, 4);
TestConnector.TestApiConnector.ClearPeriodicReadSet();
await webApiPrimitive.SetAsync(Max);
webApiPrimitive!.Cyclic = expected;
webApiPrimitive!.AddToPeriodicQueue();
await Task.Delay(WaitTimeForCyclicOperations);
Assert.Equal(expected, webApiPrimitive.Cyclic);
Assert.Equal(expected, await webApiPrimitive.GetAsync());
}

[Fact]
public virtual async void should_synchron_read_first_leap_day_value()
{
var primitive = Activator.CreateInstance(typeof(WebApiDate), Connector, "", "myLDATE_AND_TIME_leap_febr") as WebApiDate;
var expected = new DateOnly(2024, 2, 29);
TestConnector.TestApiConnector.ClearPeriodicReadSet();

Assert.Equal(expected, await primitive.GetAsync());
}

[Fact]
public virtual async void should_synchron_read_leap_day_value()
{
var primitive = Activator.CreateInstance(typeof(WebApiDate), Connector, "", "myLDATE_AND_TIME_leap_late") as WebApiDate;
var expected = new DateOnly(2024, 3, 5);
TestConnector.TestApiConnector.ClearPeriodicReadSet();

Assert.Equal(expected, await primitive.GetAsync());
}

}

}
Loading

0 comments on commit 79b10c6

Please sign in to comment.