• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Giorgi / DuckDB.NET / 23004948356

12 Mar 2026 01:42PM UTC coverage: 89.595% (+0.07%) from 89.526%
23004948356

push

github

Giorgi
Update GitVersion

1277 of 1485 branches covered (85.99%)

Branch coverage included in aggregate %.

2658 of 2907 relevant lines covered (91.43%)

451450.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.78
/DuckDB.NET.Data/DataChunk/Reader/DateTimeVectorDataReader.cs
1
namespace DuckDB.NET.Data.DataChunk.Reader;
2

3
internal sealed class DateTimeVectorDataReader : VectorDataReaderBase
4
{
5
    internal unsafe DateTimeVectorDataReader(void* dataPointer, ulong* validityMaskPointer, DuckDBType columnType, string columnName) : base(dataPointer, validityMaskPointer, columnType, columnName)
2,322✔
6
    {
7
    }
2,322✔
8

9
    protected override T GetValidValue<T>(ulong offset)
10
    {
11
        if (DuckDBType == DuckDBType.Date)
816✔
12
        {
13
            var (dateOnly, isFinite) = GetDateOnly(offset);
96✔
14

15
            if (!isFinite)
96✔
16
            {
17
                if (typeof(T) == typeof(DateTime) || typeof(T) == typeof(DateOnly))
60✔
18
                {
19
                    ThrowInfinityDateException();
36✔
20
                }
21

22
                return (T)(object)dateOnly;
24✔
23
            }
24

25
            if (typeof(T) == typeof(DateTime))
36✔
26
            {
27
                return (T)(object)(DateTime)dateOnly;
9✔
28
            }
29

30
            if (typeof(T) == typeof(DateOnly))
27✔
31
            {
32
                return (T)(object)(DateOnly)dateOnly;
9✔
33
            }
34

35
            return (T)(object)dateOnly;
18✔
36
        }
37

38
        if (DuckDBType == DuckDBType.Time)
720✔
39
        {
40
            var timeOnly = GetTimeOnly(offset);
42✔
41

42
            if (typeof(T) == typeof(DateTime))
42!
43
            {
44
                return (T)(object)(DateTime)timeOnly;
×
45
            }
46

47
            if (typeof(T) == typeof(TimeOnly))
42✔
48
            {
49
                return (T)(object)(TimeOnly)timeOnly;
18✔
50
            }
51

52
            return (T)(object)timeOnly;
24✔
53
        }
54

55
        if (DuckDBType == DuckDBType.TimeTz)
678✔
56
        {
57
            var timeTz = GetTimeTz(offset);
42✔
58

59
            if (typeof(T) == typeof(DateTimeOffset))
42✔
60
            {
61
                var dateTimeOffset = new DateTimeOffset(timeTz.Time.ToDateTime(), TimeSpan.FromSeconds(timeTz.Offset));
36✔
62
                return (T)(object)dateTimeOffset;
36✔
63
            }
64

65
            return (T)(object)timeTz;
6✔
66
        }
67

68
        return DuckDBType switch
636!
69
        {
636✔
70
            DuckDBType.Timestamp or DuckDBType.TimestampS or
636✔
71
            DuckDBType.TimestampTz or DuckDBType.TimestampMs or
636✔
72
            DuckDBType.TimestampNs => ReadTimestamp<T>(offset),
636✔
73
            _ => base.GetValidValue<T>(offset)
×
74
        };
636✔
75
    }
76

77
    private T ReadTimestamp<T>(ulong offset)
78
    {
79
        var timestampStruct = GetFieldData<DuckDBTimestampStruct>(offset);
636✔
80

81
        if (!timestampStruct.IsFinite(DuckDBType))
636✔
82
        {
83
            if (typeof(T) == typeof(DateTime) || typeof(T) == typeof(DateTimeOffset))
246✔
84
            {
85
                ThrowInfinityTimestampException();
180✔
86
            }
87

88
            return (T)(object)DuckDBTimestamp.FromDuckDBTimestampStruct(timestampStruct);
66✔
89
        }
90

91
        var (timestamp, additionalTicks) = timestampStruct.ToDuckDBTimestamp(DuckDBType);
390✔
92

93
        if (typeof(T) == typeof(DateTime))
390✔
94
        {
95
            return (T)(object)timestamp.ToDateTime().AddTicks(additionalTicks);
192✔
96
        }
97

98
        if (typeof(T) == typeof(DateTimeOffset))
198✔
99
        {
100
            var dateTime = timestamp.ToDateTime().AddTicks(additionalTicks);
171✔
101
            return (T)(object)new DateTimeOffset(dateTime, TimeSpan.Zero);
171✔
102
        }
103

104
        return (T)(object)timestamp;
27✔
105
    }
106

107
    internal override object GetValue(ulong offset, Type targetType)
108
    {
109
        return DuckDBType switch
766,904!
110
        {
766,904✔
111
            DuckDBType.Date => GetDate(offset, targetType),
750,031✔
112
            DuckDBType.Time => GetTime(offset, targetType),
734✔
113
            DuckDBType.TimeTz => GetDateTimeOffset(offset, targetType),
533✔
114
            DuckDBType.Timestamp or DuckDBType.TimestampS or
766,904✔
115
            DuckDBType.TimestampTz or DuckDBType.TimestampMs or
766,904✔
116
            DuckDBType.TimestampNs => GetDateTime(offset, targetType),
15,606✔
117
            _ => base.GetValue(offset, targetType)
×
118
        };
766,904✔
119
    }
120

121
    private DuckDBTimeTz GetTimeTz(ulong offset)
122
    {
123
        var data = GetFieldData<DuckDBTimeTzStruct>(offset);
575✔
124

125
        return NativeMethods.DateTimeHelpers.DuckDBFromTimeTz(data);
575✔
126
    }
127

128
    private DuckDBTimeOnly GetTimeOnly(ulong offset)
129
    {
130
        return NativeMethods.DateTimeHelpers.DuckDBFromTime(GetFieldData<DuckDBTime>(offset));
776✔
131
    }
132

133
    private (DuckDBDateOnly dateOnly, bool IsFinite) GetDateOnly(ulong offset)
134
    {
135
        var date = GetFieldData<DuckDBDate>(offset);
750,127✔
136
        var isFinite = NativeMethods.DateTimeHelpers.DuckDBIsFiniteDate(date);
750,127✔
137
        return (DuckDBDateOnly.FromDuckDBDate(date), isFinite);
750,127✔
138
    }
139

140
    private object GetDate(ulong offset, Type targetType)
141
    {
142
        var (dateOnly, isFinite) = GetDateOnly(offset);
750,031✔
143

144
        if (!isFinite)
750,031✔
145
        {
146
            if (targetType == typeof(DateTime) || targetType == typeof(DateOnly))
6!
147
            {
148
                ThrowInfinityDateException();
×
149
            }
150

151
            return dateOnly;
6✔
152
        }
153

154
        if (targetType == typeof(DateTime))
750,025✔
155
        {
156
            return (DateTime)dateOnly;
749,425✔
157
        }
158

159
        if (targetType == typeof(DateOnly))
600✔
160
        {
161
            return (DateOnly)dateOnly;
396✔
162
        }
163

164
        return dateOnly;
204✔
165
    }
166

167
    private object GetTime(ulong offset, Type targetType)
168
    {
169
        var timeOnly = GetTimeOnly(offset);
734✔
170
        if (targetType == typeof(DateTime))
734!
171
        {
172
            return (DateTime)timeOnly;
×
173
        }
174

175
        if (targetType == typeof(TimeOnly))
734✔
176
        {
177
            return (TimeOnly)timeOnly;
559✔
178
        }
179

180
        return timeOnly;
175✔
181
    }
182

183
    private object GetDateTime(ulong offset, Type targetType)
184
    {
185
        var timestampStruct = GetFieldData<DuckDBTimestampStruct>(offset);
15,606✔
186

187
        if (!timestampStruct.IsFinite(DuckDBType))
15,606✔
188
        {
189
            if (targetType == typeof(DateTime) || targetType == typeof(DateTimeOffset))
12!
190
            {
191
                ThrowInfinityTimestampException();
×
192
            }
193

194
            return DuckDBTimestamp.FromDuckDBTimestampStruct(timestampStruct);
12✔
195
        }
196

197
        var (timestamp, additionalTicks) = timestampStruct.ToDuckDBTimestamp(DuckDBType);
15,594✔
198

199
        if (targetType == typeof(DateTime))
15,594✔
200
        {
201
            var dateTime = timestamp.ToDateTime().AddTicks(additionalTicks);
15,558✔
202

203
            return dateTime;
15,558✔
204
        }
205

206
        if (targetType == typeof(DateTimeOffset))
36!
207
        {
208
            var dateTime = timestamp.ToDateTime().AddTicks(additionalTicks);
×
209
            return new DateTimeOffset(dateTime, TimeSpan.Zero);
×
210
        }
211

212
        return timestamp;
36✔
213
    }
214

215
    private object GetDateTimeOffset(ulong offset, Type targetType)
216
    {
217
        var timeTz = GetTimeTz(offset);
533✔
218

219
        if (targetType == typeof(DateTimeOffset))
533✔
220
        {
221
            return new DateTimeOffset(timeTz.Time.ToDateTime(), TimeSpan.FromSeconds(timeTz.Offset));
527✔
222
        }
223

224
        return timeTz;
6✔
225
    }
226

227
    private static void ThrowInfinityDateException()
228
    {
229
        throw new InvalidOperationException(
36✔
230
            "Cannot convert infinite date value to DateTime or DateOnly. " +
36✔
231
            "Use DuckDBDateOnly to read this value and check IsInfinity, IsPositiveInfinity, or IsNegativeInfinity before converting to .NET types.");
36✔
232
    }
233

234
    private static void ThrowInfinityTimestampException()
235
    {
236
        throw new InvalidOperationException(
180✔
237
            "Cannot convert infinite timestamp value to DateTime or DateTimeOffset. " +
180✔
238
            "Use DuckDBTimestamp to read this value and check IsInfinity, IsPositiveInfinity, or IsNegativeInfinity before converting to .NET types.");
180✔
239
    }
240
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc