forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlTime.cs
More file actions
executable file
·403 lines (353 loc) · 12.8 KB
/
Copy pathNpgsqlTime.cs
File metadata and controls
executable file
·403 lines (353 loc) · 12.8 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// ReSharper disable once CheckNamespace
namespace NpgsqlTypes
{
[Serializable]
public struct NpgsqlTime : IEquatable<NpgsqlTime>, IComparable<NpgsqlTime>, IComparable, IComparer<NpgsqlTime>,
IComparer
{
public static readonly NpgsqlTime AllBalls = new NpgsqlTime(0);
public static NpgsqlTime Now
{
get { return new NpgsqlTime(DateTime.Now.TimeOfDay); }
}
private readonly long _ticks;
public NpgsqlTime(long ticks)
{
if (ticks == NpgsqlInterval.TicksPerDay) {
_ticks = ticks;
} else {
ticks %= NpgsqlInterval.TicksPerDay;
_ticks = ticks < 0 ? ticks + NpgsqlInterval.TicksPerDay : ticks;
}
}
public NpgsqlTime(TimeSpan time)
: this(time.Ticks)
{
}
public NpgsqlTime(NpgsqlInterval time)
: this(time.Ticks)
{
}
public NpgsqlTime(NpgsqlTime copyFrom)
: this(copyFrom.Ticks)
{
}
public NpgsqlTime(int hours, int minutes, int seconds)
: this(hours, minutes, seconds, 0)
{
}
public NpgsqlTime(int hours, int minutes, int seconds, int microseconds)
: this(
hours * NpgsqlInterval.TicksPerHour + minutes * NpgsqlInterval.TicksPerMinute + seconds * NpgsqlInterval.TicksPerSecond +
microseconds * NpgsqlInterval.TicksPerMicrosecond)
{
}
public NpgsqlTime(int hours, int minutes, decimal seconds)
: this(
hours * NpgsqlInterval.TicksPerHour + minutes * NpgsqlInterval.TicksPerMinute +
(long)(seconds * NpgsqlInterval.TicksPerSecond))
{
}
public NpgsqlTime(int hours, int minutes, double seconds)
: this(hours, minutes, (decimal)seconds)
{
}
/// <summary>
/// The total number of ticks(100ns units) contained. This is the resolution of the
/// <see cref="NpgsqlTime"/> type.
/// <remarks>The resolution of the PostgreSQL
/// interval type is by default 1µs = 1,000 ns. It may be smaller as follows:
/// <list type="number">
/// <item>
/// <term>time(0)</term>
/// <description>resolution of 1s (1 second)</description>
/// </item>
/// <item>
/// <term>time(1)</term>
/// <description>resolution of 100ms = 0.1s (100 milliseconds)</description>
/// </item>
/// <item>
/// <term>time(2)</term>
/// <description>resolution of 10ms = 0.01s (10 milliseconds)</description>
/// </item>
/// <item>
/// <term>time(3)</term>
/// <description>resolution of 1ms = 0.001s (1 millisecond)</description>
/// </item>
/// <item>
/// <term>time(4)</term>
/// <description>resolution of 100µs = 0.0001s (100 microseconds)</description>
/// </item>
/// <item>
/// <term>time(5)</term>
/// <description>resolution of 10µs = 0.00001s (10 microseconds)</description>
/// </item>
/// <item>
/// <term>time(6) or interval</term>
/// <description>resolution of 1µs = 0.000001s (1 microsecond)</description>
/// </item>
/// </list>
/// <para>As such, if the 100-nanosecond resolution is significant to an application, a PostgreSQL time will
/// not suffice for those purposes.</para>
/// <para>In more frequent cases though, the resolution of time suffices.
/// <see cref="NpgsqlTime"/> will always suffice to handle the resolution of any time value, and upon
/// writing to the database, will be rounded to the resolution used.</para>
/// </remarks>
/// <returns>The number of ticks in the instance.</returns>
/// </summary>
public long Ticks
{
get { return _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 (int)((_ticks / 10) % 1000000); }
}
/// <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 (int)((_ticks / NpgsqlInterval.TicksPerMillsecond) % 1000); }
}
/// <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 (int)((_ticks / NpgsqlInterval.TicksPerSecond) % 60); }
}
/// <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 (int)((_ticks / NpgsqlInterval.TicksPerMinute) % 60); }
}
/// <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 (int)(_ticks / NpgsqlInterval.TicksPerHour); }
}
/// <summary>
/// Normalise this time; if it is 24:00:00, convert it to 00:00:00
/// </summary>
/// <returns>This time, normalised</returns>
public NpgsqlTime Normalize()
{
return new NpgsqlTime(_ticks % NpgsqlInterval.TicksPerDay);
}
public bool Equals(NpgsqlTime other)
{
return Ticks == other.Ticks;
}
public override bool Equals(object obj)
{
return obj != null && obj is NpgsqlTime && Equals((NpgsqlTime)obj);
}
public override int GetHashCode()
{
return Ticks.GetHashCode();
}
public override string ToString()
{
// calculate total seconds and then subtract total whole minutes in seconds to get just the seconds and fractional part
decimal seconds = _ticks / (decimal)NpgsqlInterval.TicksPerSecond - (_ticks / NpgsqlInterval.TicksPerMinute) * 60;
StringBuilder sb =
new StringBuilder(Hours.ToString("D2")).Append(':').Append(Minutes.ToString("D2")).Append(':').Append(
seconds.ToString("0#.######", System.Globalization.CultureInfo.InvariantCulture.NumberFormat));
return sb.ToString();
}
public static NpgsqlTime Parse(string str)
{
if (str == null) {
throw new ArgumentNullException();
}
try {
int hours = 0;
int minutes = 0;
decimal seconds = 0m;
string[] parts = str.Split(':');
switch (parts.Length) //One of those times that fall-through would actually be good.
{
case 1:
hours = int.Parse(parts[0]);
break;
case 2:
hours = int.Parse(parts[0]);
minutes = int.Parse(parts[1]);
break;
default:
hours = int.Parse(parts[0]);
minutes = int.Parse(parts[1]);
seconds = decimal.Parse(parts[2], System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
break;
}
if (hours < 0 || hours > 24 || minutes < 0 || minutes > 59 || seconds < 0m || seconds >= 60 ||
(hours == 24 && (minutes != 0 || seconds != 0m))) {
throw new OverflowException();
}
return new NpgsqlTime(hours, minutes, seconds);
} catch (OverflowException) {
throw;
} catch {
throw new FormatException();
}
}
public static bool TryParse(string str, out NpgsqlTime time)
{
try {
time = Parse(str);
return true;
} catch {
time = AllBalls;
return false;
}
}
public int CompareTo(NpgsqlTime other)
{
return Ticks.CompareTo(other.Ticks);
}
public int CompareTo(object obj)
{
if (obj == null) {
return 1;
}
if (obj is NpgsqlTime) {
return CompareTo((NpgsqlTime)obj);
}
throw new ArgumentException();
}
public int Compare(NpgsqlTime x, NpgsqlTime 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 ==(NpgsqlTime x, NpgsqlTime y)
{
return x.Equals(y);
}
public static bool operator !=(NpgsqlTime x, NpgsqlTime y)
{
return !(x == y);
}
public static bool operator <(NpgsqlTime x, NpgsqlTime y)
{
return x.Ticks < y.Ticks;
}
public static bool operator >(NpgsqlTime x, NpgsqlTime y)
{
return x.Ticks > y.Ticks;
}
public static bool operator <=(NpgsqlTime x, NpgsqlTime y)
{
return x.Ticks <= y.Ticks;
}
public static bool operator >=(NpgsqlTime x, NpgsqlTime y)
{
return x.Ticks >= y.Ticks;
}
public static explicit operator NpgsqlInterval(NpgsqlTime time)
{
return new NpgsqlInterval(time.Ticks);
}
public static explicit operator NpgsqlTime(NpgsqlInterval interval)
{
return new NpgsqlTime(interval);
}
public static explicit operator TimeSpan(NpgsqlTime time)
{
return new TimeSpan(time.Ticks);
}
public static explicit operator DateTime(NpgsqlTime time)
{
try {
return new DateTime(time.Ticks, DateTimeKind.Unspecified);
} catch {
throw new InvalidCastException();
}
}
public static explicit operator NpgsqlTime(TimeSpan interval)
{
return new NpgsqlTime(interval);
}
public NpgsqlTime AddTicks(long ticksAdded)
{
return new NpgsqlTime((Ticks + ticksAdded) % NpgsqlInterval.TicksPerDay);
}
private NpgsqlTime AddTicks(long ticksAdded, out int overflow)
{
long result = Ticks + ticksAdded;
overflow = (int)(result / NpgsqlInterval.TicksPerDay);
result %= NpgsqlInterval.TicksPerDay;
if (result < 0) {
--overflow; //"carry the one"
}
return new NpgsqlTime(result);
}
public NpgsqlTime Add(NpgsqlInterval interval)
{
return AddTicks(interval.Ticks);
}
internal NpgsqlTime Add(NpgsqlInterval interval, out int overflow)
{
return AddTicks(interval.Ticks, out overflow);
}
public NpgsqlTime Subtract(NpgsqlInterval interval)
{
return AddTicks(-interval.Ticks);
}
public NpgsqlInterval Subtract(NpgsqlTime earlier)
{
return new NpgsqlInterval(Ticks - earlier.Ticks);
}
public NpgsqlTimeTZ AtTimeZone(NpgsqlTimeZone timeZone)
{
return new NpgsqlTimeTZ(this).AtTimeZone(timeZone);
}
public static NpgsqlTime operator +(NpgsqlTime time, NpgsqlInterval interval)
{
return time.Add(interval);
}
public static NpgsqlTime operator +(NpgsqlInterval interval, NpgsqlTime time)
{
return time + interval;
}
public static NpgsqlTime operator -(NpgsqlTime time, NpgsqlInterval interval)
{
return time.Subtract(interval);
}
public static NpgsqlInterval operator -(NpgsqlTime later, NpgsqlTime earlier)
{
return later.Subtract(earlier);
}
}
}