forked from dotnet/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPersistence.vb
427 lines (388 loc) · 17.4 KB
/
Persistence.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
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
Imports System.Globalization
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Threading
Imports System.Xml.Serialization
Imports SystemDateTimeReference.DateTimeExtensions
Module Persistence
Private Const filenameTxt As String = ".\BadDates.txt"
Sub Snippets()
File.Delete(filenameTxt)
PersistAsLocalStrings()
File.Delete(filenameTxt)
PersistAsInvariantStrings()
File.Delete(filenameTxt)
File.Delete(filenameInts)
PersistAsIntegers()
File.Delete(filenameInts)
File.Delete(filenameXml)
PersistAsXml()
File.Delete(filenameXml)
File.Delete(filenameBin)
PersistBinary()
File.Delete(filenameBin)
SaveDateWithTimeZone()
RestoreDateWithTimeZone()
End Sub
' <Snippet1>
Public Sub PersistAsLocalStrings()
SaveDatesAsStrings()
RestoreDatesAsStrings()
End Sub
Private Sub SaveDatesAsStrings()
Dim dates As Date() = {#6/14/2014 6:32AM#, #7/10/2014 11:49PM#,
#1/10/2015 1:16AM#, #12/20/2014 9:45PM#,
#6/2/2014 3:14PM#}
Dim output As String = Nothing
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}")
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:")
For ctr As Integer = 0 To dates.Length - 1
Console.WriteLine(dates(ctr).ToString("f"))
output += dates(ctr).ToString() + If(ctr <> dates.Length - 1, "|", "")
Next
Dim sw As New StreamWriter(filenameTxt)
sw.Write(output)
sw.Close()
Console.WriteLine("Saved dates...")
End Sub
Private Sub RestoreDatesAsStrings()
TimeZoneInfo.ClearCachedData()
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}")
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB")
Dim sr As New StreamReader(filenameTxt)
Dim inputValues As String() = sr.ReadToEnd().Split({"|"c}, StringSplitOptions.RemoveEmptyEntries)
sr.Close()
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:")
For Each inputValue In inputValues
Dim dateValue As Date
If DateTime.TryParse(inputValue, dateValue) Then
Console.WriteLine($"'{inputValue}' --> {dateValue:f}")
Else
Console.WriteLine($"Cannot parse '{inputValue}'")
End If
Next
Console.WriteLine("Restored dates...")
End Sub
' When saved on an en-US system, the example displays the following output:
' Current Time Zone: (UTC-08:00) Pacific Time (US & Canada)
' The dates on an en-US system:
' Saturday, June 14, 2014 6:32 AM
' Thursday, July 10, 2014 11:49 PM
' Saturday, January 10, 2015 1:16 AM
' Saturday, December 20, 2014 9:45 PM
' Monday, June 02, 2014 3:14 PM
' Saved dates...
'
' When restored on an en-GB system, the example displays the following output:
' Current Time Zone: (UTC) Dublin, Edinburgh, Lisbon, London
' The dates on an en-GB system:
' Cannot parse '6/14/2014 6:32:00 AM'
' '7/10/2014 11:49:00 PM' --> 07 October 2014 23:49
' '1/10/2015 1:16:00 AM' --> 01 October 2015 01:16
' Cannot parse '12/20/2014 9:45:00 PM'
' '6/2/2014 3:14:00 PM' --> 06 February 2014 15:14
' Restored dates...
' </Snippet1>
' <Snippet2>
Public Sub PersistAsInvariantStrings()
SaveDatesAsInvariantStrings()
RestoreDatesAsInvariantStrings()
End Sub
Private Sub SaveDatesAsInvariantStrings()
Dim dates As Date() = {#6/14/2014 6:32AM#, #7/10/2014 11:49PM#,
#1/10/2015 1:16AM#, #12/20/2014 9:45PM#,
#6/2/2014 3:14PM#}
Dim output As String = Nothing
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}")
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:")
For ctr As Integer = 0 To dates.Length - 1
Console.WriteLine(dates(ctr).ToString("f"))
output += dates(ctr).ToUniversalTime().ToString("O", CultureInfo.InvariantCulture) +
If(ctr <> dates.Length - 1, "|", "")
Next
Dim sw As New StreamWriter(filenameTxt)
sw.Write(output)
sw.Close()
Console.WriteLine("Saved dates...")
End Sub
Private Sub RestoreDatesAsInvariantStrings()
TimeZoneInfo.ClearCachedData()
Console.WriteLine("Current Time Zone: {0}",
TimeZoneInfo.Local.DisplayName)
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB")
Dim sr As New StreamReader(filenameTxt)
Dim inputValues As String() = sr.ReadToEnd().Split({"|"c}, StringSplitOptions.RemoveEmptyEntries)
sr.Close()
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:")
For Each inputValue In inputValues
Dim dateValue As Date
If DateTime.TryParseExact(inputValue, "O", CultureInfo.InvariantCulture,
DateTimeStyles.RoundtripKind, dateValue) Then
Console.WriteLine($"'{inputValue}' --> {dateValue.ToLocalTime():f}")
Else
Console.WriteLine($"Cannot parse '{inputValue}'")
End If
Next
Console.WriteLine("Restored dates...")
End Sub
' When saved on an en-US system, the example displays the following output:
' Current Time Zone: (UTC-08:00) Pacific Time (US & Canada)
' The dates on an en-US system:
' Saturday, June 14, 2014 6:32 AM
' Thursday, July 10, 2014 11:49 PM
' Saturday, January 10, 2015 1:16 AM
' Saturday, December 20, 2014 9:45 PM
' Monday, June 02, 2014 3:14 PM
' Saved dates...
'
' When restored on an en-GB system, the example displays the following output:
' Current Time Zone: (UTC) Dublin, Edinburgh, Lisbon, London
' The dates on an en-GB system:
' '2014-06-14T13:32:00.0000000Z' --> 14 June 2014 14:32
' '2014-07-11T06:49:00.0000000Z' --> 11 July 2014 07:49
' '2015-01-10T09:16:00.0000000Z' --> 10 January 2015 09:16
' '2014-12-21T05:45:00.0000000Z' --> 21 December 2014 05:45
' '2014-06-02T22:14:00.0000000Z' --> 02 June 2014 23:14
' Restored dates...
' </Snippet2>
Private Const filenameInts As String = ".\IntDates.bin"
' <Snippet3>
Public Sub PersistAsIntegers()
SaveDatesAsIntegers()
RestoreDatesAsIntegers()
End Sub
Private Sub SaveDatesAsIntegers()
Dim dates As Date() = {#6/14/2014 6:32AM#, #7/10/2014 11:49PM#,
#1/10/2015 1:16AM#, #12/20/2014 9:45PM#,
#6/2/2014 3:14PM#}
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}")
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:")
Dim ticks(dates.Length - 1) As Long
For ctr As Integer = 0 To dates.Length - 1
Console.WriteLine(dates(ctr).ToString("f"))
ticks(ctr) = dates(ctr).ToUniversalTime().Ticks
Next
Dim fs As New FileStream(filenameInts, FileMode.Create)
Dim bw As New BinaryWriter(fs)
bw.Write(ticks.Length)
For Each tick In ticks
bw.Write(tick)
Next
bw.Close()
Console.WriteLine("Saved dates...")
End Sub
Private Sub RestoreDatesAsIntegers()
TimeZoneInfo.ClearCachedData()
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}")
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB")
Dim fs As New FileStream(filenameInts, FileMode.Open)
Dim br As New BinaryReader(fs)
Dim items As Integer
Dim dates As DateTime()
Try
items = br.ReadInt32()
ReDim dates(items - 1)
For ctr As Integer = 0 To items - 1
Dim ticks As Long = br.ReadInt64()
dates(ctr) = New DateTime(ticks).ToLocalTime()
Next
Catch e As EndOfStreamException
Console.WriteLine("File corruption detected. Unable to restore data...")
Exit Sub
Catch e As IOException
Console.WriteLine("Unspecified I/O error. Unable to restore data...")
Exit Sub
Catch e As OutOfMemoryException 'Thrown in array initialization.
Console.WriteLine("File corruption detected. Unable to restore data...")
Exit Sub
Finally
br.Close()
End Try
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:")
For Each value In dates
Console.WriteLine(value.ToString("f"))
Next
Console.WriteLine("Restored dates...")
End Sub
' When saved on an en-US system, the example displays the following output:
' Current Time Zone: (UTC-08:00) Pacific Time (US & Canada)
' The dates on an en-US system:
' Saturday, June 14, 2014 6:32 AM
' Thursday, July 10, 2014 11:49 PM
' Saturday, January 10, 2015 1:16 AM
' Saturday, December 20, 2014 9:45 PM
' Monday, June 02, 2014 3:14 PM
' Saved dates...
'
' When restored on an en-GB system, the example displays the following output:
' Current Time Zone: (UTC) Dublin, Edinburgh, Lisbon, London
' The dates on an en-GB system:
' 14 June 2014 14:32
' 11 July 2014 07:49
' 10 January 2015 09:16
' 21 December 2014 05:45
' 02 June 2014 23:14
' Restored dates...
' </Snippet3>
Private Const filenameXml As String = ".\LeapYears.xml"
' <Snippet4>
Public Sub PersistAsXml()
' Serialize the data.
Dim leapYears As New List(Of DateTime)()
For year As Integer = 2000 To 2100 Step 4
If Date.IsLeapYear(year) Then
leapYears.Add(New Date(year, 2, 29))
End If
Next
Dim dateArray As DateTime() = leapYears.ToArray()
Dim serializer As New XmlSerializer(dateArray.GetType())
Dim sw As TextWriter = New StreamWriter(filenameXml)
Try
serializer.Serialize(sw, dateArray)
Catch e As InvalidOperationException
Console.WriteLine(e.InnerException.Message)
Finally
If sw IsNot Nothing Then sw.Close()
End Try
' Deserialize the data.
Dim deserializedDates As Date()
Using fs As New FileStream(filenameXml, FileMode.Open)
deserializedDates = CType(serializer.Deserialize(fs), Date())
End Using
' Display the dates.
Console.WriteLine($"Leap year days from 2000-2100 on an {Thread.CurrentThread.CurrentCulture.Name} system:")
Dim nItems As Integer
For Each dat In deserializedDates
Console.Write($" {dat:d} ")
nItems += 1
If nItems Mod 5 = 0 Then Console.WriteLine()
Next
End Sub
' The example displays the following output:
' Leap year days from 2000-2100 on an en-GB system:
' 29/02/2000 29/02/2004 29/02/2008 29/02/2012 29/02/2016
' 29/02/2020 29/02/2024 29/02/2028 29/02/2032 29/02/2036
' 29/02/2040 29/02/2044 29/02/2048 29/02/2052 29/02/2056
' 29/02/2060 29/02/2064 29/02/2068 29/02/2072 29/02/2076
' 29/02/2080 29/02/2084 29/02/2088 29/02/2092 29/02/2096
' </Snippet4>
Private Const filenameBin As String = ".\Dates.bin"
' <Snippet5>
Public Sub PersistBinary()
SaveDatesBinary()
RestoreDatesBinary()
End Sub
Private Sub SaveDatesBinary()
Dim dates As Date() = {#6/14/2014 6:32AM#, #7/10/2014 11:49PM#,
#1/10/2015 1:16AM#, #12/20/2014 9:45PM#,
#6/2/2014 3:14PM#}
Dim fs As New FileStream(filenameBin, FileMode.Create)
Dim bin As New BinaryFormatter()
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}")
Console.WriteLine("The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:")
For ctr As Integer = 0 To dates.Length - 1
Console.WriteLine(dates(ctr).ToString("f"))
dates(ctr) = dates(ctr).ToUniversalTime()
Next
bin.Serialize(fs, dates)
fs.Close()
Console.WriteLine("Saved dates...")
End Sub
Private Sub RestoreDatesBinary()
TimeZoneInfo.ClearCachedData()
Console.WriteLine("Current Time Zone: {TimeZoneInfo.Local.DisplayName}")
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB")
Dim fs As New FileStream(filenameBin, FileMode.Open)
Dim bin As New BinaryFormatter()
Dim dates As DateTime() = DirectCast(bin.Deserialize(fs), Date())
fs.Close()
Console.WriteLine("The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:")
For Each value In dates
Console.WriteLine(value.ToLocalTime().ToString("f"))
Next
Console.WriteLine("Restored dates...")
End Sub
' When saved on an en-US system, the example displays the following output:
' Current Time Zone: (UTC-08:00) Pacific Time (US & Canada)
' The dates on an en-US system:
' Saturday, June 14, 2014 6:32 AM
' Thursday, July 10, 2014 11:49 PM
' Saturday, January 10, 2015 1:16 AM
' Saturday, December 20, 2014 9:45 PM
' Monday, June 02, 2014 3:14 PM
' Saved dates...
'
' When restored on an en-GB system, the example displays the following output:
' Current Time Zone: (UTC-6:00) Central Time (US & Canada)
' The dates on an en-GB system:
' 14 June 2014 08:32
' 11 July 2014 01:49
' 10 January 2015 03:16
' 20 December 2014 11:45
' 02 June 2014 17:14
' Restored dates...
' </Snippet5>
' <Snippet7>
Public Sub SaveDateWithTimeZone()
Dim dates As DateWithTimeZone() = {New DateWithTimeZone(#8/9/2014 7:30PM#,
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")),
New DateWithTimeZone(#8/15/2014 7:00PM#,
TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time")),
New DateWithTimeZone(#8/22/2014 7:30PM#,
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")),
New DateWithTimeZone(#8/28/2014 7:00PM#,
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"))}
Dim fs As New FileStream(".\Schedule.bin", FileMode.Create)
Dim formatter As New BinaryFormatter()
Try
formatter.Serialize(fs, dates)
Catch e As SerializationException
Console.WriteLine($"Serialization failed. Reason: {e.Message}")
Finally
If fs IsNot Nothing Then fs.Close()
End Try
' Display dates.
For Each dateInfo In dates
Dim tz As TimeZoneInfo = dateInfo.TimeZone
Console.WriteLine($"{dateInfo.DateTime} {If(tz.IsDaylightSavingTime(dateInfo.DateTime), tz.DaylightName, tz.StandardName)}")
Next
End Sub
' The example displays the following output:
' 8/9/2014 7:30:00 PM Eastern Daylight Time
' 8/15/2014 7:00:00 PM Pacific Daylight Time
' 8/22/2014 7:30:00 PM Eastern Daylight Time
' 8/28/2014 7:00:00 PM Eastern Daylight Time
' </Snippet7>
Private Const filename As String = ".\Schedule.bin"
' <Snippet8>
Public Sub RestoreDateWithTimeZone()
Dim fs As FileStream
If File.Exists(filename) Then
fs = New FileStream(filename, FileMode.Open)
Else
Console.WriteLine("Unable to find file to deserialize.")
Exit Sub
End If
Dim formatter As New BinaryFormatter()
Dim dates As DateWithTimeZone ()= Nothing
Try
dates = DirectCast(formatter.Deserialize(fs), DateWithTimeZone())
' Display dates.
For Each dateInfo In dates
Dim tz As TimeZoneInfo = dateInfo.TimeZone
Console.WriteLine($"{dateInfo.DateTime} {If(tz.IsDaylightSavingTime(dateInfo.DateTime), tz.DaylightName, tz.StandardName)}")
Next
Catch e As SerializationException
Console.WriteLine("Deserialization failed. Reason: {e.Message}")
Finally
If fs IsNot Nothing Then fs.Close()
End Try
End Sub
' The example displays the following output:
' 8/9/2014 7:30:00 PM Eastern Daylight Time
' 8/15/2014 7:00:00 PM Pacific Daylight Time
' 8/22/2014 7:30:00 PM Eastern Daylight Time
' 8/28/2014 7:00:00 PM Eastern Daylight Time
' </Snippet8>
End Module