-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDateAndTime.vb
More file actions
579 lines (484 loc) · 20.3 KB
/
DateAndTime.vb
File metadata and controls
579 lines (484 loc) · 20.3 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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
' Licensed to the .NET Foundation under one or more agreements.
' The .NET Foundation licenses this file to you under the MIT license.
Imports System
Imports System.Globalization
Imports System.Runtime.Versioning
Imports Community.VisualBasic.CompilerServices
Imports Community.VisualBasic.CompilerServices.ExceptionUtils
Imports Community.VisualBasic.CompilerServices.Utils
Namespace Global.Community.VisualBasic
Public Module DateAndTime
Private ReadOnly AcceptedDateFormatsDBCS() As String = {"yyyy-M-d", "y-M-d", "yyyy/M/d", "y/M/d"}
Private ReadOnly AcceptedDateFormatsSBCS() As String = {"M-d-yyyy", "M-d-y", "M/d/yyyy", "M/d/y"}
'============================================================================
' Date/Time Properties
'============================================================================
Public Property Today() As DateTime
Get
Return DateTime.Today
End Get
<SupportedOSPlatform("windows")>
Set(Value As DateTime)
SetDate(Value)
End Set
End Property
Public ReadOnly Property Now As DateTime
Get
Return DateTime.Now
End Get
End Property
Public Property TimeOfDay() As DateTime
Get
Dim Ticks As Int64 = DateTime.Now.TimeOfDay.Ticks
'Truncate to the nearest second
Return New DateTime(Ticks - Ticks Mod TimeSpan.TicksPerSecond)
End Get
<SupportedOSPlatform("windows")>
Set(Value As DateTime)
SetTime(Value)
End Set
End Property
' TimeString (replaces Time$)
Public Property TimeString() As String
'Locale agnostic, Always returns 24hr clock
Get
Return (New DateTime(DateTime.Now.TimeOfDay.Ticks)).ToString("HH:mm:ss", GetInvariantCultureInfo())
End Get
<SupportedOSPlatform("windows")>
Set(Value As String)
Dim dt As Date
Try
dt = CompilerServices.DateType.FromString(Value, GetInvariantCultureInfo())
Catch ex As StackOverflowException
Throw ex
Catch ex As OutOfMemoryException
Throw ex
Catch
Throw VbMakeException(New InvalidCastException(SR.Format(SR.InvalidCast_FromStringTo, Left(Value, 32), "Date")), vbErrors.IllegalFuncCall)
End Try
SetTime(dt)
End Set
End Property
Private Function IsDBCSCulture() As Boolean
If OperatingSystem.IsWindows Then
'This function is apparently trying to determine a different default for East Asian systems.
If System.Runtime.InteropServices.Marshal.SystemMaxDBCSCharSize = 1 Then
Return False
End If
Return True
Else
' Emulate IsDBCSCulture of .NET 3.5 using CultureInfo
Dim langName As String = System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName
Return String.Equals(langName, "zh", StringComparison.OrdinalIgnoreCase) OrElse
String.Equals(langName, "ko", StringComparison.OrdinalIgnoreCase) OrElse
String.Equals(langName, "ja", StringComparison.OrdinalIgnoreCase)
End If
End Function
Public Property DateString() As String
' DateString (replaces Date$)
'Returns yyyy-MM-dd for DBCS locale
'Returns MM-dd-yyyy for non-DBCS locale
Get
If IsDBCSCulture() Then
Return DateTime.Today.ToString("yyyy\-MM\-dd", GetInvariantCultureInfo())
Else
Return DateTime.Today.ToString("MM\-dd\-yyyy", GetInvariantCultureInfo())
End If
End Get
<SupportedOSPlatform("windows")>
Set(Value As String)
Dim NewDate As Date
Try
Dim TmpValue As String = ToHalfwidthNumbers(Value, GetCultureInfo())
If IsDBCSCulture() Then
NewDate = DateTime.ParseExact(TmpValue, AcceptedDateFormatsDBCS, GetInvariantCultureInfo(), DateTimeStyles.AllowWhiteSpaces)
Else
NewDate = DateTime.ParseExact(TmpValue, AcceptedDateFormatsSBCS, GetInvariantCultureInfo(), DateTimeStyles.AllowWhiteSpaces)
End If
Catch ex As StackOverflowException
Throw ex
Catch ex As OutOfMemoryException
Throw ex
Catch
Throw VbMakeException(New InvalidCastException(SR.Format(SR.InvalidCast_FromStringTo, Left(Value, 32), "Date")), vbErrors.IllegalFuncCall)
End Try
SetDate(NewDate)
End Set
End Property
Public ReadOnly Property Timer() As Double
Get
'Returns number of seconds past Midnight
Return (System.DateTime.Now.Ticks Mod System.TimeSpan.TicksPerDay) /
(TimeSpan.TicksPerMillisecond * 1000)
End Get
End Property
Private ReadOnly Property CurrentCalendar() As Calendar
Get
Return Threading.Thread.CurrentThread.CurrentCulture.Calendar
End Get
End Property
'============================================================================
' Date manipulation functions.
'============================================================================
Public Function DateAdd(Interval As DateInterval,
Number As Double,
DateValue As DateTime) As DateTime
Dim lNumber As Integer
lNumber = CInt(Fix(Number))
Select Case Interval
Case DateInterval.Year
Return CurrentCalendar.AddYears(DateValue, lNumber)
Case DateInterval.Month
Return CurrentCalendar.AddMonths(DateValue, lNumber)
Case DateInterval.Day,
DateInterval.DayOfYear,
DateInterval.Weekday
Return DateValue.AddDays(lNumber)
Case DateInterval.WeekOfYear
Return DateValue.AddDays(lNumber * 7.0#)
Case DateInterval.Hour
Return DateValue.AddHours(lNumber)
Case DateInterval.Minute
Return DateValue.AddMinutes(lNumber)
Case DateInterval.Second
Return DateValue.AddSeconds(lNumber)
Case DateInterval.Quarter
Return DateValue.AddMonths(lNumber * 3)
End Select
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "Interval"))
End Function
Public Function DateDiff(Interval As DateInterval,
Date1 As DateTime,
Date2 As DateTime,
Optional DayOfWeek As FirstDayOfWeek = FirstDayOfWeek.Sunday,
Optional WeekOfYear As FirstWeekOfYear = FirstWeekOfYear.Jan1) As Long
If WeekOfYear <> FirstWeekOfYear.System Then
End If
Dim tm As TimeSpan
Dim cal As Calendar
tm = Date2.Subtract(Date1)
Select Case Interval
Case DateInterval.Year
cal = CurrentCalendar
Return cal.GetYear(Date2) - cal.GetYear(Date1)
Case DateInterval.Month
cal = CurrentCalendar
Return (cal.GetYear(Date2) - cal.GetYear(Date1)) * 12 + cal.GetMonth(Date2) - cal.GetMonth(Date1)
Case DateInterval.Day,
DateInterval.DayOfYear
Return CLng(Fix(tm.TotalDays()))
Case DateInterval.Hour
Return CLng(Fix(tm.TotalHours()))
Case DateInterval.Minute
Return CLng(Fix(tm.TotalMinutes()))
Case DateInterval.Second
Return CLng(Fix(tm.TotalSeconds()))
Case DateInterval.WeekOfYear
Date1 = Date1.AddDays(-GetDayOfWeek(Date1, DayOfWeek))
Date2 = Date2.AddDays(-GetDayOfWeek(Date2, DayOfWeek))
tm = Date2.Subtract(Date1)
Return CLng(Fix(tm.TotalDays())) \ 7
Case DateInterval.Weekday
Return CLng(Fix(tm.TotalDays())) \ 7
Case DateInterval.Quarter
cal = CurrentCalendar
Return (cal.GetYear(Date2) - cal.GetYear(Date1)) * 4 + (cal.GetMonth(Date2) - 1) \ 3 - (cal.GetMonth(Date1) - 1) \ 3
End Select
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "Interval"))
End Function
Private Function GetDayOfWeek(dt As Date, weekdayFirst As FirstDayOfWeek) As Integer
If (weekdayFirst < FirstDayOfWeek.System OrElse weekdayFirst > FirstDayOfWeek.Saturday) Then
Throw VbMakeException(vbErrors.IllegalFuncCall)
End If
' If FirstWeekDay is 0, get offset from NLS.
If (weekdayFirst = FirstDayOfWeek.System) Then
weekdayFirst = CType(GetDateTimeFormatInfo().FirstDayOfWeek + 1, FirstDayOfWeek)
End If
Return (dt.DayOfWeek - weekdayFirst + 8) Mod 7 + 1
End Function
Public Function DatePart(Interval As DateInterval,
DateValue As DateTime,
Optional FirstDayOfWeekValue As FirstDayOfWeek = vbSunday,
Optional FirstWeekOfYearValue As FirstWeekOfYear = vbFirstJan1) As Integer
'Get the part asked for
Select Case Interval
Case DateInterval.Year
Return CurrentCalendar.GetYear(DateValue)
Case DateInterval.Month
Return CurrentCalendar.GetMonth(DateValue)
Case DateInterval.Day
Return CurrentCalendar.GetDayOfMonth(DateValue)
Case DateInterval.Hour
Return CurrentCalendar.GetHour(DateValue)
Case DateInterval.Minute
Return CurrentCalendar.GetMinute(DateValue)
Case DateInterval.Second
Return CurrentCalendar.GetSecond(DateValue)
Case DateInterval.Weekday
Return Weekday(DateValue, FirstDayOfWeekValue)
Case DateInterval.WeekOfYear
Dim WeekRule As CalendarWeekRule
Dim Day As DayOfWeek
If FirstDayOfWeekValue = vbUseSystemDayOfWeek Then
Day = GetCultureInfo().DateTimeFormat.FirstDayOfWeek
Else
Day = CType(FirstDayOfWeekValue - 1, DayOfWeek)
End If
Select Case FirstWeekOfYearValue
Case vbUseSystem
WeekRule = GetCultureInfo().DateTimeFormat.CalendarWeekRule
Case vbFirstJan1
WeekRule = CalendarWeekRule.FirstDay
Case vbFirstFourDays
WeekRule = CalendarWeekRule.FirstFourDayWeek
Case vbFirstFullWeek
WeekRule = CalendarWeekRule.FirstFullWeek
End Select
Return CurrentCalendar.GetWeekOfYear(DateValue, WeekRule, Day)
Case DateInterval.Quarter
Return ((DateValue.Month - 1) \ 3) + 1
Case DateInterval.DayOfYear
Return CurrentCalendar.GetDayOfYear(DateValue)
End Select
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "Interval"))
End Function
Public Function DateAdd(Interval As String,
Number As Double,
DateValue As Object) As DateTime
Dim dt1 As Date
Try
dt1 = CDate(DateValue)
Catch ex As StackOverflowException
Throw ex
Catch ex As OutOfMemoryException
Throw ex
Catch
Throw New InvalidCastException(SR.Format(SR.Argument_InvalidDateValue1, "DateValue"))
End Try
Return DateAdd(DateIntervalFromString(Interval), Number, dt1)
End Function
Public Function DateDiff(Interval As String,
Date1 As Object,
Date2 As Object,
Optional DayOfWeek As FirstDayOfWeek = FirstDayOfWeek.Sunday,
Optional WeekOfYear As FirstWeekOfYear = FirstWeekOfYear.Jan1) As Long
Dim dt1, dt2 As Date
Try
dt1 = CDate(Date1)
Catch ex As StackOverflowException
Throw ex
Catch ex As OutOfMemoryException
Throw ex
Catch
Throw New InvalidCastException(SR.Format(SR.Argument_InvalidDateValue1, "Date1"))
End Try
Try
dt2 = CDate(Date2)
Catch ex As StackOverflowException
Throw ex
Catch ex As OutOfMemoryException
Throw ex
Catch
Throw New InvalidCastException(SR.Format(SR.Argument_InvalidDateValue1, "Date2"))
End Try
Return DateDiff(DateIntervalFromString(Interval), dt1, dt2, DayOfWeek, WeekOfYear)
End Function
Public Function DatePart(Interval As String,
DateValue As Object,
Optional DayOfWeek As FirstDayOfWeek = FirstDayOfWeek.Sunday,
Optional WeekOfYear As FirstWeekOfYear = FirstWeekOfYear.Jan1) As Integer
Dim dt1 As Date
Try
dt1 = CDate(DateValue)
Catch ex As StackOverflowException
Throw ex
Catch ex As OutOfMemoryException
Throw ex
Catch
Throw New InvalidCastException(SR.Format(SR.Argument_InvalidDateValue1, "DateValue"))
End Try
Return DatePart(DateIntervalFromString(Interval), dt1, DayOfWeek, WeekOfYear)
End Function
Private Function DateIntervalFromString(Interval As String) As DateInterval
If Interval IsNot Nothing Then
Interval = Interval.ToUpperInvariant()
End If
Select Case Interval
Case "YYYY"
Return DateInterval.Year
Case "Y"
Return DateInterval.DayOfYear
Case "M"
Return DateInterval.Month
Case "D"
Return DateInterval.Day
Case "H"
Return DateInterval.Hour
Case "N"
Return DateInterval.Minute
Case "S"
Return DateInterval.Second
Case "WW"
Return DateInterval.WeekOfYear
Case "W"
Return DateInterval.Weekday
Case "Q"
Return DateInterval.Quarter
Case Else
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "Interval"))
End Select
End Function
'============================================================================
' Date value functions.
'============================================================================
Public Function DateSerial([Year] As Integer, [Month] As Integer, [Day] As Integer) As DateTime
'We have to handle negative months and days
' so we start with the year and add months and days
Dim cal As Calendar = CurrentCalendar
Dim Result As DateTime
If Year < 0 Then
Year = cal.GetYear(System.DateTime.Today) + Year
ElseIf Year < 100 Then
Year = cal.ToFourDigitYear(Year)
End If
'*** BEGIN PERFOPT ***
'*** Gregorian Calendar perf optimization
'*** The AddMonths/AddDays require excessive conversion to/from ticks
'*** so we special case
If TypeOf cal Is GregorianCalendar Then
If (Month >= 1 AndAlso Month <= 12) AndAlso (Day >= 1 AndAlso Day <= 28) Then
'Uses 28 so we don't have to use the calendar to obtain
' the number of days in the month, which is the cause of the
' extra overhead we are trying to avoid
Return New DateTime(Year, Month, Day)
End If
End If
'*** END PERFOPT ***
Try
Result = cal.ToDateTime(Year, 1, 1, 0, 0, 0, 0)
Catch ex As StackOverflowException
Throw ex
Catch ex As OutOfMemoryException
Throw ex
Catch
Throw VbMakeException(New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "Year")), vbErrors.IllegalFuncCall)
End Try
Try
Result = cal.AddMonths(Result, Month - 1)
Catch ex As StackOverflowException
Throw ex
Catch ex As OutOfMemoryException
Throw ex
Catch
Throw VbMakeException(New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "Month")), vbErrors.IllegalFuncCall)
End Try
Try
Result = cal.AddDays(Result, Day - 1)
Catch ex As StackOverflowException
Throw ex
Catch ex As OutOfMemoryException
Throw ex
Catch
Throw VbMakeException(New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "Day")), vbErrors.IllegalFuncCall)
End Try
Return Result
End Function
Public Function TimeSerial(Hour As Integer, Minute As Integer, Second As Integer) As DateTime
Const SecondsInDay As Integer = (24 * 60 * 60)
Dim TotalSeconds As Integer = (Hour * 60 * 60) + (Minute * 60) + Second
If TotalSeconds < 0 Then
'Wrap clock
TotalSeconds += SecondsInDay
End If
Return (New DateTime(TotalSeconds * TimeSpan.TicksPerSecond))
End Function
Public Function DateValue([StringDate] As String) As DateTime
Return CDate([StringDate]).Date
End Function
Public Function TimeValue([StringTime] As String) As DateTime
Return New DateTime(CDate([StringTime]).Ticks Mod TimeSpan.TicksPerDay)
End Function
'============================================================================
' Date/time part functions.
'============================================================================
Public Function Year(DateValue As DateTime) As Integer
Return CurrentCalendar.GetYear(DateValue)
End Function
Public Function Month(DateValue As DateTime) As Integer
Return CurrentCalendar.GetMonth(DateValue)
End Function
Public Function Day(DateValue As DateTime) As Integer
Return CurrentCalendar.GetDayOfMonth(DateValue)
End Function
Public Function Hour([TimeValue] As DateTime) As Integer
Return CurrentCalendar.GetHour([TimeValue])
End Function
Public Function Minute([TimeValue] As DateTime) As Integer
Return CurrentCalendar.GetMinute([TimeValue])
End Function
Public Function Second([TimeValue] As DateTime) As Integer
Return CurrentCalendar.GetSecond([TimeValue])
End Function
Public Function Weekday(DateValue As DateTime, Optional DayOfWeek As FirstDayOfWeek = FirstDayOfWeek.Sunday) As Integer
If DayOfWeek = FirstDayOfWeek.System Then
'
DayOfWeek = CType(DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek + 1, FirstDayOfWeek)
ElseIf (DayOfWeek < FirstDayOfWeek.Sunday) OrElse (DayOfWeek > FirstDayOfWeek.Saturday) Then
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "DayOfWeek"))
End If
'Get the day from the date
Dim iDayOfWeek As Integer
iDayOfWeek = CurrentCalendar.GetDayOfWeek(DateValue) + 1 ' System.DateTime uses Sunday = 0 thru Satuday = 6
Return ((iDayOfWeek - DayOfWeek + 7) Mod 7) + 1
End Function
'============================================================================
' Date name functions.
'============================================================================
Public Function MonthName(Month As Integer, Optional Abbreviate As Boolean = False) As String
Dim Result As String
If Month < 1 OrElse Month > 13 Then
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "Month"))
End If
If Abbreviate Then
Result = GetDateTimeFormatInfo().GetAbbreviatedMonthName(Month)
Else
Result = GetDateTimeFormatInfo().GetMonthName(Month)
End If
If Result.Length = 0 Then
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "Month"))
End If
Return Result
End Function
Public Function WeekdayName(Weekday As Integer, Optional Abbreviate As Boolean = False, Optional FirstDayOfWeekValue As FirstDayOfWeek = FirstDayOfWeek.System) As String
Dim dtfi As DateTimeFormatInfo
Dim Result As String
If (Weekday < 1) OrElse (Weekday > 7) Then
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "Weekday"))
End If
If (FirstDayOfWeekValue < 0) OrElse (FirstDayOfWeekValue > 7) Then
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "FirstDayOfWeekValue"))
End If
dtfi = CType(GetCultureInfo().GetFormat(GetType(System.Globalization.DateTimeFormatInfo)), DateTimeFormatInfo) 'Returns a read-only object
If FirstDayOfWeekValue = 0 Then
FirstDayOfWeekValue = CType(CInt(dtfi.FirstDayOfWeek) + 1, FirstDayOfWeek)
End If
Try
If Abbreviate Then
Result = dtfi.GetAbbreviatedDayName(CType((Weekday + FirstDayOfWeekValue - 2) Mod 7, System.DayOfWeek))
Else
Result = dtfi.GetDayName(CType((Weekday + FirstDayOfWeekValue - 2) Mod 7, System.DayOfWeek))
End If
Catch ex As StackOverflowException
Throw ex
Catch ex As OutOfMemoryException
Throw ex
Catch
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "Weekday"))
End Try
If Result.Length = 0 Then
Throw New ArgumentException(SR.Format(SR.Argument_InvalidValue1, "Weekday"))
End If
Return Result
End Function
End Module
End Namespace