public static void GetFirstAndLastFileDates(string fileName, out DateTime? dateFirst, out DateTime? dateLast, out bool endsUpNewLine) { dateFirst = null; dateLast = null; endsUpNewLine = false; if (!File.Exists(fileName)) return; using (var sr = new StreamReaderLog(fileName)) { // получить первую строку while (!sr.EndOfStream) { var line = sr.ReadLine(); if (string.IsNullOrEmpty(line)) continue; try { if (!dateFirst.HasValue) dateFirst = DateTime.ParseExact(line, "ddMMyyyy", CultureProvider.Common); else { var parts = line.Split(' '); var nH = int.Parse(parts[0].Substring(0, 2)); var nM = int.Parse(parts[0].Substring(2)); dateFirst = dateFirst.Value.AddMinutes(nM + nH * 60); break; } } catch { return; } } if (sr.EndOfStream) return; // получить последнюю строку var strLast = ReadEndTokens(sr.BaseStream, 1500, Encoding.ASCII, "\n"); if (!string.IsNullOrEmpty(strLast)) if (strLast[strLast.Length - 1] == '\n') endsUpNewLine = true; var quoteLines = strLast.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); int? lastHour = null, lastMin = null; for (var i = quoteLines.Length - 1; i >= 0; i--) { var quoteLineParts = quoteLines[i].Split(' '); if (quoteLineParts.Length == 3 && lastHour.HasValue == false) { lastHour = int.Parse(quoteLineParts[0].Substring(0, 2)); lastMin = int.Parse(quoteLineParts[0].Substring(2)); continue; } if (quoteLineParts.Length == 1) { dateLast = DateTime.ParseExact(quoteLines[i], "ddMMyyyy", CultureProvider.Common); if (lastHour.HasValue) dateLast = dateLast.Value.AddMinutes(lastMin.Value + lastHour.Value * 60); break; } } } }
public static void GetFirstAndLastFileDates(string fileName, out DateTime?dateFirst, out DateTime?dateLast, out bool endsUpNewLine) { dateFirst = null; dateLast = null; endsUpNewLine = false; if (!File.Exists(fileName)) { return; } using (var sr = new StreamReaderLog(fileName)) { // получить первую строку while (!sr.EndOfStream) { var line = sr.ReadLine(); if (string.IsNullOrEmpty(line)) { continue; } try { if (!dateFirst.HasValue) { DateTime lineDate; if (!DateTime.TryParseExact(line, "ddMMyyyy", CultureProvider.Common, DateTimeStyles.None, out lineDate)) { Logger.DebugFormat("Файл котировок \"{0}\" - начало повреждено", fileName); continue; } dateFirst = lineDate; } else { var parts = line.Split(' '); var nH = int.Parse(parts[0].Substring(0, 2)); var nM = int.Parse(parts[0].Substring(2)); dateFirst = dateFirst.Value.AddMinutes(nM + nH * 60); break; } } catch { return; } } if (sr.EndOfStream) { return; } // получить последнюю строку var strLast = ReadEndTokens(sr.BaseStream, 1500, Encoding.ASCII, "\n"); if (!string.IsNullOrEmpty(strLast)) { if (strLast[strLast.Length - 1] == '\n') { endsUpNewLine = true; } } var quoteLines = strLast.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); int?lastHour = null, lastMin = null; for (var i = quoteLines.Length - 1; i >= 0; i--) { var quoteLineParts = quoteLines[i].Split(' '); if (quoteLineParts.Length == 3 && lastHour.HasValue == false) { lastHour = int.Parse(quoteLineParts[0].Substring(0, 2)); lastMin = int.Parse(quoteLineParts[0].Substring(2)); continue; } if (quoteLineParts.Length == 1) { DateTime lineDate; if (!DateTime.TryParseExact(quoteLines[i], "ddMMyyyy", CultureProvider.Common, DateTimeStyles.None, out lineDate)) { continue; } dateLast = lineDate; if (lastHour.HasValue) { dateLast = dateLast.Value.AddMinutes(lastMin.Value + lastHour.Value * 60); } break; } } } }
public static DateTime? GetFirstDate(string fileName) { if (!File.Exists(fileName)) return null; DateTime? dateFirst = null; using (var sr = new StreamReaderLog(fileName)) { // получить первую строку var linesToTryParseMax = 10; while (!sr.EndOfStream) { linesToTryParseMax--; if (linesToTryParseMax < 0) break; var line = sr.ReadLine(); if (string.IsNullOrEmpty(line)) continue; if (line.Length == "ddMMyyyy".Length) { DateTime lineDate; if (DateTime.TryParseExact(line, "ddMMyyyy", CultureProvider.Common, DateTimeStyles.None, out lineDate)) dateFirst = lineDate; continue; } if (dateFirst == null) continue; try { var parts = line.Split(' '); if (parts.Length != 3) continue; var hour = parts[0].Substring(0, 2).ToInt(); var minute = parts[0].Substring(2).ToInt(); dateFirst = dateFirst.Value.AddMinutes(hour*60 + minute); return dateFirst; } catch { } } return dateFirst; } }
public static DateTime?GetFirstDate(string fileName) { if (!File.Exists(fileName)) { return(null); } DateTime?dateFirst = null; using (var sr = new StreamReaderLog(fileName)) { // получить первую строку var linesToTryParseMax = 10; while (!sr.EndOfStream) { linesToTryParseMax--; if (linesToTryParseMax < 0) { break; } var line = sr.ReadLine(); if (string.IsNullOrEmpty(line)) { continue; } if (line.Length == "ddMMyyyy".Length) { DateTime lineDate; if (DateTime.TryParseExact(line, "ddMMyyyy", CultureProvider.Common, DateTimeStyles.None, out lineDate)) { dateFirst = lineDate; } continue; } if (dateFirst == null) { continue; } try { var parts = line.Split(' '); if (parts.Length != 3) { continue; } var hour = parts[0].Substring(0, 2).ToInt(); var minute = parts[0].Substring(2).ToInt(); dateFirst = dateFirst.Value.AddMinutes(hour * 60 + minute); return(dateFirst); } catch { } } return(dateFirst); } }