From bd93f984606051321953ecb1bf7c5f67d3f9e954 Mon Sep 17 00:00:00 2001 From: Daniel Schick Date: Fri, 9 Dec 2022 09:07:15 +0100 Subject: [PATCH] =?UTF-8?q?neue=20Datumsformate=20(DE=20+=20EN)=20hinzugef?= =?UTF-8?q?=C3=BCgt=20inkl.=20kurze=20Monatsnamen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ENI2/Excel/ExcelReader.cs | 40 +++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) 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; }