diff --git a/ENI2/Excel/ExcelReader.cs b/ENI2/Excel/ExcelReader.cs index 55b7ed09..cfe44970 100644 --- a/ENI2/Excel/ExcelReader.cs +++ b/ENI2/Excel/ExcelReader.cs @@ -420,7 +420,7 @@ namespace ENI2.Excel { var val = _nameDict[lookup].RefersToRange.Value; if (val is DateTime) - { + { date = val; } else if (val is double) @@ -446,22 +446,37 @@ namespace ENI2.Excel date = tmpDate; } + if (date == null) + { + string[] formats = { "d/M/yyyy", "dd/M/yyyy", "d/MM/yyyy", "dd/MM/yyyy", "d/MMM/yyyy", "dd/MMM/yyyy" }; + if (DateTime.TryParseExact(val, formats, NumberFormatInfo.InvariantInfo, DateTimeStyles.None, out DateTime tmpDate)) + date = tmpDate; + } + + if (date == null) + { + CultureInfo en = CultureInfo.CreateSpecificCulture("en-US"); + string[] formats = { "d/M/yyyy", "dd/M/yyyy", "d/MM/yyyy", "dd/MM/yyyy", "d/MMM/yyyy", "dd/MMM/yyyy" }; + if (DateTime.TryParseExact(val, formats, en, DateTimeStyles.None, out DateTime tmpDate)) + return tmpDate; + } + if (date != null) { if ((date.Value < new DateTime(1899, 1, 1)) || (date.Value > new DateTime(2130, 1, 1))) { date = null; // this can't be right - } + } } } return date; } catch (Exception) - { + { _log.WarnFormat("error parsing datetime for lookup {0}", lookup); return null; - } + } } internal DateTime? ReadDateTime(string dateField, string timeField) @@ -690,19 +705,16 @@ namespace ENI2.Excel string dateString = ReadCellAsText(sheetName, range); if (dateString != null) { - CultureInfo provider = CultureInfo.InvariantCulture; - const string dateTimeFormat = "yyyy/MM/dd HH:mm"; - if (DateTime.TryParseExact(dateString, dateTimeFormat, provider, DateTimeStyles.None, out DateTime tmpDate)) - return tmpDate.ToUniversalTime(); - const string dateFormat = "yyyy/MM/dd"; - if (DateTime.TryParseExact(dateString, dateFormat, provider, DateTimeStyles.None, out DateTime tmpDate2)) + string[] formats = { "yyyy/MM/dd HH:mm", "yyyy/MM/dd", "dd.MM.yyyy", "d/M/yyyy", "dd/M/yyyy", "d/MM/yyyy", "dd/MM/yyyy", "d/MMM/yyyy", "dd/MMM/yyyy" }; + if (DateTime.TryParseExact(dateString, formats, NumberFormatInfo.InvariantInfo, DateTimeStyles.None, out DateTime tmpDate)) + return tmpDate; + CultureInfo en = CultureInfo.CreateSpecificCulture("en-US"); + if (DateTime.TryParseExact(dateString, formats, en, DateTimeStyles.None, out DateTime tmpDate2)) return tmpDate2; - const string dateFormat2 = "dd.MM.yyyy"; - if (DateTime.TryParseExact(dateString, dateFormat2, provider, DateTimeStyles.None, out DateTime tmpDate3)) - return tmpDate3; return null; } - else return null; + else + return null; }