diff --git a/src/MiniExcel.OpenXml/Utils/OpenXmlNumberFormatHelper.cs b/src/MiniExcel.OpenXml/Utils/OpenXmlNumberFormatHelper.cs index 800b6afd..df3e5180 100644 --- a/src/MiniExcel.OpenXml/Utils/OpenXmlNumberFormatHelper.cs +++ b/src/MiniExcel.OpenXml/Utils/OpenXmlNumberFormatHelper.cs @@ -791,7 +791,10 @@ private static void ParseMilliseconds(List tokens, out List resu reader.ReadOneOrMore('s') || reader.ReadOneOrMore('S') || reader.ReadOneOrMore('g') || - reader.ReadOneOrMore('G')) + reader.ReadOneOrMore('G') || + reader.ReadOneOrMore('e') || + reader.ReadOneOrMore('E') || + ReadDayOfWeek(reader)) { syntaxError = false; var length = reader.Position - offset; @@ -802,6 +805,20 @@ private static void ParseMilliseconds(List tokens, out List resu return null; } + private static bool ReadDayOfWeek(Tokenizer reader) + { + // aaa and aaaa are the day of the week; a single a or aa is not a format code + var count = 0; + while (reader.Peek(count) is 'a' or 'A') + count++; + + if (count < 3) + return false; + + reader.Advance(count); + return true; + } + private static bool ReadLiteral(Tokenizer reader) { if (reader.Peek() == '\\' || reader.Peek() == '*' || reader.Peek() == '_') @@ -987,6 +1004,8 @@ public static bool IsDatePart(string token) => token.StartsWith("s", StringComparison.OrdinalIgnoreCase) || token.StartsWith("h", StringComparison.OrdinalIgnoreCase) || (token.StartsWith("g", StringComparison.OrdinalIgnoreCase) && !IsGeneral(token)) || + (token.StartsWith("e", StringComparison.OrdinalIgnoreCase) && !IsExponent(token)) || + token.StartsWith("aaa", StringComparison.OrdinalIgnoreCase) || string.Compare(token, "am/pm", StringComparison.OrdinalIgnoreCase) == 0 || string.Compare(token, "a/p", StringComparison.OrdinalIgnoreCase) == 0 || IsDurationPart(token); diff --git a/tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs b/tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs index dd886318..6187f8bd 100644 --- a/tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs +++ b/tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs @@ -1533,6 +1533,53 @@ public void TestIssue268_DateFormat() Assert.False(IsDateFormatString("General")); Assert.False(IsDateFormatString(@"_-* #,##0\ _P_t_s_-;\-* #,##0\ _P_t_s_-;_-* "" - ""??\ _P_t_s_-;_-@_- ")); } + [Fact] + public void TestDateFormatWithWeekdayOrEraYear() + { + // aaa and aaaa are the short and long day of the week, e is the year of the era + Assert.True(IsDateFormatString("aaa")); + Assert.True(IsDateFormatString("aaaa")); + Assert.True(IsDateFormatString("AAAA")); + Assert.True(IsDateFormatString("m/d (aaa)")); + Assert.True(IsDateFormatString("yyyy/m/d aaaa h:mm")); + Assert.True(IsDateFormatString("m/d (aaa);@")); + Assert.True(IsDateFormatString("e/m/d")); + Assert.True(IsDateFormatString("[$-404]e/m/d")); + Assert.True(IsDateFormatString("ee")); + Assert.True(IsDateFormatString("[$-411]ge.m.d")); + Assert.True(IsDateFormatString("[$-411]ggge\"年\"m\"月\"d\"日\"")); + Assert.False(IsDateFormatString("0.00E+00")); + Assert.False(IsDateFormatString("##0.0e-0")); + Assert.False(IsDateFormatString(@"0 \a")); + Assert.False(IsDateFormatString("0 \"aaa\"")); + } + + [Fact] + public void TestQueryDateWithWeekdayOrEraYearFormat() + { + using var workbook = new XLWorkbook(); + var sheet = workbook.AddWorksheet("Sheet1"); + string[] formats = ["m/d (aaa)", "aaaa", "e/m/d", "[$-411]ge.m.d"]; + for (var i = 0; i < formats.Length; i++) + { + var cell = sheet.Cell(i + 1, 1); + cell.Value = new DateTime(2024, 1, 15); + cell.Style.NumberFormat.Format = formats[i]; + } + + using var stream = new MemoryStream(); + workbook.SaveAs(stream); + stream.Position = 0; + + var rows = _excelImporter.Query(stream).ToList(); + Assert.Equal(formats.Length, rows.Count); + foreach (var row in rows) + { + object value = row.A; + Assert.Equal(new DateTime(2024, 1, 15), value); + } + } + /// /// Custom excel zip can't read and show Number of entries expected in End Of Central Directory does not correspond to number of entries in Central Directory. #272