-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDateTimeOffset.cs
417 lines (323 loc) · 15.5 KB
/
DateTimeOffset.cs
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
// https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset?view=netframework-4.8.1
// https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/DateTimeOffset.cs
using GSystem = global::System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System;
using DotNetLib.System.Globalization;
using DotNetLib.Extensions;
namespace DotNetLib.System
{
// TODO : Explict Interface Implementations
[ComVisible(true)]
[Description("Represents a point in time, typically expressed as a date and time of day, relative to Coordinated Universal Time (UTC).")]
[Guid("27660912-6101-4779-B4E0-24F2B164B334")]
[ProgId("DotNetLib.System.DateTimeOffset")]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(IDateTimeOffset))]
public class DateTimeOffset : IComparable, IFormattable, IDateTimeOffset, IWrappedObject
{
private GSystem.DateTimeOffset _dateTimeOffset;
// Static Fields
private static readonly DateTimeOffset dtoMaxValue = new DateTimeOffset(GSystem.DateTimeOffset.MaxValue);
private static readonly DateTimeOffset dtoMinValue = new DateTimeOffset(GSystem.DateTimeOffset.MinValue);
// Constructors
internal DateTimeOffset(GSystem.DateTimeOffset dateTimeOffsetObject)
{
_dateTimeOffset = dateTimeOffsetObject;
}
public DateTimeOffset()
{
_dateTimeOffset = new GSystem.DateTimeOffset();
}
public DateTimeOffset(DateTime dateTime)
{
_dateTimeOffset = new GSystem.DateTimeOffset(dateTime.WrappedDateTime);
}
public DateTimeOffset(DateTime dateTime, TimeSpan offset)
{
_dateTimeOffset = new GSystem.DateTimeOffset(dateTime.WrappedDateTime, offset.WrappedTimeSpan);
}
public DateTimeOffset(long ticks, TimeSpan offset)
{
_dateTimeOffset = new GSystem.DateTimeOffset(ticks, offset.WrappedTimeSpan);
}
public DateTimeOffset(int year, int month, int day, int hour, int minute, int second, TimeSpan offset)
{
_dateTimeOffset = new GSystem.DateTimeOffset(year, month, day, hour, minute, second, offset.WrappedTimeSpan);
}
public DateTimeOffset(int year, int month, int day, int hour, int minute, int second, int millisecond, TimeSpan offset)
{
_dateTimeOffset = new GSystem.DateTimeOffset(year, month, day, hour, minute, second, millisecond, offset.WrappedTimeSpan);
}
public DateTimeOffset(int year, int month, int day, int hour, int minute, int second, int millisecond, GSystem.Globalization.Calendar pCalendar, TimeSpan offset)
{
_dateTimeOffset = new GSystem.DateTimeOffset(year, month, day, hour, minute, second, millisecond, pCalendar, offset.WrappedTimeSpan);
}
// Fields
public static DateTimeOffset MaxValue => dtoMaxValue;
public static DateTimeOffset MinValue => dtoMinValue;
// Properties
internal GSystem.DateTimeOffset WrappedDateTimeOffset => _dateTimeOffset;
public object WrappedObject => _dateTimeOffset;
//Note: VBA reserved word Date
public DateTime Date => new DateTime(_dateTimeOffset.Date);
public DateTime DateTime => new DateTime(_dateTimeOffset.DateTime);
public int Day => _dateTimeOffset.Day;
public DayOfWeek DayOfWeek => (DayOfWeek)_dateTimeOffset.DayOfWeek;
public int DayOfYear => _dateTimeOffset.DayOfYear;
public int Hour => _dateTimeOffset.Hour;
public DateTime LocalDateTime => new DateTime(_dateTimeOffset.LocalDateTime);
public int Millisecond => _dateTimeOffset.Millisecond;
public int Minute => _dateTimeOffset.Minute;
public int Month => _dateTimeOffset.Month;
public static DateTimeOffset Now => new DateTimeOffset(GSystem.DateTimeOffset.Now);
public TimeSpan Offset => new TimeSpan(_dateTimeOffset.Offset);
public int Second => _dateTimeOffset.Second;
public long Ticks => _dateTimeOffset.Ticks;
public TimeSpan TimeOfDay => new TimeSpan(_dateTimeOffset.TimeOfDay);
public DateTime UtcDateTime => new DateTime(_dateTimeOffset.UtcDateTime);
public static DateTimeOffset UtcNow => new DateTimeOffset(GSystem.DateTimeOffset.UtcNow);
public long UtcTicks => _dateTimeOffset.UtcTicks;
public int Year => _dateTimeOffset.Year;
// Methods
public DateTimeOffset Add(TimeSpan ts)
{
return new DateTimeOffset(_dateTimeOffset.Add(ts.WrappedTimeSpan));
}
public DateTimeOffset AddDays(double days)
{
return new DateTimeOffset(_dateTimeOffset.AddDays(days));
}
public DateTimeOffset AddHours(double hours)
{
return new DateTimeOffset(_dateTimeOffset.AddHours(hours));
}
public DateTimeOffset AddMilliseconds(double pMilliseconds)
{
return new DateTimeOffset(_dateTimeOffset.AddMilliseconds(pMilliseconds));
}
public DateTimeOffset AddMinutes(double minutes)
{
return new DateTimeOffset(_dateTimeOffset.AddMinutes(minutes));
}
public DateTimeOffset AddMonths(int months)
{
return new DateTimeOffset(_dateTimeOffset.AddMonths(months));
}
public DateTimeOffset AddSeconds(double pSeconds)
{
return new DateTimeOffset(_dateTimeOffset.AddSeconds(pSeconds));
}
public DateTimeOffset AddTicks(long ticks)
{
return new DateTimeOffset(_dateTimeOffset.AddTicks(ticks));
}
public DateTimeOffset AddYears(int years)
{
return new DateTimeOffset(_dateTimeOffset.AddYears(years));
}
public static int Compare(DateTimeOffset first, DateTimeOffset second)
{
return GSystem.DateTimeOffset.Compare(first.WrappedDateTimeOffset, second.WrappedDateTimeOffset);
}
public int CompareTo(object value)
{
//TODO : Check implementation for some reason implemented differently then DateTime and TimeSpan? Not public?
//return _dateTimeOffset.CompareTo((GSystem.DateTimeOffset)value.Unwrap());
const string Arg_MustBeDateTimeOffset = "Object must be of type (DateTimeOffset.";
if (value == null) return 1;
if (!(value is DateTimeOffset dto))
{
throw new ArgumentException(Arg_MustBeDateTimeOffset);
}
return _dateTimeOffset.CompareTo(dto.WrappedDateTimeOffset);
}
public int CompareTo(DateTimeOffset other)
{
return _dateTimeOffset.CompareTo(other.WrappedDateTimeOffset);
}
public int CompareTo2(object value)
{
return CompareTo(value);
}
public bool Equals(DateTimeOffset other)
{
return _dateTimeOffset.Equals(other.WrappedDateTimeOffset);
}
// Checks if this DateTimeOffset is equal to a given object. Returns
// true if the given object is a boxed DateTimeOffset and its value
// is equal to the value of this DateTimeOffset. Returns false
// otherwise.
//
public bool Equals2(object obj)
{
return obj is DateTimeOffset dto && _dateTimeOffset == dto.WrappedDateTimeOffset;
}
public static bool Equals(DateTimeOffset first, DateTimeOffset second)
{
return GSystem.DateTimeOffset.Equals(first.WrappedDateTimeOffset, second.WrappedDateTimeOffset);
}
public bool EqualsExact(DateTimeOffset other)
{
return _dateTimeOffset.EqualsExact(other.WrappedDateTimeOffset);
}
public static DateTimeOffset FromFileTime(long fileTime)
{
return new DateTimeOffset(GSystem.DateTimeOffset.FromFileTime(fileTime));
}
public static DateTimeOffset FromUnixTimeMilliseconds(long pMilliseconds)
{
return new DateTimeOffset(GSystem.DateTimeOffset.FromUnixTimeMilliseconds(pMilliseconds));
}
public static DateTimeOffset FromUnixTimeSeconds(long pSeconds)
{
return new DateTimeOffset(GSystem.DateTimeOffset.FromUnixTimeSeconds(pSeconds));
}
public override int GetHashCode()
{
return _dateTimeOffset.GetHashCode();
}
public new Type GetType()
{
return new Type(((GSystem.Object)this).GetType());
}
public static DateTimeOffset Parse(string input)
{
return new DateTimeOffset(GSystem.DateTimeOffset.Parse(input));
}
public static DateTimeOffset Parse(string input, IFormatProvider formatProvider)
{
return new DateTimeOffset(GSystem.DateTimeOffset.Parse(input, DateTimeFormatInfo.Unwrap(formatProvider)));
}
public static DateTimeOffset Parse(string input, IFormatProvider formatProvider, GSystem.Globalization.DateTimeStyles styles)
{
return new DateTimeOffset(GSystem.DateTimeOffset.Parse(input, DateTimeFormatInfo.Unwrap(formatProvider), styles));
}
public static DateTimeOffset ParseExact(string input, string format, IFormatProvider formatProvider)
{
return new DateTimeOffset(GSystem.DateTimeOffset.ParseExact(input, format, DateTimeFormatInfo.Unwrap(formatProvider)));
}
public static DateTimeOffset ParseExact(string input, string format, IFormatProvider formatProvider, GSystem.Globalization.DateTimeStyles styles)
{
return new DateTimeOffset(GSystem.DateTimeOffset.ParseExact(input, format, DateTimeFormatInfo.Unwrap(formatProvider), styles));
}
public static DateTimeOffset ParseExact(string input, [In] ref string[] formats, IFormatProvider formatProvider, GSystem.Globalization.DateTimeStyles styles)
{
return new DateTimeOffset(GSystem.DateTimeOffset.ParseExact(input, formats, DateTimeFormatInfo.Unwrap(formatProvider), styles));
}
public TimeSpan Subtract(DateTimeOffset value)
{
return new TimeSpan(_dateTimeOffset.Subtract(value.WrappedDateTimeOffset));
}
public DateTimeOffset Subtract2(TimeSpan value)
{
return new DateTimeOffset(_dateTimeOffset.Subtract(value.WrappedTimeSpan));
}
public long ToFileTime()
{
return _dateTimeOffset.ToFileTime();
}
public DateTimeOffset ToLocalTime()
{
return new DateTimeOffset(_dateTimeOffset.ToLocalTime());
}
public DateTimeOffset ToOffset(TimeSpan offset)
{
return new DateTimeOffset(_dateTimeOffset.ToOffset(offset.WrappedTimeSpan));
}
public override string ToString()
{
return _dateTimeOffset.ToString();
}
public string ToString(string format, IFormatProvider formatProvider)
{
return _dateTimeOffset.ToString(format, DateTimeFormatInfo.Unwrap(formatProvider));
}
public string ToString2(string format, IFormatProvider provider = null)
{
if (provider == null)
return _dateTimeOffset.ToString(format);
return _dateTimeOffset.ToString(format, DateTimeFormatInfo.Unwrap(provider));
}
public string ToString3(IFormatProvider formatProvider)
{
return _dateTimeOffset.ToString(DateTimeFormatInfo.Unwrap(formatProvider));
}
public DateTimeOffset ToUniversalTime()
{
return new DateTimeOffset(_dateTimeOffset.ToUniversalTime());
}
public long ToUnixTimeMilliseconds()
{
return _dateTimeOffset.ToUnixTimeMilliseconds();
}
public long ToUnixTimeSeconds()
{
return _dateTimeOffset.ToUnixTimeSeconds();
}
// TODO: public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider? formatProvider = default);
public static bool TryParse(string input, out DateTimeOffset result)
{
bool pvtTryParse = GSystem.DateTimeOffset.TryParse(input, out GSystem.DateTimeOffset outResult);
result = new DateTimeOffset(outResult);
return pvtTryParse;
}
public static bool TryParse(string input, IFormatProvider formatProvider, GSystem.Globalization.DateTimeStyles styles, out DateTimeOffset result)
{
bool pvtTryParse = GSystem.DateTimeOffset.TryParse(input, DateTimeFormatInfo.Unwrap(formatProvider), styles,out GSystem.DateTimeOffset outResult);
result = new DateTimeOffset(outResult);
return pvtTryParse;
}
public static bool TryParseExact(string input, string format, IFormatProvider formatProvider, GSystem.Globalization.DateTimeStyles styles, out DateTimeOffset result)
{
bool pvtTryParseExact = GSystem.DateTimeOffset.TryParseExact(input, format, DateTimeFormatInfo.Unwrap(formatProvider), styles, out GSystem.DateTimeOffset outResult);
result = new DateTimeOffset(outResult);
return pvtTryParseExact;
}
public static bool TryParseExact(string input, string[] formats, IFormatProvider formatProvider, GSystem.Globalization.DateTimeStyles styles, out DateTimeOffset result)
{
bool pvtTryParseExact = GSystem.DateTimeOffset.TryParseExact(input, formats, DateTimeFormatInfo.Unwrap(formatProvider), styles, out GSystem.DateTimeOffset outResult);
result = new DateTimeOffset(outResult);
return pvtTryParseExact;
}
// Operators
public static DateTimeOffset Addition(DateTimeOffset dateTimeOffset, TimeSpan timeSpan)
{
return new DateTimeOffset(dateTimeOffset.WrappedDateTimeOffset + timeSpan.WrappedTimeSpan);
}
public static bool Equality(DateTimeOffset left, DateTimeOffset right)
{
return (left.WrappedDateTimeOffset == right.WrappedDateTimeOffset);
}
public static bool GreaterThan(DateTimeOffset left, DateTimeOffset right)
{
return (left.WrappedDateTimeOffset > right.WrappedDateTimeOffset);
}
public static bool GreaterThanOrEqual(DateTimeOffset left, DateTimeOffset right)
{
return (left.WrappedDateTimeOffset >= right.WrappedDateTimeOffset);
}
public static DateTimeOffset Implicit(DateTime dateTime) => new DateTimeOffset(dateTime);
public static bool Inequality(DateTimeOffset left, DateTimeOffset right)
{
return (left.WrappedDateTimeOffset != right.WrappedDateTimeOffset);
}
public static bool LessThan(DateTimeOffset left, DateTimeOffset right)
{
return (left.WrappedDateTimeOffset < right.WrappedDateTimeOffset);
}
public static bool LessThanOrEqual(DateTimeOffset left, DateTimeOffset right)
{
return (left.WrappedDateTimeOffset <= right.WrappedDateTimeOffset);
}
public static TimeSpan Subtraction(DateTimeOffset left, DateTimeOffset right)
{
return new TimeSpan(left.WrappedDateTimeOffset - right.WrappedDateTimeOffset);
}
public static DateTimeOffset Subtraction(DateTimeOffset dateTimeOffset, TimeSpan timeSpan)
{
return new DateTimeOffset(dateTimeOffset.WrappedDateTimeOffset - timeSpan.WrappedTimeSpan);
}
}
}