1 // Licensed to the Apache Software Foundation (ASF) under one or more
2 // contributor license agreements. See the NOTICE file distributed with
3 // this work for additional information regarding copyright ownership.
4 // The ASF licenses this file to You under the Apache License, Version 2.0
5 // (the "License"); you may not use this file except in compliance with
6 // the License. You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 using System.Collections.Generic;
21 namespace Apache.Arrow.Tests
23 public class Date64ArrayTests
25 private const long MillisecondsPerDay = 86400000;
27 public static IEnumerable<object[]> GetDatesData() =>
28 TestDateAndTimeData.ExampleDates.Select(d => new object[] { d });
30 public static IEnumerable<object[]> GetDateTimesData() =>
31 TestDateAndTimeData.ExampleDateTimes.Select(dt => new object[] { dt });
33 public static IEnumerable<object[]> GetDateTimeOffsetsData() =>
34 TestDateAndTimeData.ExampleDateTimeOffsets.Select(dto => new object[] { dto });
36 public class AppendNull
39 public void AppendThenGetGivesNull()
42 var builder = new Date64Array.Builder();
45 builder = builder.AppendNull();
48 var array = builder.Build();
49 Assert.Equal(1, array.Length);
50 Assert.Null(array.GetDateTime(0));
51 Assert.Null(array.GetDateTimeOffset(0));
52 Assert.Null(array.GetValue(0));
56 public class AppendDateTime
59 [MemberData(nameof(GetDatesData), MemberType = typeof(Date64ArrayTests))]
60 public void AppendDateGivesSameDate(DateTime date)
63 var builder = new Date64Array.Builder();
64 var expectedDateTime = date;
65 var expectedDateTimeOffset =
66 new DateTimeOffset(DateTime.SpecifyKind(date, DateTimeKind.Unspecified), TimeSpan.Zero);
67 long expectedValue = (long)date.Subtract(new DateTime(1970, 1, 1)).TotalDays * MillisecondsPerDay;
70 builder = builder.Append(date);
73 var array = builder.Build();
74 Assert.Equal(1, array.Length);
75 Assert.Equal(expectedDateTime, array.GetDateTime(0));
76 Assert.Equal(expectedDateTimeOffset, array.GetDateTimeOffset(0));
77 Assert.Equal(expectedValue, array.GetValue(0));
78 Assert.Equal(0, array.GetValue(0).Value % MillisecondsPerDay);
82 [MemberData(nameof(GetDateTimesData), MemberType = typeof(Date64ArrayTests))]
83 public void AppendWithTimeGivesSameWithTimeIgnored(DateTime dateTime)
86 var builder = new Date64Array.Builder();
87 var expectedDateTime = dateTime.Date;
88 var expectedDateTimeOffset =
89 new DateTimeOffset(DateTime.SpecifyKind(dateTime.Date, DateTimeKind.Unspecified), TimeSpan.Zero);
91 (long)dateTime.Date.Subtract(new DateTime(1970, 1, 1)).TotalDays * MillisecondsPerDay;
94 builder = builder.Append(dateTime);
97 var array = builder.Build();
98 Assert.Equal(1, array.Length);
99 Assert.Equal(expectedDateTime, array.GetDateTime(0));
100 Assert.Equal(expectedDateTimeOffset, array.GetDateTimeOffset(0));
101 Assert.Equal(expectedValue, array.GetValue(0));
102 Assert.Equal(0, array.GetValue(0).Value % MillisecondsPerDay);
106 public class AppendDateTimeOffset
109 [MemberData(nameof(GetDateTimeOffsetsData), MemberType = typeof(Date64ArrayTests))]
110 public void AppendGivesUtcDate(DateTimeOffset dateTimeOffset)
113 var builder = new Date64Array.Builder();
114 var expectedDateTime = dateTimeOffset.UtcDateTime.Date;
115 var expectedDateTimeOffset = new DateTimeOffset(dateTimeOffset.UtcDateTime.Date, TimeSpan.Zero);
117 (long)dateTimeOffset.UtcDateTime.Date.Subtract(new DateTime(1970, 1, 1)).TotalDays *
121 builder = builder.Append(dateTimeOffset);
124 var array = builder.Build();
125 Assert.Equal(1, array.Length);
126 Assert.Equal(expectedDateTime, array.GetDateTime(0));
127 Assert.Equal(expectedDateTimeOffset, array.GetDateTimeOffset(0));
128 Assert.Equal(expectedValue, array.GetValue(0));
129 Assert.Equal(0, array.GetValue(0).Value % MillisecondsPerDay);