Skip to content

Commit

Permalink
Timestamps honor timezone Pt. 2 (#109)
Browse files Browse the repository at this point in the history
* Tests

* Fix
  • Loading branch information
gfoidl authored Jul 9, 2022
1 parent 9b9460f commit 4a9f7a5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
8 changes: 7 additions & 1 deletion source/trx2junit.Core/Extensions/TimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ public static string ToTrxDateTime(this DateTimeOffset dt)
value.Millisecond.TryFormat(buffer.Slice(20), out int written, "000");
Debug.Assert(written == 3);

value.Offset.Hours.TryFormat(buffer.Slice(24), out written, "00");
int offsetHours = value.Offset.Hours;
if (offsetHours < 0)
{
buffer[23] = '-';
offsetHours = -offsetHours;
}
offsetHours.TryFormat(buffer.Slice(24), out written, "00");
Debug.Assert(written == 2);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public void Brute_force_validation()
}
//-------------------------------------------------------------------------
[Test]
public void Valid_TrxDateTime___OK()
public void Valid_TrxDateTime___OK([Values(-1, 0, 1)] int offset)
{
DateTimeOffset now = new(2019, 11, 10, 15, 33, 27, 123, TimeSpan.FromHours(1d));
DateTimeOffset now = new(2019, 11, 10, 15, 33, 27, 123, TimeSpan.FromHours(offset));
string value = now.ToTrxDateTime();

DateTimeOffset? actual = value.ParseDateTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ public string DateTime_given___correct_format(DateTimeOffset dateTime)
//-------------------------------------------------------------------------
private static IEnumerable<TestCaseData> DateTime_given___correct_format_TestCases()
{
yield return new TestCaseData(new DateTimeOffset(2019, 11, 10, 15, 33, 27, 446, TimeSpan.FromHours(0d))).Returns("2019-11-10T15:33:27.446+00:00");
yield return new TestCaseData(new DateTimeOffset(2019, 11, 10, 15, 33, 27, 446, TimeSpan.FromHours(1d))).Returns("2019-11-10T15:33:27.446+01:00");
yield return new TestCaseData(new DateTimeOffset(2019, 11, 10, 15, 33, 27, 446, TimeSpan.FromHours(2d))).Returns("2019-11-10T15:33:27.446+02:00");
yield return new TestCaseData(new DateTimeOffset(2019, 11, 10, 15, 33, 27, 446, TimeSpan.FromHours(-2d))).Returns("2019-11-10T15:33:27.446-02:00");
yield return new TestCaseData(new DateTimeOffset(2019, 11, 10, 15, 33, 27, 446, TimeSpan.FromHours(-1d))).Returns("2019-11-10T15:33:27.446-01:00");
yield return new TestCaseData(new DateTimeOffset(2019, 11, 10, 15, 33, 27, 446, TimeSpan.FromHours( 0d))).Returns("2019-11-10T15:33:27.446+00:00");
yield return new TestCaseData(new DateTimeOffset(2019, 11, 10, 15, 33, 27, 446, TimeSpan.FromHours( 1d))).Returns("2019-11-10T15:33:27.446+01:00");
yield return new TestCaseData(new DateTimeOffset(2019, 11, 10, 15, 33, 27, 446, TimeSpan.FromHours( 2d))).Returns("2019-11-10T15:33:27.446+02:00");
}
}

0 comments on commit 4a9f7a5

Please sign in to comment.