Skip to content

Commit cc352ba

Browse files
authored
fix: correct ForceStringDecoding offsets for DATE12 and DATE14 (#51)
1 parent 1ec240f commit cc352ba

5 files changed

Lines changed: 77 additions & 12 deletions

File tree

NetCore8583.Test/Parse/TestDate12ParseInfo.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ public class TestDate12ParseInfo
4141
{
4242
private static sbyte[] Ascii(string s) => s.GetSignedBytes(Encoding.ASCII);
4343

44+
private static Encoding GetTestEncoding(string name) => name switch
45+
{
46+
"ASCII" => Encoding.ASCII,
47+
"UTF8" => Encoding.UTF8,
48+
"IBM037" => CodePagesEncodingProvider.Instance.GetEncoding(37),
49+
_ => throw new ArgumentOutOfRangeException(nameof(name), name, null)
50+
};
51+
4452
// ═══════════════════════════════════════════════════════════════════════
4553
// Parse (ASCII)
4654
// ═══════════════════════════════════════════════════════════════════════
@@ -61,6 +69,31 @@ public void Parse_ReturnsCorrectDateTime()
6169
Assert.Equal(0, dt.Second);
6270
}
6371

72+
[Theory]
73+
[InlineData("ASCII")]
74+
[InlineData("UTF8")]
75+
[InlineData("IBM037")]
76+
public void Parse_ForceStringDecoding_ReturnsCorrectDateTime(string encodingName)
77+
{
78+
// Regression for issue #50:
79+
// var val = fpi.Parse(1, "260316143000".GetSignedBytes(Encoding.ASCII), 0, null);
80+
var encoding = GetTestEncoding(encodingName);
81+
var fpi = new Date12ParseInfo
82+
{
83+
ForceStringDecoding = true,
84+
Encoding = encoding
85+
};
86+
var val = fpi.Parse(1, "260316143000".GetSignedBytes(encoding), 0, null);
87+
var dt = (DateTime) val.Value;
88+
89+
Assert.Equal(2026, dt.Year);
90+
Assert.Equal(3, dt.Month);
91+
Assert.Equal(16, dt.Day);
92+
Assert.Equal(14, dt.Hour);
93+
Assert.Equal(30, dt.Minute);
94+
Assert.Equal(0, dt.Second);
95+
}
96+
6497
[Fact]
6598
public void Parse_YearAbove50_Is1900sEra()
6699
{

NetCore8583.Test/Parse/TestDate14ParseInfo.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ public class TestDate14ParseInfo
4141
{
4242
private static sbyte[] Ascii(string s) => s.GetSignedBytes(Encoding.ASCII);
4343

44+
private static Encoding GetTestEncoding(string name) => name switch
45+
{
46+
"ASCII" => Encoding.ASCII,
47+
"UTF8" => Encoding.UTF8,
48+
"IBM037" => CodePagesEncodingProvider.Instance.GetEncoding(37),
49+
_ => throw new ArgumentOutOfRangeException(nameof(name), name, null)
50+
};
51+
4452
// ═══════════════════════════════════════════════════════════════════════
4553
// Parse (ASCII)
4654
// ═══════════════════════════════════════════════════════════════════════
@@ -61,6 +69,24 @@ public void Parse_ReturnsCorrectDateTime()
6169
Assert.Equal(0, dt.Second);
6270
}
6371

72+
[Theory]
73+
[InlineData("ASCII")]
74+
[InlineData("UTF8")]
75+
[InlineData("IBM037")]
76+
public void Parse_ForceStringDecoding_ReturnsCorrectDateTime(string encodingName)
77+
{
78+
var encoding = GetTestEncoding(encodingName);
79+
var fpi = new Date14ParseInfo
80+
{
81+
ForceStringDecoding = true,
82+
Encoding = encoding
83+
};
84+
var val = fpi.Parse(1, "20260316143000".GetSignedBytes(encoding), 0, null);
85+
var dt = (DateTime) val.Value;
86+
87+
Assert.Equal(new DateTime(2026, 3, 16, 14, 30, 0), dt);
88+
}
89+
6490
[Fact]
6591
public void Parse_EndOfMillennium()
6692
{

NetCore8583/NetCore8583.csproj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@
1111
<PackageReadmeFile>README.md</PackageReadmeFile>
1212
<PackageTags>Iso8583 NetCore Banking C#</PackageTags>
1313
<Copyright>Arsene Tochemey Gandote 2018-2026</Copyright>
14-
<PackageVersion>2.5.0</PackageVersion>
14+
<PackageVersion>2.5.1</PackageVersion>
1515
<TargetFrameworks>net10.0;net8.0;net9.0</TargetFrameworks>
16-
<ReleaseVersion>2.5.0</ReleaseVersion>
16+
<ReleaseVersion>2.5.1</ReleaseVersion>
1717
<Title>NetCore8583</Title>
1818
<PackageReleaseNotes>
19+
v2.5.1
20+
21+
Bug Fixes:
22+
- Corrected DATE12 and DATE14 ForceStringDecoding field offsets for year, month, day, hour, minute, and second parsing.
23+
- Added regression coverage for ASCII, UTF-8, and IBM037 encoded date fields.
24+
1925
v2.5.0
2026

2127
New Features:

NetCore8583/Parse/Date12ParseInfo.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,23 @@ public override IsoValue Parse(int field,
5656

5757
if (year > 50) year = 1900 + year;
5858
else year = 2000 + year;
59-
var month = Convert.ToInt32(buf.ToString(pos,
59+
var month = Convert.ToInt32(buf.ToString(pos + 2,
6060
2,
6161
Encoding),
6262
10);
63-
var day = Convert.ToInt32(buf.ToString(pos + 2,
63+
var day = Convert.ToInt32(buf.ToString(pos + 4,
6464
2,
6565
Encoding),
6666
10);
67-
var hour = Convert.ToInt32(buf.ToString(pos + 4,
67+
var hour = Convert.ToInt32(buf.ToString(pos + 6,
6868
2,
6969
Encoding),
7070
10);
71-
var min = Convert.ToInt32(buf.ToString(pos + 6,
71+
var min = Convert.ToInt32(buf.ToString(pos + 8,
7272
2,
7373
Encoding),
7474
10);
75-
var sec = Convert.ToInt32(buf.ToString(pos + 8,
75+
var sec = Convert.ToInt32(buf.ToString(pos + 10,
7676
2,
7777
Encoding),
7878
10);

NetCore8583/Parse/Date14ParseInfo.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,23 @@ public override IsoValue Parse(int field,
5151
4,
5252
Encoding),
5353
10);
54-
var month = Convert.ToInt32(buf.ToString(pos,
54+
var month = Convert.ToInt32(buf.ToString(pos + 4,
5555
2,
5656
Encoding),
5757
10);
58-
var day = Convert.ToInt32(buf.ToString(pos + 2,
58+
var day = Convert.ToInt32(buf.ToString(pos + 6,
5959
2,
6060
Encoding),
6161
10);
62-
var hour = Convert.ToInt32(buf.ToString(pos + 4,
62+
var hour = Convert.ToInt32(buf.ToString(pos + 8,
6363
2,
6464
Encoding),
6565
10);
66-
var min = Convert.ToInt32(buf.ToString(pos + 6,
66+
var min = Convert.ToInt32(buf.ToString(pos + 10,
6767
2,
6868
Encoding),
6969
10);
70-
var sec = Convert.ToInt32(buf.ToString(pos + 8,
70+
var sec = Convert.ToInt32(buf.ToString(pos + 12,
7171
2,
7272
Encoding),
7373
10);

0 commit comments

Comments
 (0)