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

Giorgi / DuckDB.NET / 22921736195

10 Mar 2026 07:24PM UTC coverage: 89.526% (+0.08%) from 89.45%
22921736195

push

github

Giorgi
Update global.json

1255 of 1463 branches covered (85.78%)

Branch coverage included in aggregate %.

2651 of 2900 relevant lines covered (91.41%)

447643.55 hits per line

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

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

3
internal sealed class DateTimeVectorDataReader : VectorDataReaderBase
4
{
5
    private static readonly Type DateTimeType = typeof(DateTime);
3✔
6
    private static readonly Type DateTimeOffsetType = typeof(DateTimeOffset);
3✔
7
    private static readonly Type DateOnlyType = typeof(DateOnly);
3✔
8
    private static readonly Type TimeOnlyType = typeof(TimeOnly);
3✔
9

10
    internal unsafe DateTimeVectorDataReader(void* dataPointer, ulong* validityMaskPointer, DuckDBType columnType, string columnName) : base(dataPointer, validityMaskPointer, columnType, columnName)
2,322✔
11
    {
12
    }
2,322✔
13

14
    protected override T GetValidValue<T>(ulong offset, Type targetType)
15
    {
16
        if (DuckDBType == DuckDBType.Date)
816✔
17
        {
18
            var (dateOnly, isFinite) = GetDateOnly(offset);
96✔
19

20
            if (!isFinite)
96✔
21
            {
22
                if (targetType == DateTimeType || targetType == DateOnlyType)
60✔
23
                {
24
                    ThrowInfinityDateException();
36✔
25
                }
26

27
                return (T)(object)dateOnly;
24✔
28
            }
29

30
            if (targetType == DateTimeType)
36✔
31
            {
32
                return (T)(object)(DateTime)dateOnly;
9✔
33
            }
34

35
            if (targetType == DateOnlyType)
27✔
36
            {
37
                return (T)(object)(DateOnly)dateOnly;
9✔
38
            }
39

40
            return (T)(object)dateOnly;
18✔
41
        }
42

43
        if (DuckDBType == DuckDBType.Time)
720✔
44
        {
45
            var timeOnly = GetTimeOnly(offset);
42✔
46

47
            if (targetType == DateTimeType)
42!
48
            {
49
                return (T)(object)(DateTime)timeOnly;
×
50
            }
51

52
            if (targetType == TimeOnlyType)
42✔
53
            {
54
                return (T)(object)(TimeOnly)timeOnly;
18✔
55
            }
56

57
            return (T)(object)timeOnly;
24✔
58
        }
59

60
        if (DuckDBType == DuckDBType.TimeTz)
678✔
61
        {
62
            var timeTz = GetTimeTz(offset);
42✔
63

64
            if (targetType == DateTimeOffsetType)
42✔
65
            {
66
                var dateTimeOffset = new DateTimeOffset(timeTz.Time.ToDateTime(), TimeSpan.FromSeconds(timeTz.Offset));
36✔
67
                return (T)(object)dateTimeOffset;
36✔
68
            }
69

70
            return (T)(object)timeTz;
6✔
71
        }
72

73
        return DuckDBType switch
636!
74
        {
636✔
75
            DuckDBType.Timestamp or DuckDBType.TimestampS or
636✔
76
            DuckDBType.TimestampTz or DuckDBType.TimestampMs or
636✔
77
            DuckDBType.TimestampNs => ReadTimestamp<T>(offset, targetType),
636✔
78
            _ => base.GetValidValue<T>(offset, targetType)
×
79
        };
636✔
80
    }
81

82
    private T ReadTimestamp<T>(ulong offset, Type targetType)
83
    {
84
        var timestampStruct = GetFieldData<DuckDBTimestampStruct>(offset);
636✔
85

86
        if (!timestampStruct.IsFinite(DuckDBType))
636✔
87
        {
88
            if (targetType == DateTimeType || targetType == DateTimeOffsetType)
246✔
89
            {
90
                ThrowInfinityTimestampException();
180✔
91
            }
92

93
            return (T)(object)DuckDBTimestamp.FromDuckDBTimestampStruct(timestampStruct);
66✔
94
        }
95

96
        var (timestamp, additionalTicks) = timestampStruct.ToDuckDBTimestamp(DuckDBType);
390✔
97

98
        if (targetType == DateTimeType)
390✔
99
        {
100
            return (T)(object)timestamp.ToDateTime().AddTicks(additionalTicks);
192✔
101
        }
102

103
        if (targetType == DateTimeOffsetType)
198✔
104
        {
105
            var dateTime = timestamp.ToDateTime().AddTicks(additionalTicks);
171✔
106
            return (T)(object)new DateTimeOffset(dateTime, TimeSpan.Zero);
171✔
107
        }
108

109
        return (T)(object)timestamp;
27✔
110
    }
111

112
    internal override object GetValue(ulong offset, Type targetType)
113
    {
114
        return DuckDBType switch
767,935!
115
        {
767,935✔
116
            DuckDBType.Date => GetDate(offset, targetType),
750,796✔
117
            DuckDBType.Time => GetTime(offset, targetType),
922✔
118
            DuckDBType.TimeTz => GetDateTimeOffset(offset, targetType),
611✔
119
            DuckDBType.Timestamp or DuckDBType.TimestampS or
767,935✔
120
            DuckDBType.TimestampTz or DuckDBType.TimestampMs or
767,935✔
121
            DuckDBType.TimestampNs => GetDateTime(offset, targetType),
15,606✔
122
            _ => base.GetValue(offset, targetType)
×
123
        };
767,935✔
124
    }
125

126
    private DuckDBTimeTz GetTimeTz(ulong offset)
127
    {
128
        var data = GetFieldData<DuckDBTimeTzStruct>(offset);
653✔
129

130
        return NativeMethods.DateTimeHelpers.DuckDBFromTimeTz(data);
653✔
131
    }
132

133
    private DuckDBTimeOnly GetTimeOnly(ulong offset)
134
    {
135
        return NativeMethods.DateTimeHelpers.DuckDBFromTime(GetFieldData<DuckDBTime>(offset));
964✔
136
    }
137

138
    private (DuckDBDateOnly dateOnly, bool IsFinite) GetDateOnly(ulong offset)
139
    {
140
        var date = GetFieldData<DuckDBDate>(offset);
750,892✔
141
        var isFinite = NativeMethods.DateTimeHelpers.DuckDBIsFiniteDate(date);
750,892✔
142
        return (DuckDBDateOnly.FromDuckDBDate(date), isFinite);
750,892✔
143
    }
144

145
    private object GetDate(ulong offset, Type targetType)
146
    {
147
        var (dateOnly, isFinite) = GetDateOnly(offset);
750,796✔
148

149
        if (!isFinite)
750,796✔
150
        {
151
            if (targetType == DateTimeType || targetType == DateOnlyType)
6!
152
            {
153
                ThrowInfinityDateException();
×
154
            }
155

156
            return dateOnly;
6✔
157
        }
158

159
        if (targetType == DateTimeType)
750,790✔
160
        {
161
            return (DateTime)dateOnly;
750,053✔
162
        }
163

164
        if (targetType == DateOnlyType)
737✔
165
        {
166
            return (DateOnly)dateOnly;
502✔
167
        }
168

169
        return dateOnly;
235✔
170
    }
171

172
    private object GetTime(ulong offset, Type targetType)
173
    {
174
        var timeOnly = GetTimeOnly(offset);
922✔
175
        if (targetType == DateTimeType)
922!
176
        {
177
            return (DateTime)timeOnly;
×
178
        }
179

180
        if (targetType == TimeOnlyType)
922✔
181
        {
182
            return (TimeOnly)timeOnly;
649✔
183
        }
184

185
        return timeOnly;
273✔
186
    }
187

188
    private object GetDateTime(ulong offset, Type targetType)
189
    {
190
        var timestampStruct = GetFieldData<DuckDBTimestampStruct>(offset);
15,606✔
191

192
        if (!timestampStruct.IsFinite(DuckDBType))
15,606✔
193
        {
194
            if (targetType == DateTimeType || targetType == DateTimeOffsetType)
12!
195
            {
196
                ThrowInfinityTimestampException();
×
197
            }
198

199
            return DuckDBTimestamp.FromDuckDBTimestampStruct(timestampStruct);
12✔
200
        }
201

202
        var (timestamp, additionalTicks) = timestampStruct.ToDuckDBTimestamp(DuckDBType);
15,594✔
203

204
        if (targetType == DateTimeType)
15,594✔
205
        {
206
            var dateTime = timestamp.ToDateTime().AddTicks(additionalTicks);
15,558✔
207

208
            return dateTime;
15,558✔
209
        }
210

211
        if (targetType == DateTimeOffsetType)
36!
212
        {
213
            var dateTime = timestamp.ToDateTime().AddTicks(additionalTicks);
×
214
            return new DateTimeOffset(dateTime, TimeSpan.Zero);
×
215
        }
216

217
        return timestamp;
36✔
218
    }
219

220
    private object GetDateTimeOffset(ulong offset, Type targetType)
221
    {
222
        var timeTz = GetTimeTz(offset);
611✔
223

224
        if (targetType == DateTimeOffsetType)
611✔
225
        {
226
            return new DateTimeOffset(timeTz.Time.ToDateTime(), TimeSpan.FromSeconds(timeTz.Offset));
605✔
227
        }
228

229
        return timeTz;
6✔
230
    }
231

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

239
    private static void ThrowInfinityTimestampException()
240
    {
241
        throw new InvalidOperationException(
180✔
242
            "Cannot convert infinite timestamp value to DateTime or DateTimeOffset. " +
180✔
243
            "Use DuckDBTimestamp to read this value and check IsInfinity, IsPositiveInfinity, or IsNegativeInfinity before converting to .NET types.");
180✔
244
    }
245
}
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