Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SqlDateTime.cs
Go to the documentation of this file.
5using System.Xml;
8
10
12[XmlSchemaProvider("GetXsdType")]
13[TypeForwardedFrom("System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
15{
16 private bool m_fNotNull;
17
18 private int m_day;
19
20 private int m_time;
21
22 public static readonly int SQLTicksPerSecond = 300;
23
24 public static readonly int SQLTicksPerMinute = SQLTicksPerSecond * 60;
25
26 public static readonly int SQLTicksPerHour = SQLTicksPerMinute * 60;
27
28 private static readonly int s_SQLTicksPerDay = SQLTicksPerHour * 24;
29
30 private static readonly DateTime s_SQLBaseDate = new DateTime(1900, 1, 1);
31
32 private static readonly long s_SQLBaseDateTicks = s_SQLBaseDate.Ticks;
33
34 private static readonly int s_maxTime = s_SQLTicksPerDay - 1;
35
36 private static readonly int[] s_daysToMonth365 = new int[13]
37 {
38 0, 31, 59, 90, 120, 151, 181, 212, 243, 273,
39 304, 334, 365
40 };
41
42 private static readonly int[] s_daysToMonth366 = new int[13]
43 {
44 0, 31, 60, 91, 121, 152, 182, 213, 244, 274,
45 305, 335, 366
46 };
47
48 private static readonly DateTime s_minDateTime = new DateTime(1753, 1, 1);
49
50 private static readonly DateTime s_maxDateTime = DateTime.MaxValue;
51
52 private static readonly TimeSpan s_minTimeSpan = s_minDateTime.Subtract(s_SQLBaseDate);
53
54 private static readonly TimeSpan s_maxTimeSpan = s_maxDateTime.Subtract(s_SQLBaseDate);
55
56 private static readonly string[] s_dateTimeFormats = new string[8] { "MMM d yyyy hh:mm:ss:ffftt", "MMM d yyyy hh:mm:ss:fff", "d MMM yyyy hh:mm:ss:ffftt", "d MMM yyyy hh:mm:ss:fff", "hh:mm:ss:ffftt", "hh:mm:ss:fff", "yyMMdd", "yyyyMMdd" };
57
58 public static readonly SqlDateTime MinValue = new SqlDateTime(-53690, 0);
59
60 public static readonly SqlDateTime MaxValue = new SqlDateTime(2958463, s_maxTime);
61
62 public static readonly SqlDateTime Null = new SqlDateTime(fNull: true);
63
64 public bool IsNull => !m_fNotNull;
65
67 {
68 get
69 {
70 if (m_fNotNull)
71 {
72 return ToDateTime(this);
73 }
74 throw new SqlNullValueException();
75 }
76 }
77
78 public int DayTicks
79 {
80 get
81 {
82 if (m_fNotNull)
83 {
84 return m_day;
85 }
86 throw new SqlNullValueException();
87 }
88 }
89
90 public int TimeTicks
91 {
92 get
93 {
94 if (m_fNotNull)
95 {
96 return m_time;
97 }
98 throw new SqlNullValueException();
99 }
100 }
101
102 private SqlDateTime(bool fNull)
103 {
104 m_fNotNull = false;
105 m_day = 0;
106 m_time = 0;
107 }
108
110 {
111 this = FromDateTime(value);
112 }
113
114 public SqlDateTime(int year, int month, int day)
115 : this(year, month, day, 0, 0, 0, 0.0)
116 {
117 }
118
119 public SqlDateTime(int year, int month, int day, int hour, int minute, int second)
120 : this(year, month, day, hour, minute, second, 0.0)
121 {
122 }
123
124 public SqlDateTime(int year, int month, int day, int hour, int minute, int second, double millisecond)
125 {
126 if (year >= 1753 && year <= 9999 && month >= 1 && month <= 12)
127 {
129 if (day >= 1 && day <= array[month] - array[month - 1])
130 {
131 int num = year - 1;
132 int num2 = num * 365 + num / 4 - num / 100 + num / 400 + array[month - 1] + day - 1;
133 num2 -= 693595;
135 {
136 double num3 = millisecond * 0.3 + 0.5;
137 int num4 = hour * SQLTicksPerHour + minute * SQLTicksPerMinute + second * SQLTicksPerSecond + (int)num3;
138 if (num4 > s_maxTime)
139 {
140 num4 = 0;
141 num2++;
142 }
143 this = new SqlDateTime(num2, num4);
144 return;
145 }
146 }
147 }
149 }
150
151 public SqlDateTime(int year, int month, int day, int hour, int minute, int second, int bilisecond)
152 : this(year, month, day, hour, minute, second, (double)bilisecond / 1000.0)
153 {
154 }
155
167
169 {
170 long num = (long)((double)value.m_time / 0.3 + 0.5);
171 return new TimeSpan(value.m_day * 864000000000L + num * 10000);
172 }
173
175 {
176 return s_SQLBaseDate.Add(ToTimeSpan(value));
177 }
178
180 {
182 {
184 }
185 int num = value.Days;
186 long num2 = value.Ticks - num * 864000000000L;
187 if (num2 < 0)
188 {
189 num--;
190 num2 += 864000000000L;
191 }
192 int num3 = (int)((double)num2 / 10000.0 * 0.3 + 0.5);
193 if (num3 > s_maxTime)
194 {
195 num3 = 0;
196 num++;
197 }
198 return new SqlDateTime(num, num3);
199 }
200
202 {
203 if (value == DateTime.MaxValue)
204 {
205 return MaxValue;
206 }
207 return FromTimeSpan(value.Subtract(s_SQLBaseDate));
208 }
209
210 public static implicit operator SqlDateTime(DateTime value)
211 {
212 return new SqlDateTime(value);
213 }
214
215 public static explicit operator DateTime(SqlDateTime x)
216 {
217 return ToDateTime(x);
218 }
219
220 public override string ToString()
221 {
222 if (IsNull)
223 {
224 return SQLResource.NullString;
225 }
226 return ToDateTime(this).ToString((IFormatProvider?)null);
227 }
228
229 public static SqlDateTime Parse(string s)
230 {
231 if (s == SQLResource.NullString)
232 {
233 return Null;
234 }
236 try
237 {
239 }
240 catch (FormatException)
241 {
244 }
245 return new SqlDateTime(value);
246 }
247
249 {
250 if (!x.IsNull)
251 {
252 return FromDateTime(ToDateTime(x) + t);
253 }
254 return Null;
255 }
256
258 {
259 if (!x.IsNull)
260 {
261 return FromDateTime(ToDateTime(x) - t);
262 }
263 return Null;
264 }
265
267 {
268 return x + t;
269 }
270
272 {
273 return x - t;
274 }
275
276 public static explicit operator SqlDateTime(SqlString x)
277 {
278 if (!x.IsNull)
279 {
280 return Parse(x.Value);
281 }
282 return Null;
283 }
284
285 private static bool IsLeapYear(int year)
286 {
287 if (year % 4 == 0)
288 {
289 if (year % 100 == 0)
290 {
291 return year % 400 == 0;
292 }
293 return true;
294 }
295 return false;
296 }
297
299 {
300 if (!x.IsNull && !y.IsNull)
301 {
302 return new SqlBoolean(x.m_day == y.m_day && x.m_time == y.m_time);
303 }
304 return SqlBoolean.Null;
305 }
306
308 {
309 return !(x == y);
310 }
311
313 {
314 if (!x.IsNull && !y.IsNull)
315 {
316 return new SqlBoolean(x.m_day < y.m_day || (x.m_day == y.m_day && x.m_time < y.m_time));
317 }
318 return SqlBoolean.Null;
319 }
320
322 {
323 if (!x.IsNull && !y.IsNull)
324 {
325 return new SqlBoolean(x.m_day > y.m_day || (x.m_day == y.m_day && x.m_time > y.m_time));
326 }
327 return SqlBoolean.Null;
328 }
329
331 {
332 if (!x.IsNull && !y.IsNull)
333 {
334 return new SqlBoolean(x.m_day < y.m_day || (x.m_day == y.m_day && x.m_time <= y.m_time));
335 }
336 return SqlBoolean.Null;
337 }
338
340 {
341 if (!x.IsNull && !y.IsNull)
342 {
343 return new SqlBoolean(x.m_day > y.m_day || (x.m_day == y.m_day && x.m_time >= y.m_time));
344 }
345 return SqlBoolean.Null;
346 }
347
349 {
350 return x == y;
351 }
352
354 {
355 return x != y;
356 }
357
359 {
360 return x < y;
361 }
362
364 {
365 return x > y;
366 }
367
369 {
370 return x <= y;
371 }
372
374 {
375 return x >= y;
376 }
377
379 {
380 return (SqlString)this;
381 }
382
383 public int CompareTo(object? value)
384 {
386 {
387 return CompareTo(value2);
388 }
389 throw ADP.WrongType(value.GetType(), typeof(SqlDateTime));
390 }
391
393 {
394 if (IsNull)
395 {
396 if (!value.IsNull)
397 {
398 return -1;
399 }
400 return 0;
401 }
402 if (value.IsNull)
403 {
404 return 1;
405 }
406 if (this < value)
407 {
408 return -1;
409 }
410 if (this > value)
411 {
412 return 1;
413 }
414 return 0;
415 }
416
417 public override bool Equals([NotNullWhen(true)] object? value)
418 {
420 {
421 return false;
422 }
423 if (sqlDateTime.IsNull || IsNull)
424 {
425 if (sqlDateTime.IsNull)
426 {
427 return IsNull;
428 }
429 return false;
430 }
431 return (this == sqlDateTime).Value;
432 }
433
434 public override int GetHashCode()
435 {
436 if (!IsNull)
437 {
438 return Value.GetHashCode();
439 }
440 return 0;
441 }
442
444 {
445 return null;
446 }
447
449 {
450 string attribute = reader.GetAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance");
451 if (attribute != null && XmlConvert.ToBoolean(attribute))
452 {
453 reader.ReadElementString();
454 m_fNotNull = false;
455 return;
456 }
458 if (value.Kind != 0)
459 {
461 }
463 m_day = sqlDateTime.DayTicks;
464 m_time = sqlDateTime.TimeTicks;
465 m_fNotNull = true;
466 }
467
469 {
470 if (IsNull)
471 {
472 writer.WriteAttributeString("xsi", "nil", "http://www.w3.org/2001/XMLSchema-instance", "true");
473 }
474 else
475 {
476 writer.WriteString(XmlConvert.ToString(Value, "yyyy-MM-ddTHH:mm:ss.fff"));
477 }
478 }
479
481 {
482 return new XmlQualifiedName("dateTime", "http://www.w3.org/2001/XMLSchema");
483 }
484}
static Exception WrongType(Type got, Type expected)
Definition ADP.cs:174
static CultureInfo CurrentCulture
static CultureInfo InvariantCulture
static bool ToBoolean(string s)
static string ToString(bool value)
static DateTime ToDateTime(string s)
string? GetAttribute(string name)
virtual string ReadElementString()
Definition XmlReader.cs:667
static readonly SqlBoolean Null
Definition SqlBoolean.cs:21
static readonly int s_maxTime
int CompareTo(SqlDateTime value)
static SqlBoolean operator<=(SqlDateTime x, SqlDateTime y)
SqlDateTime(int year, int month, int day)
SqlDateTime(int year, int month, int day, int hour, int minute, int second)
override bool Equals([NotNullWhen(true)] object? value)
static DateTime ToDateTime(SqlDateTime value)
static SqlBoolean Equals(SqlDateTime x, SqlDateTime y)
static readonly DateTime s_minDateTime
static SqlBoolean operator>=(SqlDateTime x, SqlDateTime y)
static readonly int[] s_daysToMonth365
static readonly int SQLTicksPerSecond
static SqlBoolean LessThan(SqlDateTime x, SqlDateTime y)
static SqlDateTime Parse(string s)
static readonly SqlDateTime MaxValue
static readonly int s_SQLTicksPerDay
static SqlDateTime Subtract(SqlDateTime x, TimeSpan t)
static SqlBoolean operator<(SqlDateTime x, SqlDateTime y)
SqlDateTime(int dayTicks, int timeTicks)
static readonly int SQLTicksPerHour
SqlDateTime(int year, int month, int day, int hour, int minute, int second, int bilisecond)
static SqlBoolean GreaterThan(SqlDateTime x, SqlDateTime y)
static readonly SqlDateTime Null
static TimeSpan ToTimeSpan(SqlDateTime value)
static readonly TimeSpan s_minTimeSpan
static SqlBoolean NotEquals(SqlDateTime x, SqlDateTime y)
static SqlBoolean operator>(SqlDateTime x, SqlDateTime y)
static SqlDateTime FromDateTime(DateTime value)
static readonly int SQLTicksPerMinute
static SqlDateTime operator-(SqlDateTime x, TimeSpan t)
static SqlBoolean GreaterThanOrEqual(SqlDateTime x, SqlDateTime y)
static bool IsLeapYear(int year)
static readonly DateTime s_maxDateTime
static readonly SqlDateTime MinValue
static SqlDateTime Add(SqlDateTime x, TimeSpan t)
static SqlBoolean LessThanOrEqual(SqlDateTime x, SqlDateTime y)
static readonly int[] s_daysToMonth366
static SqlBoolean operator==(SqlDateTime x, SqlDateTime y)
SqlDateTime(int year, int month, int day, int hour, int minute, int second, double millisecond)
static readonly string[] s_dateTimeFormats
static readonly DateTime s_SQLBaseDate
static SqlDateTime FromTimeSpan(TimeSpan value)
static SqlDateTime operator+(SqlDateTime x, TimeSpan t)
static SqlBoolean operator!=(SqlDateTime x, SqlDateTime y)
static readonly long s_SQLBaseDateTicks
static XmlQualifiedName GetXsdType(XmlSchemaSet schemaSet)
static readonly TimeSpan s_maxTimeSpan
override int GetHashCode()
Definition DateTime.cs:886
static readonly DateTime MaxValue
Definition DateTime.cs:37
static DateTime ParseExact(string s, string format, IFormatProvider? provider)
Definition DateTime.cs:953
static DateTime Parse(string s)
Definition DateTime.cs:919