forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlTimeZone.cs
More file actions
executable file
·244 lines (211 loc) · 7.05 KB
/
Copy pathNpgsqlTimeZone.cs
File metadata and controls
executable file
·244 lines (211 loc) · 7.05 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// ReSharper disable once CheckNamespace
namespace NpgsqlTypes
{
[Serializable]
public struct NpgsqlTimeZone : IEquatable<NpgsqlTimeZone>, IComparable<NpgsqlTimeZone>, IComparable
{
public static NpgsqlTimeZone UTC = new NpgsqlTimeZone(0);
private readonly int _totalSeconds;
public NpgsqlTimeZone(TimeSpan ts)
: this(ts.Ticks)
{
}
private NpgsqlTimeZone(long ticks)
{
_totalSeconds = (int)(ticks / NpgsqlInterval.TicksPerSecond);
}
public NpgsqlTimeZone(NpgsqlInterval ni)
: this(ni.Ticks)
{
}
public NpgsqlTimeZone(NpgsqlTimeZone copyFrom)
{
_totalSeconds = copyFrom._totalSeconds;
}
public NpgsqlTimeZone(int hours, int minutes)
: this(hours, minutes, 0)
{
}
public NpgsqlTimeZone(int hours, int minutes, int seconds)
{
_totalSeconds = hours * 60 * 60 + minutes * 60 + seconds;
}
public static implicit operator NpgsqlTimeZone(NpgsqlInterval interval)
{
return new NpgsqlTimeZone(interval);
}
public static implicit operator NpgsqlInterval(NpgsqlTimeZone timeZone)
{
return new NpgsqlInterval(timeZone._totalSeconds * NpgsqlInterval.TicksPerSecond);
}
public static implicit operator NpgsqlTimeZone(TimeSpan interval)
{
return new NpgsqlTimeZone(interval);
}
public static implicit operator TimeSpan(NpgsqlTimeZone timeZone)
{
return new TimeSpan(timeZone._totalSeconds * NpgsqlInterval.TicksPerSecond);
}
public static NpgsqlTimeZone SolarTimeZone(decimal longitude)
{
return new NpgsqlTimeZone((long)(longitude / 15m * NpgsqlInterval.TicksPerHour));
}
public int Hours
{
get { return _totalSeconds / 60 / 60; }
}
public int Minutes
{
get { return (_totalSeconds / 60) % 60; }
}
public int Seconds
{
get { return _totalSeconds % 60; }
}
public static NpgsqlTimeZone CurrentTimeZone
{
get { return new NpgsqlTimeZone(TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now)); }
}
public static NpgsqlTimeZone LocalTimeZone(NpgsqlDate date)
{
DateTime dt;
if (date.Year >= 1902 && date.Year <= 2038) {
dt = (DateTime)date;
} else {
dt = new DateTime(2000, date.Month, date.Day);
}
return new NpgsqlTimeZone(TimeZone.CurrentTimeZone.GetUtcOffset(dt));
}
public bool Equals(NpgsqlTimeZone other)
{
return _totalSeconds == other._totalSeconds;
}
public override bool Equals(object obj)
{
return obj != null && obj is NpgsqlTimeZone && Equals((NpgsqlTimeZone)obj);
}
public override string ToString()
{
StringBuilder sb = new StringBuilder(_totalSeconds < 0 ? "-" : "+").Append(Math.Abs(Hours).ToString("D2"));
if (Minutes != 0 || Seconds != 0) {
sb.Append(':').Append(Math.Abs(Minutes).ToString("D2"));
if (Seconds != 0) {
sb.Append(":").Append(Math.Abs(Seconds).ToString("D2"));
}
}
return sb.ToString();
}
public static NpgsqlTimeZone Parse(string str)
{
if (str == null) {
throw new ArgumentNullException();
}
try {
str = str.Trim();
bool neg;
switch (str[0]) {
case '+':
neg = false;
break;
case '-':
neg = true;
break;
default:
throw new FormatException();
}
int hours;
int minutes;
int seconds;
string[] parts = str.Substring(1).Split(':');
switch (parts.Length) //One of those times that fall-through would actually be good.
{
case 1:
hours = int.Parse(parts[0]);
minutes = seconds = 0;
break;
case 2:
hours = int.Parse(parts[0]);
minutes = int.Parse(parts[1]);
seconds = 0;
break;
default:
hours = int.Parse(parts[0]);
minutes = int.Parse(parts[1]);
seconds = int.Parse(parts[2]);
break;
}
int totalSeconds = (hours * 60 * 60 + minutes * 60 + seconds) * (neg ? -1 : 1);
return new NpgsqlTimeZone(totalSeconds * NpgsqlInterval.TicksPerSecond);
} catch (OverflowException) {
throw;
} catch {
throw new FormatException();
}
}
public static bool TryParse(string str, NpgsqlTimeZone tz)
{
try {
tz = Parse(str);
return true;
} catch {
tz = UTC;
return false;
}
}
public override int GetHashCode()
{
return _totalSeconds;
}
//Note, +01:00 is less than -01:00
public int CompareTo(NpgsqlTimeZone other)
{
return -(_totalSeconds.CompareTo(other._totalSeconds));
}
public int CompareTo(object obj)
{
if (obj == null) {
return 1;
}
if (obj is NpgsqlTimeZone) {
return CompareTo((NpgsqlTimeZone)obj);
}
throw new ArgumentException();
}
public static NpgsqlTimeZone operator -(NpgsqlTimeZone tz)
{
return new NpgsqlTimeZone(-tz._totalSeconds * NpgsqlInterval.TicksPerSecond);
}
public static NpgsqlTimeZone operator +(NpgsqlTimeZone tz)
{
return tz;
}
public static bool operator ==(NpgsqlTimeZone x, NpgsqlTimeZone y)
{
return x.Equals(y);
}
public static bool operator !=(NpgsqlTimeZone x, NpgsqlTimeZone y)
{
return !(x == y);
}
public static bool operator <(NpgsqlTimeZone x, NpgsqlTimeZone y)
{
return x.CompareTo(y) < 0;
}
public static bool operator <=(NpgsqlTimeZone x, NpgsqlTimeZone y)
{
return x.CompareTo(y) <= 0;
}
public static bool operator >(NpgsqlTimeZone x, NpgsqlTimeZone y)
{
return x.CompareTo(y) > 0;
}
public static bool operator >=(NpgsqlTimeZone x, NpgsqlTimeZone y)
{
return x.CompareTo(y) >= 0;
}
}
}