forked from dotnet/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMethods.vb
403 lines (357 loc) · 17.5 KB
/
Methods.vb
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
' Visual Basic .NET Document
Option Strict On
Module modMain
Public Sub Main()
ShowSchedule() ' Add method
Console.WriteLine("----------")
ShowStartOfWorkWeek() ' AddDays method
Console.WriteLine("----------")
ShowShiftStartTimes()
Console.WriteLine("----------")
ShowQuarters()
Console.WriteLine("----------")
DisplayTimes()
Console.WriteLine("----------")
ShowLegalLicenseAge()
Console.WriteLine("----------")
CompareForEquality1()
Console.WriteLine("----------")
CompareForEquality2()
Console.WriteLine("----------")
CompareForEquality3()
Console.WriteLine("----------")
CompareExactly()
Console.WriteLine("----------")
Subtract1()
Console.WriteLine("----------")
Subtract2()
Console.WriteLine("----------")
ConvertToLocal()
Console.WriteLine("----------")
'' ConvertOffsets()
Console.WriteLine("----------")
ConvertToUniversal()
Console.WriteLine("----------")
End Sub
Private Sub ShowSchedule()
' <Snippet1>
Dim takeOff As New DateTimeOffset(#6/1/2007 7:55AM#, _
New TimeSpan(-5, 0, 0))
Dim currentTime As DateTimeOffset = takeOff
Dim flightTimes() As TimeSpan = New TimeSpan() _
{New TimeSpan(2, 25, 0), New TimeSpan(1, 48, 0)}
Console.WriteLine("Takeoff is scheduled for {0:d} at {0:T}.", _
takeOff)
For ctr As Integer = flightTimes.GetLowerBound(0) To _
flightTimes.GetUpperBound(0)
currentTime = currentTime.Add(flightTimes(ctr))
Console.WriteLine("Destination #{0} at {1}.", ctr + 1, currentTime)
Next
' </Snippet1>
End Sub
Private Sub ShowStartOfWorkWeek()
' <Snippet2>
Dim workDay As New DateTimeOffset(#3/1/2008 9:00AM#, _
DateTimeOffset.Now.Offset)
Dim month As Integer = workDay.Month
' Start with the first Monday of the month
If workDay.DayOfWeek <> DayOfWeek.Monday Then
If workDay.DayOfWeek = DayOfWeek.Sunday Then
workDay = workDay.AddDays(1)
Else
workDay = workDay.AddDays(8 - CInt(workDay.DayOfWeek))
End If
End If
Console.WriteLine("Beginning of Work Week In {0:MMMM} {0:yyyy}:", workDay)
' Add one week to the current date
Do While workDay.Month = month
Console.WriteLine(" {0:dddd}, {0:MMMM}{0: d}", workDay)
workDay = workDay.AddDays(7)
Loop
' The example produces the following output:
' Beginning of Work Week In March 2008:
' Monday, March 3
' Monday, March 10
' Monday, March 17
' Monday, March 24
' Monday, March 31
' </Snippet2>
End Sub
Private Sub ShowShiftStartTimes()
' <Snippet3>
Const SHIFT_LENGTH As Integer = 8
Dim startTime As New DateTimeOffset(#8/6/2007 12:00:00AM#, _
DateTimeOffset.Now.Offset)
Dim startOfShift As DateTimeOffset = startTime.AddHours(SHIFT_LENGTH)
Console.WriteLine("Shifts for the week of {0:D}", startOfShift)
Do
' Exclude third shift
If startOfShift.Hour > 6 Then _
Console.WriteLine(" {0:d} at {0:T}", startOfShift)
startOfShift = startOfShift.AddHours(SHIFT_LENGTH)
Loop While startOfShift.DayOfWeek <> DayOfWeek.Saturday And _
startOfShift.DayOfWeek <> DayOfWeek.Sunday
' The example produces the following output:
'
' Shifts for the week of Monday, August 06, 2007
' 8/6/2007 at 8:00:00 AM
' 8/6/2007 at 4:00:00 PM
' 8/7/2007 at 8:00:00 AM
' 8/7/2007 at 4:00:00 PM
' 8/8/2007 at 8:00:00 AM
' 8/8/2007 at 4:00:00 PM
' 8/9/2007 at 8:00:00 AM
' 8/9/2007 at 4:00:00 PM
' 8/10/2007 at 8:00:00 AM
' 8/10/2007 at 4:00:00 PM
' </Snippet3>
End Sub
Private Sub ShowQuarters()
' <Snippet4>
Dim quarterDate As New DateTimeOffset(#01/01/2007#, DateTimeOffset.Now.Offset)
For ctr As Integer = 1 To 4
Console.WriteLine("Quarter {0}: {1:MMMM d}", ctr, quarterDate)
quarterDate = quarterDate.AddMonths(3)
Next
' This example produces the following output:
' Quarter 1: January 1
' Quarter 2: April 1
' Quarter 3: July 1
' Quarter 4: October 1
' </Snippet4>
End Sub
Private Sub DisplayTimes()
' <Snippet5>
Dim lapTimes() As Double = {1.308, 1.283, 1.325, 1.3625, 1.317, 1.267}
Dim currentTime As New DateTimeOffset(#1:30PM#, DateTimeOffset.Now.Offset)
Console.WriteLine("Start: {0:T}", currentTime)
For ctr As Integer = lapTimes.GetLowerBound(0) To lapTimes.GetUpperBound(0)
currentTime = currentTime.AddMinutes(lapTimes(ctr))
Console.WriteLIne("Lap {0}: {1:T}", ctr + 1, currentTime)
Next
' The example produces the following output:
' Start: 1:30:00 PM
' Lap 1: 1:31:18 PM
' Lap 2: 1:32:35 PM
' Lap 3: 1:33:54 PM
' Lap 4: 1:35:16 PM
' Lap 5: 1:36:35 PM
' Lap 6: 1:37:51 PM
' </Snippet5>
End Sub
Private Sub ShowLegalLicenseAge()
' <Snippet6>
Const minimumAge As Integer = 16
Dim dateToday As DateTimeOffset = DateTimeOffset.Now
Dim latestBirthday As DateTimeOffset = dateToday.AddYears(-1 * minimumAge)
Console.WriteLine("To possess a driver's license, you must have been born on or before {0:d}.", _
latestBirthday)
' </Snippet6>
End Sub
Private Sub CompareForEquality1()
' <Snippet9>
Dim firstTime As New DateTimeOffset(#09/01/2007 6:45:00AM#, _
New TimeSpan(-7, 0, 0))
Dim secondTime As DateTimeOffset = firstTime
Console.WriteLine("{0} = {1}: {2}", _
firstTime, secondTime, _
firstTime.Equals(secondTime))
secondTime = New DateTimeOffset(#09/01/2007 6:45:00AM#, _
New TimeSpan(-6, 0, 0))
Console.WriteLine("{0} = {1}: {2}", _
firstTime, secondTime, _
firstTime.Equals(secondTime))
secondTime = New DateTimeOffset(#09/01/2007 8:45:00AM#, _
New TimeSpan(-5, 0, 0))
Console.WriteLine("{0} = {1}: {2}", _
firstTime, secondTime, _
firstTime.Equals(secondTime))
' The example displays the following output to the console:
' 9/1/2007 6:45:00 AM -07:00 = 9/1/2007 6:45:00 AM -07:00: True
' 9/1/2007 6:45:00 AM -07:00 = 9/1/2007 6:45:00 AM -06:00: False
' 9/1/2007 6:45:00 AM -07:00 = 9/1/2007 8:45:00 AM -05:00: True
' </Snippet9>
End Sub
Private Sub CompareForEquality2()
' <Snippet10>
Dim firstTime As New DateTimeOffset(#09/01/2007 6:45:00AM#, _
New TimeSpan(-7, 0, 0))
Dim secondTime As Object = firstTime
Console.WriteLine("{0} = {1}: {2}", _
firstTime, secondTime, _
firstTime.Equals(secondTime))
secondTime = New DateTimeOffset(#09/01/2007 6:45:00AM#, _
New TimeSpan(-6, 0, 0))
Console.WriteLine("{0} = {1}: {2}", _
firstTime, secondTime, _
firstTime.Equals(secondTime))
secondTime = New DateTimeOffset(#09/01/2007 8:45:00AM#, _
New TimeSpan(-5, 0, 0))
Console.WriteLine("{0} = {1}: {2}", _
firstTime, secondTime, _
firstTime.Equals(secondTime))
secondTime = Nothing
Console.WriteLine("{0} = {1}: {2}", _
firstTime, secondTime, _
firstTime.Equals(secondTime))
secondTime = #9/1/2007 6:45AM#
Console.WriteLine("{0} = {1}: {2}", _
firstTime, secondTime, _
firstTime.Equals(secondTime))
' The example displays the following output to the console:
' 9/1/2007 6:45:00 AM -07:00 = 9/1/2007 6:45:00 AM -07:00: True
' 9/1/2007 6:45:00 AM -07:00 = 9/1/2007 6:45:00 AM -06:00: False
' 9/1/2007 6:45:00 AM -07:00 = 9/1/2007 8:45:00 AM -05:00: True
' 9/1/2007 6:45:00 AM -07:00 = : False
' 9/1/2007 6:45:00 AM -07:00 = 9/1/2007 6:45:00 AM: False
' </Snippet10>
End Sub
Private Sub CompareForEquality3()
' <Snippet11>
Dim firstTime As New DateTimeOffset(#11/15/2007 11:35AM#, _
DateTimeOffset.Now.Offset)
Dim secondTime As DateTimeOffset = firstTime
Console.WriteLine("{0} = {1}: {2}", _
firstTime, secondTime, _
DateTimeOffset.Equals(firstTime, secondTime))
' The value of firstTime remains unchanged
secondTime = New DateTimeOffset(firstTime.DateTime, _
TimeSpan.FromHours(firstTime.Offset.Hours + 1))
Console.WriteLine("{0} = {1}: {2}", _
firstTime, secondTime, _
DateTimeOffset.Equals(firstTime, secondTime))
' value of firstTime remains unchanged
secondTime = New DateTimeOffset(firstTime.DateTime + TimeSpan.FromHours(1), _
TimeSpan.FromHours(firstTime.Offset.Hours + 1))
Console.WriteLine("{0} = {1}: {2}", _
firstTime, secondTime, _
DateTimeOffset.Equals(firstTime, secondTime))
' The example produces the following output:
' 11/15/2007 11:35:00 AM -07:00 = 11/15/2007 11:35:00 AM -07:00: True
' 11/15/2007 11:35:00 AM -07:00 = 11/15/2007 11:35:00 AM -06:00: False
' 11/15/2007 11:35:00 AM -07:00 = 11/15/2007 12:35:00 PM -06:00: True
' </Snippet11>
End Sub
Private Sub CompareExactly
' <Snippet12>
Dim instanceTime As New DateTimeOffset(#10/31/2007 12:00AM#, _
DateTimeOffset.Now.Offset)
Dim otherTime As DateTimeOffset = instanceTime
Console.WriteLine("{0} = {1}: {2}", _
instanceTime, otherTime, _
instanceTime.EqualsExact(otherTime))
otherTime = New DateTimeOffset(instanceTime.DateTime, _
TimeSpan.FromHours(instanceTime.Offset.Hours + 1))
Console.WriteLine("{0} = {1}: {2}", _
instanceTime, otherTime, _
instanceTime.EqualsExact(otherTime))
otherTime = New DateTimeOffset(instanceTime.DateTime + TimeSpan.FromHours(1), _
TimeSpan.FromHours(instanceTime.Offset.Hours + 1))
Console.WriteLine("{0} = {1}: {2}", _
instanceTime, otherTime, _
instanceTime.EqualsExact(otherTime))
' The example produces the following output:
' 10/31/2007 12:00:00 AM -07:00 = 10/31/2007 12:00:00 AM -07:00: True
' 10/31/2007 12:00:00 AM -07:00 = 10/31/2007 12:00:00 AM -06:00: False
' 10/31/2007 12:00:00 AM -07:00 = 10/31/2007 1:00:00 AM -06:00: False
' </Snippet12>
End Sub
Private Sub Subtract1()
' <Snippet13>
Dim firstDate As New DateTimeOffset(#10/25/2018 6:00PM#, _
New TimeSpan(-7, 0, 0))
Dim secondDate As New DateTimeOffset(#10/25/2018 6:00PM#, _
New TimeSpan(-5, 0, 0))
Dim thirdDate As New DateTimeOffset(#9/28/2018 9:00AM#, _
New TimeSpan(-7, 0, 0))
Dim difference As TimeSpan
difference = firstDate.Subtract(secondDate)
Console.WriteLine($"({firstDate}) - ({secondDate}): {difference.Days} days, {difference.Hours}:{difference.Minutes:d2}")
difference = firstDate.Subtract(thirdDate)
Console.WriteLine($"({firstDate}) - ({thirdDate}): {difference.Days} days, {difference.Hours}:{difference.Minutes:d2}")
' The example produces the following output:
' (10/25/2018 6:00:00 PM -07:00) - (10/25/2018 6:00:00 PM -05:00): 0 days, 2:00
' (10/25/2018 6:00:00 PM -07:00) - (9/28/2018 9:00:00 AM -07:00): 27 days, 9:00
' </Snippet13>
End Sub
Private Sub Subtract2()
' <Snippet14>
Dim offsetDate As New DateTimeOffset(#12/3/2007 11:30AM#, _
New TimeSpan(-8, 0, 0))
Dim duration As New TimeSpan(7, 18, 0, 0)
Console.WriteLine(offsetDate.Subtract(duration)) ' Displays 11/25/2007 5:30:00 PM -08:00
' </Snippet14>
End Sub
Private Sub ConvertToLocal()
' <Snippet15>
' Local time changes on 3/11/2007 at 2:00 AM
Dim originalTime, localTime As DateTimeOffset
originalTime = New DateTimeOffset(#03/11/2007 3:00AM#, _
New TimeSpan(-6, 0, 0))
localTime = originalTime.ToLocalTime()
Console.WriteLine("Converted {0} to {1}.", originalTime.ToString(), _
localTime.ToString())
originalTime = New DateTimeOffset(#03/11/2007 4:00AM#, _
New TimeSpan(-6, 0, 0))
localTime = originalTime.ToLocalTime()
Console.WriteLine("Converted {0} to {1}.", originalTime.ToString(), _
localTime.ToString())
' Define a summer UTC time
originalTime = New DateTimeOffset(#6/15/2007 8:00AM#, _
TimeSpan.Zero)
localTime = originalTime.ToLocalTime()
Console.WriteLine("Converted {0} to {1}.", originalTime.ToString(), _
localTime.ToString())
' Define a winter time
originalTime = New DateTimeOffset(#11/30/2007 2:00PM#, _
New TimeSpan(3, 0, 0))
localTime = originalTime.ToLocalTime()
Console.WriteLine("Converted {0} to {1}.", originalTime.ToString(), _
localTime.ToString())
' The example produces the following output:
' Converted 3/11/2007 3:00:00 AM -06:00 to 3/11/2007 1:00:00 AM -08:00.
' Converted 3/11/2007 4:00:00 AM -06:00 to 3/11/2007 3:00:00 AM -07:00.
' Converted 6/15/2007 8:00:00 AM +00:00 to 6/15/2007 1:00:00 AM -07:00.
' Converted 11/30/2007 2:00:00 PM +03:00 to 11/30/2007 3:00:00 AM -08:00.
' </Snippet15>
End Sub
Private Sub ConvertToUniversal()
' <Snippet16>
Dim localTime, otherTime, universalTime As DateTimeOffset
' Define local time in local time zone
localTime = New DateTimeOffset(#6/15/2007 12:00:00PM#)
Console.WriteLine("Local time: {0}", localTime)
Console.WriteLine()
' Convert local time to offset 0 and assign to otherTime
otherTime = localTime.ToOffset(TimeSpan.Zero)
Console.WriteLine("Other time: {0}", otherTime)
Console.WriteLine("{0} = {1}: {2}", _
localTime, otherTime, _
localTime.Equals(otherTime))
Console.WriteLine("{0} exactly equals {1}: {2}", _
localTime, otherTime, _
localTime.EqualsExact(otherTime))
Console.WriteLine()
' Convert other time to UTC
universalTime = localTime.ToUniversalTime()
Console.WriteLine("Universal time: {0}", universalTime)
Console.WriteLine("{0} = {1}: {2}", _
otherTime, universalTime, _
universalTime.Equals(otherTime))
Console.WriteLine("{0} exactly equals {1}: {2}", _
otherTime, universalTime, _
universalTime.EqualsExact(otherTime))
Console.WriteLine()
' The example produces the following output to the console:
' Local time: 6/15/2007 12:00:00 PM -07:00
'
' Other time: 6/15/2007 7:00:00 PM +00:00
' 6/15/2007 12:00:00 PM -07:00 = 6/15/2007 7:00:00 PM +00:00: True
' 6/15/2007 12:00:00 PM -07:00 exactly equals 6/15/2007 7:00:00 PM +00:00: False
'
' Universal time: 6/15/2007 7:00:00 PM +00:00
' 6/15/2007 7:00:00 PM +00:00 = 6/15/2007 7:00:00 PM +00:00: True
' 6/15/2007 7:00:00 PM +00:00 exactly equals 6/15/2007 7:00:00 PM +00:00: True
' </Snippet16>
End Sub
End Module