Skip to content

Commit fc6b4b0

Browse files
GH-47131: [C#] Fix day off by 1 in Date64Array (#47132)
### Rationale for this change `Date64Array.Convert(DateTimeOffset)` substracts one day on date times that are at 00:00 am and < 1970. For example, 01/01/1969 00:00:00 gets converted to 12/31/1968 whereas the expected result is 01/01/1969. ### What changes are included in this PR? Fix to the described bug. ### Are these changes tested? Yes ### Are there any user-facing changes? No **This PR contains a "Critical Fix".** * GitHub Issue: #47131 Authored-by: Jeremy Costanzo <[email protected]> Signed-off-by: Curt Hagenlocher <[email protected]>
1 parent eda392c commit fc6b4b0

File tree

2 files changed

+2
-7
lines changed

2 files changed

+2
-7
lines changed

csharp/src/Apache.Arrow/Arrays/Date64Array.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,7 @@ protected override long Convert(DateTime dateTime)
6565

6666
protected override long Convert(DateTimeOffset dateTimeOffset)
6767
{
68-
// The internal value stored for a DateTimeOffset can be thought of as the number of milliseconds,
69-
// in multiples of 86400000, that have passed since the UNIX epoch. It is not the same as what would
70-
// result from encoding the date from the DateTimeOffset.Date property.
71-
long millis = dateTimeOffset.ToUnixTimeMilliseconds();
72-
long days = millis / MillisecondsPerDay;
73-
return (millis < 0 ? days - 1 : days) * MillisecondsPerDay;
68+
return new DateTimeOffset(dateTimeOffset.UtcDateTime.Date, TimeSpan.Zero).ToUnixTimeMilliseconds();
7469
}
7570

7671
#if NET6_0_OR_GREATER

csharp/test/Apache.Arrow.Tests/TestDateAndTimeData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ internal static class TestDateAndTimeData
3636

3737
private static readonly TimeSpan[] _exampleTimes =
3838
{
39-
new TimeSpan(0, 0, 1), new TimeSpan(12, 0, 0), new TimeSpan(23, 59, 59),
39+
new TimeSpan(0, 0, 0), new TimeSpan(0, 0, 1), new TimeSpan(12, 0, 0), new TimeSpan(23, 59, 59),
4040
};
4141

4242
private static readonly DateTimeKind[] _exampleKinds =

0 commit comments

Comments
 (0)