using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; namespace libxmltv { public static class DateTimeStringConverter { private const string DEFAULT_DATE_FORMAT = "yyyy/MM/dd hh:mm tt"; public static string ToDateTimeString(this DateTime dt) { return dt.ToDateTimeString(DateTimeStringConverter.DEFAULT_DATE_FORMAT); } public static string ToDateTimeString(this DateTime dt, IFormatProvider provider) { return dt.ToString(provider); } public static string ToDateTimeString(this DateTime dt, string format) { return dt.ToString(format); } public static string ToDateTimeString(this DateTime dt, string format, IFormatProvider provider) { return dt.ToString(format, provider); } } public static class extensions { public static IEnumerable> Chunk(this IEnumerable source, int chunkSize) { // Validate parameters. if (source == null) throw new ArgumentNullException("source"); if (chunkSize <= 0) throw new ArgumentOutOfRangeException("chunkSize", "The chunkSize parameter must be a positive value."); // Call the internal implementation. return source.ChunkInternal(chunkSize); } private static IEnumerable> ChunkInternal(this IEnumerable source, int chunkSize) { // Validate parameters. Debug.Assert(source != null); Debug.Assert(chunkSize > 0); // Get the enumerator. Dispose of when done. using (IEnumerator enumerator = source.GetEnumerator()) do { // Move to the next element. If there's nothing left // then get out. if (!enumerator.MoveNext()) yield break; // Return the chunked sequence. yield return ChunkSequence(enumerator, chunkSize); } while (true); } private static IEnumerable ChunkSequence(IEnumerator enumerator, int chunkSize) { // Validate parameters. Debug.Assert(enumerator != null); Debug.Assert(chunkSize > 0); // The count. int count = 0; // There is at least one item. Yield and then continue. do { // Yield the item. yield return enumerator.Current; } while (++count < chunkSize && enumerator.MoveNext()); } } }