forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlTimestamp.cs
More file actions
executable file
·479 lines (415 loc) · 14.3 KB
/
Copy pathNpgsqlTimestamp.cs
File metadata and controls
executable file
·479 lines (415 loc) · 14.3 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Npgsql;
// ReSharper disable once CheckNamespace
namespace NpgsqlTypes
{
[Serializable]
public struct NpgsqlTimeStamp : IEquatable<NpgsqlTimeStamp>, IComparable<NpgsqlTimeStamp>, IComparable,
IComparer<NpgsqlTimeStamp>, IComparer
{
private enum TimeType
{
Finite,
Infinity,
MinusInfinity
}
public static readonly NpgsqlTimeStamp Epoch = new NpgsqlTimeStamp(NpgsqlDate.Epoch);
public static readonly NpgsqlTimeStamp Era = new NpgsqlTimeStamp(NpgsqlDate.Era);
public static readonly NpgsqlTimeStamp Infinity =
new NpgsqlTimeStamp(TimeType.Infinity, NpgsqlDate.Era, NpgsqlTime.AllBalls);
public static readonly NpgsqlTimeStamp MinusInfinity =
new NpgsqlTimeStamp(TimeType.MinusInfinity, NpgsqlDate.Era, NpgsqlTime.AllBalls);
public static NpgsqlTimeStamp Now
{
get { return new NpgsqlTimeStamp(NpgsqlDate.Now, NpgsqlTime.Now); }
}
public static NpgsqlTimeStamp Today
{
get { return new NpgsqlTimeStamp(NpgsqlDate.Now); }
}
public static NpgsqlTimeStamp Yesterday
{
get { return new NpgsqlTimeStamp(NpgsqlDate.Yesterday); }
}
public static NpgsqlTimeStamp Tomorrow
{
get { return new NpgsqlTimeStamp(NpgsqlDate.Tomorrow); }
}
private readonly NpgsqlDate _date;
private readonly NpgsqlTime _time;
private readonly TimeType _type;
private NpgsqlTimeStamp(TimeType type, NpgsqlDate date, NpgsqlTime time)
{
_type = type;
_date = date;
_time = time;
}
public NpgsqlTimeStamp(NpgsqlDate date, NpgsqlTime time)
: this(TimeType.Finite, date, time)
{
}
public NpgsqlTimeStamp(NpgsqlDate date)
: this(date, NpgsqlTime.AllBalls)
{
}
public NpgsqlTimeStamp(int year, int month, int day, int hours, int minutes, int seconds)
: this(new NpgsqlDate(year, month, day), new NpgsqlTime(hours, minutes, seconds))
{
}
public NpgsqlDate Date
{
get { return _date; }
}
public NpgsqlTime Time
{
get { return _time; }
}
public int DayOfYear
{
get { return _date.DayOfYear; }
}
public int Year
{
get { return _date.Year; }
}
public int Month
{
get { return _date.Month; }
}
public int Day
{
get { return _date.Day; }
}
public DayOfWeek DayOfWeek
{
get { return _date.DayOfWeek; }
}
public bool IsLeapYear
{
get { return _date.IsLeapYear; }
}
public NpgsqlTimeStamp AddDays(int days)
{
switch (_type) {
case TimeType.Infinity:
case TimeType.MinusInfinity:
return this;
default:
return new NpgsqlTimeStamp(_date.AddDays(days), _time);
}
}
public NpgsqlTimeStamp AddYears(int years)
{
switch (_type) {
case TimeType.Infinity:
case TimeType.MinusInfinity:
return this;
default:
return new NpgsqlTimeStamp(_date.AddYears(years), _time);
}
}
public NpgsqlTimeStamp AddMonths(int months)
{
switch (_type) {
case TimeType.Infinity:
case TimeType.MinusInfinity:
return this;
default:
return new NpgsqlTimeStamp(_date.AddMonths(months), _time);
}
}
public long Ticks
{
get { return _date.DaysSinceEra * NpgsqlInterval.TicksPerDay + _time.Ticks; }
}
public int Microseconds
{
get { return _time.Microseconds; }
}
public int Milliseconds
{
get { return _time.Milliseconds; }
}
public int Seconds
{
get { return _time.Seconds; }
}
public int Minutes
{
get { return _time.Minutes; }
}
public int Hours
{
get { return _time.Hours; }
}
public bool IsFinite
{
get { return _type == TimeType.Finite; }
}
public bool IsInfinity
{
get { return _type == TimeType.Infinity; }
}
public bool IsMinusInfinity
{
get { return _type == TimeType.MinusInfinity; }
}
public NpgsqlTimeStamp Normalize()
{
return Add(NpgsqlInterval.Zero);
}
public override string ToString()
{
switch (_type) {
case TimeType.Infinity:
return "infinity";
case TimeType.MinusInfinity:
return "-infinity";
default:
return string.Format("{0} {1}", _date, _time);
}
}
public static NpgsqlTimeStamp Parse(string str)
{
if (str == null) {
throw new NullReferenceException();
}
switch (str = str.Trim().ToLowerInvariant()) {
case "infinity":
return Infinity;
case "-infinity":
return MinusInfinity;
default:
try {
int idxSpace = str.IndexOf(' ');
string datePart = str.Substring(0, idxSpace);
if (str.Contains("bc")) {
datePart += " BC";
}
int idxSecond = str.IndexOf(' ', idxSpace + 1);
if (idxSecond == -1) {
idxSecond = str.Length;
}
string timePart = str.Substring(idxSpace + 1, idxSecond - idxSpace - 1);
return new NpgsqlTimeStamp(NpgsqlDate.Parse(datePart), NpgsqlTime.Parse(timePart));
} catch (OverflowException) {
throw;
} catch {
throw new FormatException();
}
}
}
/// <summary>
/// Creates a NpgsqlTimeStamp from backend binary format
/// </summary>
/// <param name="value">Number of microseconds since 2000-01-01 00:00:00</param>
internal static NpgsqlTimeStamp FromInt64(long value)
{
if (value == long.MaxValue)
return Infinity;
if (value == long.MinValue)
return MinusInfinity;
if (value >= 0) {
int date = (int)(value / 86400000000L);
long time = value % 86400000000L;
date += 730119; // 730119 = days since era (0001-01-01) for 2000-01-01
time *= 10; // To 100ns
return new NpgsqlTimeStamp(new NpgsqlDate(date), new NpgsqlTime(time));
} else {
value = -value;
int date = (int)(value / 86400000000L);
long time = value % 86400000000L;
if (time != 0) {
++date;
time = 86400000000L - time;
}
date = 730119 - date; // 730119 = days since era (0001-01-01) for 2000-01-01
time *= 10; // To 100ns
return new NpgsqlTimeStamp(new NpgsqlDate(date), new NpgsqlTime(time));
}
}
public bool Equals(NpgsqlTimeStamp other)
{
switch (_type) {
case TimeType.Infinity:
return other._type == TimeType.Infinity;
case TimeType.MinusInfinity:
return other._type == TimeType.MinusInfinity;
default:
return other._type == TimeType.Finite && _date.Equals(other._date) && _time.Equals(other._time);
}
}
public override bool Equals(object obj)
{
return obj != null && obj is NpgsqlTimeStamp && Equals((NpgsqlTimeStamp)obj);
}
public override int GetHashCode()
{
switch (_type) {
case TimeType.Infinity:
return int.MaxValue;
case TimeType.MinusInfinity:
return int.MinValue;
default:
return _date.GetHashCode() ^ PGUtil.RotateShift(_time.GetHashCode(), 16);
}
}
public int CompareTo(NpgsqlTimeStamp other)
{
switch (_type) {
case TimeType.Infinity:
return other._type == TimeType.Infinity ? 0 : 1;
case TimeType.MinusInfinity:
return other._type == TimeType.MinusInfinity ? 0 : -1;
default:
switch (other._type) {
case TimeType.Infinity:
return -1;
case TimeType.MinusInfinity:
return 1;
default:
int cmp = _date.CompareTo(other._date);
return cmp == 0 ? _time.CompareTo(_time) : cmp;
}
}
}
public int CompareTo(object obj)
{
if (obj == null) {
return 1;
}
if (obj is NpgsqlTimeStamp) {
return CompareTo((NpgsqlTimeStamp)obj);
}
throw new ArgumentException();
}
public int Compare(NpgsqlTimeStamp x, NpgsqlTimeStamp y)
{
return x.CompareTo(y);
}
public int Compare(object x, object y)
{
if (x == null) {
return y == null ? 0 : -1;
}
if (y == null) {
return 1;
}
if (!(x is IComparable) || !(y is IComparable)) {
throw new ArgumentException();
}
return ((IComparable)x).CompareTo(y);
}
public NpgsqlTimeStampTZ AtTimeZone(NpgsqlTimeZone timeZoneFrom, NpgsqlTimeZone timeZoneTo)
{
int overflow;
NpgsqlTimeTZ adjusted = new NpgsqlTimeTZ(_time, timeZoneFrom).AtTimeZone(timeZoneTo, out overflow);
return new NpgsqlTimeStampTZ(_date.AddDays(overflow), adjusted);
}
public NpgsqlTimeStampTZ AtTimeZone(NpgsqlTimeZone timeZone)
{
return AtTimeZone(timeZone, NpgsqlTimeZone.LocalTimeZone(_date));
}
public NpgsqlTimeStamp Add(NpgsqlInterval interval)
{
switch (_type) {
case TimeType.Infinity:
case TimeType.MinusInfinity:
return this;
default:
int overflow;
NpgsqlTime time = _time.Add(interval, out overflow);
return new NpgsqlTimeStamp(_date.Add(interval, overflow), time);
}
}
public NpgsqlTimeStamp Subtract(NpgsqlInterval interval)
{
return Add(-interval);
}
public NpgsqlInterval Subtract(NpgsqlTimeStamp timestamp)
{
switch (_type) {
case TimeType.Infinity:
case TimeType.MinusInfinity:
throw new ArgumentOutOfRangeException("this", "You cannot subtract infinity timestamps");
}
switch (timestamp._type) {
case TimeType.Infinity:
case TimeType.MinusInfinity:
throw new ArgumentOutOfRangeException("timestamp", "You cannot subtract infinity timestamps");
}
return new NpgsqlInterval(0, _date.DaysSinceEra - timestamp._date.DaysSinceEra, _time.Ticks - timestamp._time.Ticks);
}
public static implicit operator NpgsqlTimeStamp(DateTime datetime)
{
if (datetime == DateTime.MaxValue) {
return Infinity;
} else if (datetime == DateTime.MinValue) {
return MinusInfinity;
} else {
return new NpgsqlTimeStamp(new NpgsqlDate(datetime), new NpgsqlTime(datetime.TimeOfDay));
}
}
public static implicit operator DateTime(NpgsqlTimeStamp timestamp)
{
switch (timestamp._type) {
case TimeType.Infinity:
return DateTime.MaxValue;
case TimeType.MinusInfinity:
return DateTime.MinValue;
default:
try {
return
new DateTime(timestamp.Date.DaysSinceEra * NpgsqlInterval.TicksPerDay + timestamp._time.Ticks,
DateTimeKind.Unspecified);
} catch {
throw new InvalidCastException();
}
}
}
public static NpgsqlTimeStamp operator +(NpgsqlTimeStamp timestamp, NpgsqlInterval interval)
{
return timestamp.Add(interval);
}
public static NpgsqlTimeStamp operator +(NpgsqlInterval interval, NpgsqlTimeStamp timestamp)
{
return timestamp.Add(interval);
}
public static NpgsqlTimeStamp operator -(NpgsqlTimeStamp timestamp, NpgsqlInterval interval)
{
return timestamp.Subtract(interval);
}
public static NpgsqlInterval operator -(NpgsqlTimeStamp x, NpgsqlTimeStamp y)
{
return x.Subtract(y);
}
public static bool operator ==(NpgsqlTimeStamp x, NpgsqlTimeStamp y)
{
return x.Equals(y);
}
public static bool operator !=(NpgsqlTimeStamp x, NpgsqlTimeStamp y)
{
return !(x == y);
}
public static bool operator <(NpgsqlTimeStamp x, NpgsqlTimeStamp y)
{
return x.CompareTo(y) < 0;
}
public static bool operator >(NpgsqlTimeStamp x, NpgsqlTimeStamp y)
{
return x.CompareTo(y) > 0;
}
public static bool operator <=(NpgsqlTimeStamp x, NpgsqlTimeStamp y)
{
return x.CompareTo(y) <= 0;
}
public static bool operator >=(NpgsqlTimeStamp x, NpgsqlTimeStamp y)
{
return x.CompareTo(y) >= 0;
}
}
}