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

Giorgi / DuckDB.NET / 12284417432

11 Dec 2024 08:32PM UTC coverage: 89.878% (-0.05%) from 89.926%
12284417432

push

github

Giorgi
Read as DateTimeOffset

1071 of 1225 branches covered (87.43%)

Branch coverage included in aggregate %.

5 of 7 new or added lines in 1 file covered. (71.43%)

2090 of 2292 relevant lines covered (91.19%)

765013.48 hits per line

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

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

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

7
internal sealed 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
#endif
22

23
    internal unsafe DateTimeVectorDataReader(void* dataPointer, ulong* validityMaskPointer, DuckDBType columnType, string columnName) : base(dataPointer, validityMaskPointer, columnType, columnName)
2,163✔
24
    {
25
    }
2,163✔
26

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

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

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

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

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

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

69
        if (DuckDBType == DuckDBType.TimeTz)
216✔
70
        {
71
            var timeTz = GetTimeTz(offset);
42✔
72

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

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

82
        return DuckDBType switch
174!
83
        {
174✔
84
            DuckDBType.Timestamp or DuckDBType.TimestampS or 
174✔
85
            DuckDBType.TimestampTz or DuckDBType.TimestampMs or 
174✔
86
            DuckDBType.TimestampNs => ReadTimestamp<T>(offset, targetType),
174✔
87
            _ => base.GetValidValue<T>(offset, targetType)
×
88
        };
174✔
89
    }
90

91
    private T ReadTimestamp<T>(ulong offset, Type targetType)
92
    {
93
        var (timestamp, additionalTicks) = GetFieldData<DuckDBTimestampStruct>(offset).ToDuckDBTimestamp(DuckDBType);
174✔
94

95
        if (targetType == DateTimeType || targetType == DateTimeNullableType)
174✔
96
        {
97
            var dateTime = timestamp.ToDateTime().AddTicks(additionalTicks);
78✔
98
            return (T)(object)dateTime;
78✔
99
        }
100

101
        if (targetType == DateTimeOffsetType || targetType == DateTimeOffsetNullableType)
96✔
102
        {
103
            var dateTime = timestamp.ToDateTime().AddTicks(additionalTicks);
72✔
104
            var dateTimeOffset = new DateTimeOffset(dateTime, TimeSpan.Zero);
72✔
105
            return (T)(object)dateTimeOffset;
72✔
106
        }
107

108
        return (T)(object)timestamp;
24✔
109
    }
110

111
    internal override object GetValue(ulong offset, Type targetType)
112
    {
113
        return DuckDBType switch
771,655!
114
        {
771,655✔
115
            DuckDBType.Date => GetDate(offset, targetType),
754,985✔
116
            DuckDBType.Time => GetTime(offset, targetType),
656✔
117
            DuckDBType.TimeTz => GetDateTimeOffset(offset, targetType),
429✔
118
            DuckDBType.Timestamp or DuckDBType.TimestampS or 
771,655✔
119
            DuckDBType.TimestampTz or DuckDBType.TimestampMs or
771,655✔
120
            DuckDBType.TimestampNs => GetDateTime(offset, targetType),
15,585✔
121
            _ => base.GetValue(offset, targetType)
×
122
        };
771,655✔
123
    }
124

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

129
        return NativeMethods.DateTimeHelpers.DuckDBFromTimeTz(data);
471✔
130
    }
131

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

137
    private DuckDBDateOnly GetDateOnly(ulong offset)
138
    {
139
        return NativeMethods.DateTimeHelpers.DuckDBFromDate(GetFieldData<DuckDBDate>(offset));
755,018✔
140
    }
141

142
    private object GetDate(ulong offset, Type targetType)
143
    {
144
        var dateOnly = GetDateOnly(offset);
754,985✔
145
        if (targetType == DateTimeType)
754,985✔
146
        {
147
            return (DateTime)dateOnly;
754,362✔
148
        }
149

150
#if NET6_0_OR_GREATER
151
        if (targetType == DateOnlyType)
623✔
152
        {
153
            return (DateOnly)dateOnly;
392✔
154
        }
155
#endif
156

157
        return dateOnly;
231✔
158
    }
159

160
    private object GetTime(ulong offset, Type targetType)
161
    {
162
        var timeOnly = GetTimeOnly(offset);
656✔
163
        if (targetType == DateTimeType)
656!
164
        {
165
            return (DateTime)timeOnly;
×
166
        }
167

168
#if NET6_0_OR_GREATER
169
        if (targetType == TimeOnlyType)
656✔
170
        {
171
            return (TimeOnly)timeOnly;
390✔
172
        }
173
#endif
174

175
        return timeOnly;
266✔
176
    }
177

178
    private object GetDateTime(ulong offset, Type targetType)
179
    {
180
        var (timestamp, additionalTicks) = GetFieldData<DuckDBTimestampStruct>(offset).ToDuckDBTimestamp(DuckDBType);
15,585✔
181

182
        if (targetType == typeof(DateTime))
15,585✔
183
        {
184
            var dateTime = timestamp.ToDateTime().AddTicks(additionalTicks);
15,561✔
185

186
            return dateTime;
15,561✔
187
        }
188

189
        if (targetType == DateTimeOffsetType)
24!
190
        {
NEW
191
            var dateTime = timestamp.ToDateTime().AddTicks(additionalTicks);
×
NEW
192
            return new DateTimeOffset(dateTime, TimeSpan.Zero);
×
193
        }
194

195
        return timestamp;
24✔
196
    }
197

198
    private object GetDateTimeOffset(ulong offset, Type targetType)
199
    {
200
        var timeTz = GetTimeTz(offset);
429✔
201

202
        if (targetType == typeof(DateTimeOffset))
429✔
203
        {
204
            return new DateTimeOffset(timeTz.Time.ToDateTime(), TimeSpan.FromSeconds(timeTz.Offset));
423✔
205
        }
206

207
        return timeTz;
6✔
208
    }
209
}
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