1 |
william |
141 |
using System; |
2 |
|
|
using System.Collections.Generic; |
3 |
|
|
using System.Linq; |
4 |
|
|
using System.Text; |
5 |
william |
197 |
using System.Diagnostics; |
6 |
william |
141 |
|
7 |
|
|
namespace libxmltv |
8 |
|
|
{ |
9 |
william |
159 |
public static class DateTimeStringConverter |
10 |
william |
141 |
{ |
11 |
|
|
private const string DEFAULT_DATE_FORMAT = "yyyy/MM/dd hh:mm tt"; |
12 |
|
|
public static string ToDateTimeString(this DateTime dt) |
13 |
|
|
{ |
14 |
|
|
return dt.ToDateTimeString(DateTimeStringConverter.DEFAULT_DATE_FORMAT); |
15 |
|
|
} |
16 |
|
|
public static string ToDateTimeString(this DateTime dt, IFormatProvider provider) |
17 |
|
|
{ |
18 |
|
|
return dt.ToString(provider); |
19 |
|
|
} |
20 |
|
|
public static string ToDateTimeString(this DateTime dt, string format) |
21 |
|
|
{ |
22 |
|
|
return dt.ToString(format); |
23 |
|
|
} |
24 |
|
|
public static string ToDateTimeString(this DateTime dt, string format, IFormatProvider provider) |
25 |
|
|
{ |
26 |
|
|
return dt.ToString(format, provider); |
27 |
|
|
} |
28 |
|
|
} |
29 |
|
|
|
30 |
william |
196 |
public static class extensions |
31 |
|
|
{ |
32 |
william |
197 |
public static IEnumerable<IEnumerable<T>> Chunk<T>(this IEnumerable<T> source, |
33 |
|
|
int chunkSize) |
34 |
william |
196 |
{ |
35 |
william |
197 |
// Validate parameters. |
36 |
|
|
if (source == null) throw new ArgumentNullException("source"); |
37 |
|
|
if (chunkSize <= 0) throw new ArgumentOutOfRangeException("chunkSize", |
38 |
|
|
"The chunkSize parameter must be a positive value."); |
39 |
|
|
|
40 |
|
|
// Call the internal implementation. |
41 |
|
|
return source.ChunkInternal(chunkSize); |
42 |
|
|
} |
43 |
|
|
private static IEnumerable<IEnumerable<T>> ChunkInternal<T>(this IEnumerable<T> source, int chunkSize) |
44 |
|
|
{ |
45 |
|
|
// Validate parameters. |
46 |
|
|
Debug.Assert(source != null); |
47 |
|
|
Debug.Assert(chunkSize > 0); |
48 |
|
|
|
49 |
|
|
// Get the enumerator. Dispose of when done. |
50 |
|
|
using (IEnumerator<T> enumerator = source.GetEnumerator()) |
51 |
|
|
do |
52 |
|
|
{ |
53 |
|
|
// Move to the next element. If there's nothing left |
54 |
|
|
// then get out. |
55 |
|
|
if (!enumerator.MoveNext()) yield break; |
56 |
|
|
|
57 |
|
|
// Return the chunked sequence. |
58 |
|
|
yield return ChunkSequence(enumerator, chunkSize); |
59 |
|
|
} while (true); |
60 |
|
|
} |
61 |
|
|
private static IEnumerable<T> ChunkSequence<T>(IEnumerator<T> enumerator, int chunkSize) |
62 |
|
|
{ |
63 |
|
|
// Validate parameters. |
64 |
|
|
Debug.Assert(enumerator != null); |
65 |
|
|
Debug.Assert(chunkSize > 0); |
66 |
|
|
|
67 |
|
|
// The count. |
68 |
|
|
int count = 0; |
69 |
|
|
|
70 |
|
|
// There is at least one item. Yield and then continue. |
71 |
|
|
do |
72 |
william |
196 |
{ |
73 |
william |
197 |
// Yield the item. |
74 |
|
|
yield return enumerator.Current; |
75 |
|
|
} while (++count < chunkSize && enumerator.MoveNext()); |
76 |
william |
196 |
} |
77 |
|
|
} |
78 |
william |
141 |
} |