Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/MiniExcel.OpenXml/Utils/OpenXmlNumberFormatHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,10 @@ private static void ParseMilliseconds(List<string> tokens, out List<string> 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;
Expand All @@ -802,6 +805,20 @@ private static void ParseMilliseconds(List<string> tokens, out List<string> 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() == '_')
Expand Down Expand Up @@ -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);
Expand Down
47 changes: 47 additions & 0 deletions tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<object>(new DateTime(2024, 1, 15), value);
}
}


/// <summary>
/// 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
Expand Down
Loading