/
DateTimeField.cs
328 lines (297 loc) · 11 KB
/
DateTimeField.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
using System.Text.Json;
namespace Serenity.Data;
/// <summary>
/// Field with a DateTime value
/// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="DateTimeField"/> class.
/// </remarks>
/// <param name="collection">The collection.</param>
/// <param name="name">The name.</param>
/// <param name="caption">The caption.</param>
/// <param name="size">The size.</param>
/// <param name="flags">The flags.</param>
/// <param name="getValue">The get value.</param>
/// <param name="setValue">The set value.</param>
public sealed class DateTimeField(ICollection<Field> collection, string name, LocalText caption = null, int size = 0, FieldFlags flags = FieldFlags.Default,
Func<IRow, DateTime?> getValue = null, Action<IRow, DateTime?> setValue = null) : GenericValueField<DateTime>(collection, FieldType.DateTime, name, caption, size, flags, getValue, setValue)
{
/// <summary>
/// Static factory for field, for backward compatibility, avoid using.
/// </summary>
/// <param name="collection">The collection.</param>
/// <param name="name">The name.</param>
/// <param name="caption">The caption.</param>
/// <param name="size">The size.</param>
/// <param name="flags">The flags.</param>
/// <param name="getValue">The get value.</param>
/// <param name="setValue">The set value.</param>
/// <returns></returns>
public static DateTimeField Factory(ICollection<Field> collection, string name, LocalText caption, int size, FieldFlags flags,
Func<IRow, DateTime?> getValue, Action<IRow, DateTime?> setValue)
{
return new DateTimeField(collection, name, caption, size, flags, getValue, setValue);
}
/// <summary>
/// Converts the value.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="provider">The provider.</param>
/// <returns></returns>
public override object ConvertValue(object source, IFormatProvider provider)
{
if (source is Newtonsoft.Json.Linq.JValue jValue)
source = jValue.Value;
if (source == null)
return null;
else
{
if (source is DateTime dt)
return dt;
if (source is DateTimeOffset dto)
return dto.DateTime;
return Convert.ChangeType(source, typeof(DateTime), provider);
}
}
/// <summary>
/// Gets field value from a data reader.
/// </summary>
/// <param name="reader">The reader.</param>
/// <param name="index">The index.</param>
/// <param name="row">The row.</param>
/// <exception cref="ArgumentNullException">reader</exception>
public override void GetFromReader(IDataReader reader, int index, IRow row)
{
if (reader == null)
throw new ArgumentNullException("reader");
if (reader.IsDBNull(index))
_setValue(row, null);
else
{
var value = reader.GetValue(index);
DateTime datetime = value is DateTimeOffset dto ?
dto.DateTime : (value is DateTime dt ? dt : Convert.ToDateTime(value));
if (DateTimeKind != System.DateTimeKind.Unspecified)
datetime = DateTime.SpecifyKind(datetime, DateTimeKind);
_setValue(row, datetime);
}
row.FieldAssignedValue(this);
}
private DateTimeKind? dateTimeKind;
/// <summary>
/// Gets or sets a value indicating whether the field is date only, e.g. no time part.
/// </summary>
/// <value>
/// <c>true</c> if date only; otherwise, <c>false</c>.
/// </value>
public bool DateOnly
{
get
{
return dateTimeKind == null;
}
set
{
if (value != (dateTimeKind == null))
dateTimeKind = value ? null : DateTimeKind.Unspecified;
}
}
/// <summary>
/// Gets or sets the kind of the date time.
/// Unspecified means no date/time conversions, Local means local time zone, Utc means UTC time zone
/// </summary>
/// <value>
/// The kind of the date time.
/// </value>
public DateTimeKind DateTimeKind
{
get { return dateTimeKind ?? DateTimeKind.Unspecified; }
set { dateTimeKind = value; }
}
/// <summary>
/// Converts the value to specified DateTimeKind
/// </summary>
/// <param name="value">The value.</param>
/// <param name="dateTimeKind">Kind of the date time.</param>
/// <returns></returns>
public static DateTime ToDateTimeKind(DateTime value, DateTimeKind? dateTimeKind)
{
if (dateTimeKind == null || dateTimeKind == System.DateTimeKind.Unspecified)
return value;
if (dateTimeKind == DateTimeKind.Utc)
return value.ToUniversalTime();
else
return value.ToLocalTime();
}
/// <summary>
/// Converts the value to specified DateTimeKind
/// </summary>
/// <param name="value">The value.</param>
/// <param name="dateTimeKind">Kind of the date time.</param>
/// <returns></returns>
public static DateTime ToDateTimeKind(DateTimeOffset value, DateTimeKind? dateTimeKind)
{
if (dateTimeKind == null || dateTimeKind == System.DateTimeKind.Unspecified)
return value.DateTime;
if (dateTimeKind == DateTimeKind.Utc)
return value.UtcDateTime;
else
return value.LocalDateTime;
}
private DateTime ToDateTimeKind(DateTimeOffset value)
{
return ToDateTimeKind(value, dateTimeKind);
}
private DateTime ToDateTimeKind(DateTime value)
{
return ToDateTimeKind(value, dateTimeKind);
}
/// <summary>
/// Gets or sets the value of this field with the specified row.
/// </summary>
/// <param name="row">The row.</param>
public new DateTime? this[IRow row]
{
get
{
CheckUnassignedRead(row);
return _getValue(row);
}
set
{
if (value != null)
_setValue(row, ToDateTimeKind(value.Value));
else
_setValue(row, value);
row.FieldAssignedValue(this);
}
}
/// <summary>
/// Gets the value of this field in specified row as object.
/// </summary>
/// <param name="row">The row.</param>
public override object AsObject(IRow row)
{
CheckUnassignedRead(row);
return _getValue(row);
}
/// <summary>
/// Sets the value of this field in specified row as object.
/// </summary>
/// <param name="row">The row.</param>
/// <param name="value">The value.</param>
public override void AsObject(IRow row, object value)
{
if (value == null)
_setValue(row, null);
else
_setValue(row, ToDateTimeKind((DateTime)value));
row.FieldAssignedValue(this);
}
/// <summary>
/// Serializes this fields value to JSON
/// </summary>
/// <param name="writer">The writer.</param>
/// <param name="row">The row.</param>
/// <param name="serializer">The serializer.</param>
public override void ValueToJson(Newtonsoft.Json.JsonWriter writer, IRow row, Newtonsoft.Json.JsonSerializer serializer)
{
var value = _getValue(row);
if (value.HasValue)
{
var dt = value.Value;
if (DateTimeKind == DateTimeKind.Local)
dt = dt.ToUniversalTime();
writer.WriteValue(dt.ToString(
(DateTimeKind == System.DateTimeKind.Unspecified ?
DateHelper.ISODateTimeFormatLocal :
DateHelper.ISODateTimeFormatUTC), CultureInfo.InvariantCulture));
}
else
writer.WriteNull();
}
/// <summary>
/// Deserializes this fields value from JSON
/// </summary>
/// <param name="reader">The reader.</param>
/// <param name="row">The row.</param>
/// <param name="serializer">The serializer.</param>
/// <exception cref="ArgumentNullException">reader</exception>
public override void ValueFromJson(Newtonsoft.Json.JsonReader reader, IRow row, Newtonsoft.Json.JsonSerializer serializer)
{
if (reader == null)
throw new ArgumentNullException("reader");
switch (reader.TokenType)
{
case Newtonsoft.Json.JsonToken.Null:
case Newtonsoft.Json.JsonToken.Undefined:
_setValue(row, null);
break;
case Newtonsoft.Json.JsonToken.Date:
var obj = reader.Value;
DateTime value;
if (obj is DateTime dt)
value = dt;
else if (obj is DateTimeOffset dto)
{
_setValue(row, ToDateTimeKind(dto));
break;
}
else
value = Convert.ToDateTime(obj, CultureInfo.InvariantCulture);
_setValue(row, ToDateTimeKind(value));
break;
case Newtonsoft.Json.JsonToken.String:
var s = ((string)reader.Value).TrimToNull();
if (s == null)
_setValue(row, null);
else
_setValue(row, ToDateTimeKind(Convert.ToDateTime(s, CultureInfo.InvariantCulture)));
break;
default:
throw JsonUnexpectedToken(reader);
}
row.FieldAssignedValue(this);
}
/// <inheritdoc/>
public override void ValueFromJson(ref Utf8JsonReader reader, IRow row, JsonSerializerOptions options)
{
switch (reader.TokenType)
{
case JsonTokenType.Null:
_setValue(row, null);
break;
case JsonTokenType.String:
var s = reader.GetString();
if (string.IsNullOrWhiteSpace(s))
_setValue(row, null);
else if (reader.TryGetDateTimeOffset(out var dtofs))
_setValue(row, ToDateTimeKind(dtofs));
else if (reader.TryGetDateTime(out var dt))
_setValue(row, ToDateTimeKind(dt));
else
_setValue(row, ToDateTimeKind(Convert.ToDateTime(s.Trim(), CultureInfo.InvariantCulture)));
break;
default:
throw UnexpectedJsonToken(ref reader);
}
row.FieldAssignedValue(this);
}
/// <inheritdoc/>
public override void ValueToJson(Utf8JsonWriter writer, IRow row, JsonSerializerOptions options)
{
var value = _getValue(row);
if (value == null)
writer.WriteNullValue();
else
{
var dt = value.Value;
if (DateTimeKind == DateTimeKind.Local)
dt = dt.ToUniversalTime();
writer.WriteStringValue(dt.ToString(
(DateTimeKind == DateTimeKind.Unspecified ?
DateHelper.ISODateTimeFormatLocal :
DateHelper.ISODateTimeFormatUTC), CultureInfo.InvariantCulture));
}
}
}