forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlTimeTZ.cs
More file actions
executable file
·369 lines (310 loc) · 11.2 KB
/
Copy pathNpgsqlTimeTZ.cs
File metadata and controls
executable file
·369 lines (310 loc) · 11.2 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
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 NpgsqlTimeTZ : IEquatable<NpgsqlTimeTZ>, IComparable<NpgsqlTimeTZ>, IComparable, IComparer<NpgsqlTimeTZ>,
IComparer
{
public static readonly NpgsqlTimeTZ AllBalls = new NpgsqlTimeTZ(NpgsqlTime.AllBalls, NpgsqlTimeZone.UTC);
public static NpgsqlTimeTZ Now
{
get { return new NpgsqlTimeTZ(NpgsqlTime.Now); }
}
public static NpgsqlTimeTZ LocalMidnight(NpgsqlDate date)
{
return new NpgsqlTimeTZ(NpgsqlTime.AllBalls, NpgsqlTimeZone.LocalTimeZone(date));
}
private readonly NpgsqlTime _localTime;
private readonly NpgsqlTimeZone _timeZone;
public NpgsqlTimeTZ(NpgsqlTime localTime, NpgsqlTimeZone timeZone)
{
_localTime = localTime;
_timeZone = timeZone;
}
public NpgsqlTimeTZ(NpgsqlTime localTime)
: this(localTime, NpgsqlTimeZone.CurrentTimeZone)
{
}
public NpgsqlTimeTZ(long ticks)
: this(new NpgsqlTime(ticks))
{
}
public NpgsqlTimeTZ(TimeSpan time)
: this(new NpgsqlTime(time))
{
}
public NpgsqlTimeTZ(NpgsqlInterval time)
: this(new NpgsqlTime(time))
{
}
public NpgsqlTimeTZ(NpgsqlTimeTZ copyFrom)
: this(copyFrom._localTime, copyFrom._timeZone)
{
}
public NpgsqlTimeTZ(int hours, int minutes, int seconds)
: this(new NpgsqlTime(hours, minutes, seconds))
{
}
public NpgsqlTimeTZ(int hours, int minutes, int seconds, int microseconds)
: this(new NpgsqlTime(hours, minutes, seconds, microseconds))
{
}
public NpgsqlTimeTZ(int hours, int minutes, decimal seconds)
: this(new NpgsqlTime(hours, minutes, seconds))
{
}
public NpgsqlTimeTZ(int hours, int minutes, double seconds)
: this(new NpgsqlTime(hours, minutes, seconds))
{
}
public NpgsqlTimeTZ(long ticks, NpgsqlTimeZone timeZone)
: this(new NpgsqlTime(ticks), timeZone)
{
}
public NpgsqlTimeTZ(TimeSpan time, NpgsqlTimeZone timeZone)
: this(new NpgsqlTime(time), timeZone)
{
}
public NpgsqlTimeTZ(NpgsqlInterval time, NpgsqlTimeZone timeZone)
: this(new NpgsqlTime(time), timeZone)
{
}
public NpgsqlTimeTZ(int hours, int minutes, int seconds, NpgsqlTimeZone timeZone)
: this(new NpgsqlTime(hours, minutes, seconds), timeZone)
{
}
public NpgsqlTimeTZ(int hours, int minutes, int seconds, int microseconds, NpgsqlTimeZone timeZone)
: this(new NpgsqlTime(hours, minutes, seconds, microseconds), timeZone)
{
}
public NpgsqlTimeTZ(int hours, int minutes, decimal seconds, NpgsqlTimeZone timeZone)
: this(new NpgsqlTime(hours, minutes, seconds), timeZone)
{
}
public NpgsqlTimeTZ(int hours, int minutes, double seconds, NpgsqlTimeZone timeZone)
: this(new NpgsqlTime(hours, minutes, seconds), timeZone)
{
}
public override string ToString()
{
return string.Format("{0}{1}", _localTime, _timeZone);
}
public static NpgsqlTimeTZ Parse(string str)
{
if (str == null) {
throw new ArgumentNullException();
}
try {
int idx = Math.Max(str.IndexOf('+'), str.IndexOf('-'));
if (idx == -1) {
throw new FormatException();
}
return new NpgsqlTimeTZ(NpgsqlTime.Parse(str.Substring(0, idx)), NpgsqlTimeZone.Parse(str.Substring(idx)));
} catch (OverflowException) {
throw;
} catch {
throw new FormatException();
}
}
public NpgsqlTime LocalTime
{
get { return _localTime; }
}
public NpgsqlTimeZone TimeZone
{
get { return _timeZone; }
}
public NpgsqlTime UTCTime
{
get { return AtTimeZone(NpgsqlTimeZone.UTC).LocalTime; }
}
public NpgsqlTimeTZ AtTimeZone(NpgsqlTimeZone timeZone)
{
return new NpgsqlTimeTZ(LocalTime - _timeZone + timeZone, timeZone);
}
internal NpgsqlTimeTZ AtTimeZone(NpgsqlTimeZone timeZone, out int overflow)
{
return
new NpgsqlTimeTZ(LocalTime.Add(timeZone - (NpgsqlInterval)(_timeZone), out overflow), timeZone);
}
public long Ticks
{
get { return _localTime.Ticks; }
}
/// <summary>
/// Gets the number of whole microseconds held in the instance.
/// <returns>An integer in the range [0, 999999].</returns>
/// </summary>
public int Microseconds
{
get { return _localTime.Microseconds; }
}
/// <summary>
/// Gets the number of whole milliseconds held in the instance.
/// <returns>An integer in the range [0, 999].</returns>
/// </summary>
public int Milliseconds
{
get { return _localTime.Milliseconds; }
}
/// <summary>
/// Gets the number of whole seconds held in the instance.
/// <returns>An interger in the range [0, 59].</returns>
/// </summary>
public int Seconds
{
get { return _localTime.Seconds; }
}
/// <summary>
/// Gets the number of whole minutes held in the instance.
/// <returns>An integer in the range [0, 59].</returns>
/// </summary>
public int Minutes
{
get { return _localTime.Minutes; }
}
/// <summary>
/// Gets the number of whole hours held in the instance.
/// <remarks>Note that the time 24:00:00 can be stored for roundtrip compatibility. Any calculations on such a
/// value will normalised it to 00:00:00.</remarks>
/// </summary>
public int Hours
{
get { return _localTime.Hours; }
}
/// <summary>
/// Normalise this time; if it is 24:00:00, convert it to 00:00:00
/// </summary>
/// <returns>This time, normalised</returns>
public NpgsqlTimeTZ Normalize()
{
return new NpgsqlTimeTZ(_localTime.Normalize(), _timeZone);
}
public bool Equals(NpgsqlTimeTZ other)
{
return _localTime.Equals(other._localTime) && _timeZone.Equals(other._timeZone);
}
public override bool Equals(object obj)
{
return obj != null && obj is NpgsqlTimeTZ && Equals((NpgsqlTimeTZ)obj);
}
public override int GetHashCode()
{
return _localTime.GetHashCode() ^ PGUtil.RotateShift(_timeZone.GetHashCode(), 24);
}
/// <summary>
/// Compares this with another <see cref="NpgsqlTimeTZ"/>. As per postgres' rules,
/// first the times are compared as if they were both in the same timezone. If they are equal then
/// then timezones are compared (+01:00 being "smaller" than -01:00).
/// </summary>
/// <param name="other">the <see cref="NpgsqlTimeTZ"/> to compare with.</param>
/// <returns>An integer which is 0 if they are equal, < 0 if this is the smaller and > 0 if this is the larger.</returns>
public int CompareTo(NpgsqlTimeTZ other)
{
int cmp = AtTimeZone(NpgsqlTimeZone.UTC).LocalTime.CompareTo(other.AtTimeZone(NpgsqlTimeZone.UTC).LocalTime);
return cmp == 0 ? _timeZone.CompareTo(other._timeZone) : cmp;
}
public int CompareTo(object obj)
{
if (obj == null) {
return 1;
}
if (obj is NpgsqlTimeTZ) {
return CompareTo((NpgsqlTimeTZ)obj);
}
throw new ArgumentException();
}
public int Compare(NpgsqlTimeTZ x, NpgsqlTimeTZ 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 static bool operator ==(NpgsqlTimeTZ x, NpgsqlTimeTZ y)
{
return x.Equals(y);
}
public static bool operator !=(NpgsqlTimeTZ x, NpgsqlTimeTZ y)
{
return !(x == y);
}
public static bool operator <(NpgsqlTimeTZ x, NpgsqlTimeTZ y)
{
return x.CompareTo(y) < 0;
}
public static bool operator >(NpgsqlTimeTZ x, NpgsqlTimeTZ y)
{
return x.CompareTo(y) > 0;
}
public static bool operator <=(NpgsqlTimeTZ x, NpgsqlTimeTZ y)
{
return x.CompareTo(y) <= 0;
}
public static bool operator >=(NpgsqlTimeTZ x, NpgsqlTimeTZ y)
{
return x.CompareTo(y) >= 0;
}
public NpgsqlTimeTZ Add(NpgsqlInterval interval)
{
return new NpgsqlTimeTZ(_localTime.Add(interval), _timeZone);
}
internal NpgsqlTimeTZ Add(NpgsqlInterval interval, out int overflow)
{
return new NpgsqlTimeTZ(_localTime.Add(interval, out overflow), _timeZone);
}
public NpgsqlTimeTZ Subtract(NpgsqlInterval interval)
{
return new NpgsqlTimeTZ(_localTime.Subtract(interval), _timeZone);
}
public NpgsqlInterval Subtract(NpgsqlTimeTZ earlier)
{
return _localTime.Subtract(earlier.AtTimeZone(_timeZone)._localTime);
}
public static NpgsqlTimeTZ operator +(NpgsqlTimeTZ time, NpgsqlInterval interval)
{
return time.Add(interval);
}
public static NpgsqlTimeTZ operator +(NpgsqlInterval interval, NpgsqlTimeTZ time)
{
return time + interval;
}
public static NpgsqlTimeTZ operator -(NpgsqlTimeTZ time, NpgsqlInterval interval)
{
return time.Subtract(interval);
}
public static NpgsqlInterval operator -(NpgsqlTimeTZ later, NpgsqlTimeTZ earlier)
{
return later.Subtract(earlier);
}
public static explicit operator NpgsqlTimeTZ(TimeSpan time)
{
return new NpgsqlTimeTZ(new NpgsqlTime(time));
}
public static explicit operator TimeSpan(NpgsqlTimeTZ time)
{
return (TimeSpan)time.LocalTime;
}
public static explicit operator DateTime(NpgsqlTimeTZ time)
{
// LocalTime property is actually time local to TimeZone
return new DateTime(time.AtTimeZone(NpgsqlTimeZone.CurrentTimeZone).Ticks, DateTimeKind.Local);
}
}
}