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

Giorgi / DuckDB.NET / 8331073809

18 Mar 2024 05:34PM UTC coverage: 89.771% (+0.06%) from 89.716%
8331073809

push

github

Giorgi
Update to DuckDB 0.10.1

584 of 680 branches covered (85.88%)

Branch coverage included in aggregate %.

1373 of 1500 relevant lines covered (91.53%)

12610.11 hits per line

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

90.32
/DuckDB.NET.Data/Internal/Reader/DateTimeVectorDataReader.cs
1
using System;
2
using DuckDB.NET.Data.Extensions;
3
using DuckDB.NET.Native;
4

5
namespace DuckDB.NET.Data.Internal.Reader;
6

7
internal class DateTimeVectorDataReader : VectorDataReaderBase
8
{
9
    private static readonly Type DateTimeType = typeof(DateTime);
3✔
10
    private static readonly Type DateTimeNullableType = typeof(DateTime?);
3✔
11

12
    private static readonly Type DateTimeOffsetType = typeof(DateTimeOffset);
3✔
13
    private static readonly Type DateTimeOffsetNullableType = typeof(DateTimeOffset?);
3✔
14

15
#if NET6_0_OR_GREATER
16
    private static readonly Type DateOnlyType = typeof(DateOnly);
3✔
17
    private static readonly Type DateOnlyNullableType = typeof(DateOnly?);
3✔
18

19
    private static readonly Type TimeOnlyType = typeof(TimeOnly);
3✔
20
    private static readonly Type TimeOnlyNullableType = typeof(TimeOnly?);
3✔
21

22
#endif
23

24
    internal unsafe DateTimeVectorDataReader(void* dataPointer, ulong* validityMaskPointer, DuckDBType columnType, string columnName) : base(dataPointer, validityMaskPointer, columnType, columnName)
1,872✔
25
    {
26
    }
1,872✔
27

28
    protected override T GetValidValue<T>(ulong offset, Type targetType)
29
    {
30
        if (DuckDBType == DuckDBType.Date)
147✔
31
        {
32
            var dateOnly = GetDateOnly(offset);
33✔
33

34
            if (targetType == DateTimeType || targetType == DateTimeNullableType)
33✔
35
            {
36
                var dateTime = (DateTime)dateOnly;
9✔
37
                return (T)(object)dateTime;
9✔
38
            }
39

40
#if NET6_0_OR_GREATER
41
            if (targetType == DateOnlyType || targetType == DateOnlyNullableType)
24✔
42
            {
43
                var dateTime = (DateOnly)dateOnly;
9✔
44
                return (T)(object)dateTime;
9✔
45
            }
46
#endif
47
            return (T)(object)dateOnly;
15✔
48
        }
49

50
        if (DuckDBType == DuckDBType.Time)
114✔
51
        {
52
            var timeOnly = GetTimeOnly(offset);
42✔
53

54
            if (targetType == DateTimeType || targetType == DateTimeNullableType)
42!
55
            {
56
                var dateTime = (DateTime)timeOnly;
×
57
                return (T)(object)dateTime;
×
58
            }
59

60
#if NET6_0_OR_GREATER
61
            if (targetType == TimeOnlyType || targetType == TimeOnlyNullableType)
42✔
62
            {
63
                var dateTime = (TimeOnly)timeOnly;
18✔
64
                return (T)(object)dateTime;
18✔
65
            }
66
#endif
67
            return (T)(object)timeOnly;
24✔
68
        }
69

70
        if (DuckDBType == DuckDBType.TimeTz)
72✔
71
        {
72
            var timeTz = GetTimeTz(offset);
24✔
73

74
            if (targetType == DateTimeOffsetType || targetType == DateTimeOffsetNullableType)
24✔
75
            {
76
                var dateTimeOffset = new DateTimeOffset(timeTz.Time.ToDateTime(), TimeSpan.FromSeconds(timeTz.Offset));
18✔
77
                return (T)(object)dateTimeOffset;
18✔
78
            }
79

80
            return (T)(object)timeTz;
6✔
81
        }
82

83
        if (DuckDBType == DuckDBType.Timestamp || DuckDBType == DuckDBType.TimestampTz)
48✔
84
        {
85
            return ReadTimestamp<T>(offset, targetType);
30✔
86
        }
87

88
        if (DuckDBType == DuckDBType.TimestampS)
18✔
89
        {
90
            return ReadTimestamp<T>(offset, targetType, 1000000);
6✔
91
        }
92

93
        if (DuckDBType == DuckDBType.TimestampMs)
12✔
94
        {
95
            return ReadTimestamp<T>(offset, targetType, 1000);
6✔
96
        }
97

98
        if (DuckDBType == DuckDBType.TimestampNs)
6!
99
        {
100
            return ReadTimestamp<T>(offset, targetType, 1, 1000);
6✔
101
        }
102

103
        return base.GetValidValue<T>(offset, targetType);
×
104
    }
105

106
    private T ReadTimestamp<T>(ulong offset, Type targetType, int factor = 1, int divisor = 1)
107
    {
108
        var timestampStruct = GetFieldData<DuckDBTimestampStruct>(offset);
48✔
109

110
        timestampStruct.Micros = timestampStruct.Micros * factor / divisor;
48✔
111

112
        if (targetType == DateTimeType || targetType == DateTimeNullableType)
48✔
113
        {
114
            var dateTime = timestampStruct.ToDateTime();
24✔
115
            return (T)(object)dateTime;
24✔
116
        }
117

118
        var timestamp = NativeMethods.DateTime.DuckDBFromTimestamp(timestampStruct);
24✔
119
        return (T)(object)timestamp;
24✔
120
    }
121

122
    internal override object GetValue(ulong offset, Type targetType)
123
    {
124
        return DuckDBType switch
270!
125
        {
270✔
126
            DuckDBType.Date => GetDate(offset, targetType),
54✔
127
            DuckDBType.Time => GetTime(offset, targetType),
54✔
128
            DuckDBType.TimeTz => GetDateTimeOffset(offset, targetType),
24✔
129
            DuckDBType.Timestamp => GetDateTime(offset, targetType),
114✔
130
            DuckDBType.TimestampTz => GetDateTime(offset, targetType),
6✔
131
            DuckDBType.TimestampS => GetDateTime(offset, targetType, 1000000),
6✔
132
            DuckDBType.TimestampMs => GetDateTime(offset, targetType, 1000),
6✔
133
            DuckDBType.TimestampNs => GetDateTime(offset, targetType, 1, 1000),
6✔
134
            _ => base.GetValue(offset, targetType)
×
135
        };
270✔
136
    }
137

138
    private DuckDBTimeTz GetTimeTz(ulong offset)
139
    {
140
        var data = GetFieldData<DuckDBTimeTzStruct>(offset);
48✔
141

142
        return NativeMethods.DateTime.DuckDBFromTimeTz(data);
48✔
143
    }
144

145
    private DuckDBTimeOnly GetTimeOnly(ulong offset)
146
    {
147
        return NativeMethods.DateTime.DuckDBFromTime(GetFieldData<DuckDBTime>(offset));
96✔
148
    }
149

150
    private DuckDBDateOnly GetDateOnly(ulong offset)
151
    {
152
        return NativeMethods.DateTime.DuckDBFromDate(GetFieldData<DuckDBDate>(offset));
87✔
153
    }
154

155
    private object GetDate(ulong offset, Type targetType)
156
    {
157
        var dateOnly = GetDateOnly(offset);
54✔
158
        if (targetType == DateTimeType)
54✔
159
        {
160
            return (DateTime)dateOnly;
12✔
161
        }
162

163
#if NET6_0_OR_GREATER
164
        if (targetType == DateOnlyType)
42✔
165
        {
166
            return (DateOnly)dateOnly;
24✔
167
        }
168
#endif
169

170
        return dateOnly;
18✔
171
    }
172

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

181
#if NET6_0_OR_GREATER
182
        if (targetType == TimeOnlyType)
54✔
183
        {
184
            return (TimeOnly)timeOnly;
48✔
185
        }
186
#endif
187

188
        return timeOnly;
6✔
189
    }
190

191
    private object GetDateTime(ulong offset, Type targetType, int factor = 1, int divisor = 1)
192
    {
193
        var data = GetFieldData<DuckDBTimestampStruct>(offset);
138✔
194

195
        data.Micros = (data.Micros * factor / divisor);
138✔
196

197
        if (targetType == typeof(DateTime))
138✔
198
        {
199
            return data.ToDateTime();
114✔
200
        }
201

202
        return NativeMethods.DateTime.DuckDBFromTimestamp(data);
24✔
203
    }
204

205
    private object GetDateTimeOffset(ulong offset, Type targetType)
206
    {
207
        var timeTz = GetTimeTz(offset);
24✔
208

209
        if (targetType == typeof(DateTimeOffset))
24✔
210
        {
211
            return new DateTimeOffset(timeTz.Time.ToDateTime(), TimeSpan.FromSeconds(timeTz.Offset));
18✔
212
        }
213

214
        return timeTz;
6✔
215
    }
216
}
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