Skip to content

Commit

Permalink
Merge pull request #1456 from antony-liu/poi/v3.16-bug-59227
Browse files Browse the repository at this point in the history
bug 59227: parse dates formatted in Japanese or Chinese.
  • Loading branch information
tonyqus authored Dec 8, 2024
2 parents 4fd6908 + 2ff8768 commit bcdb67f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
10 changes: 9 additions & 1 deletion main/SS/UserModel/DateUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,15 @@ public class DateUtil
private static Regex date_ptrn1 = new Regex("^\\[\\$\\-.*?\\]", RegexOptions.Compiled);
private static Regex date_ptrn2 = new Regex("^\\[[a-zA-Z]+\\]", RegexOptions.Compiled);
private static Regex date_ptrn3a = new Regex("[yYmMdDhHsS]", RegexOptions.Compiled);
private static Regex date_ptrn3b = new Regex("^[\\[\\]yYmMdDhHsS\\-T/,. :\"\\\\]+0*[ampAMP/]*$", RegexOptions.Compiled);
// add "\u5e74 \u6708 \u65e5"£¨ÄêÔÂÈÕ£© for Chinese/Japanese date format:2017Äê2ÔÂ7ÈÕ
private static Regex date_ptrn3b = new Regex("^[\\[\\]yYmMdDhHsS\\-T/\u5e74\u6708\u65e5,. :\"\\\\]+0*[ampAMP/]*$", RegexOptions.Compiled);

// elapsed time patterns: [h],[m] and [s]
//private static Regex date_ptrn4 = new Regex("^\\[([hH]+|[mM]+|[sS]+)\\]", RegexOptions.Compiled);
private static Regex date_ptrn4 = new Regex("^\\[([hH]+|[mM]+|[sS]+)\\]$", RegexOptions.Compiled);

// for format which start with "[DBNum1]" or "[DBNum2]" or "[DBNum3]" could be a Chinese date
private static Regex date_ptrn5 = new Regex("^\\[DBNum(1|2|3)\\]", RegexOptions.Compiled);

/// <summary>
/// Given a Calendar, return the number of days since 1899/12/31.
Expand Down Expand Up @@ -672,6 +676,10 @@ public static bool IsADateFormat(int formatIndex, String formatString)
return true;
}

// If it starts with [DBNum1] or [DBNum2] or [DBNum3]
// then it could be a Chinese date
fs = date_ptrn5.Replace(fs, "");

// If it starts with [$-...], then could be a date, but
// who knows what that starting bit Is all about
//fs = Regex.Replace(fs, "^\\[\\$\\-.*?\\]", "");
Expand Down
31 changes: 31 additions & 0 deletions testcases/main/SS/UserModel/TestDateUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,36 @@ public void GetJavaCalendar_ValidValue()
Assert.AreEqual(expCal, actCal[2]);
Assert.AreEqual(expCal, actCal[3]);
}

[Test]
public void IsADateFormat()
{
// Cell content 2016-12-8 as an example
// Cell show "12/8/2016"
Assert.IsTrue(DateUtil.IsADateFormat(14, "m/d/yy"));
// Cell show "Thursday, December 8, 2016"
Assert.IsTrue(DateUtil.IsADateFormat(182, "[$-F800]dddd\\,\\ mmmm\\ dd\\,\\ yyyy"));
// Cell show "12/8"
Assert.IsTrue(DateUtil.IsADateFormat(183, "m/d;@"));
// Cell show "12/08/16"
Assert.IsTrue(DateUtil.IsADateFormat(184, "mm/dd/yy;@"));
// Cell show "8-Dec-16"
Assert.IsTrue(DateUtil.IsADateFormat(185, "[$-409]d\\-mmm\\-yy;@"));
// Cell show "D-16"
Assert.IsTrue(DateUtil.IsADateFormat(186, "[$-409]mmmmm\\-yy;@"));

// Cell show "2016年12月8日"
Assert.IsTrue(DateUtil.IsADateFormat(165, "yyyy\"\"m\"\"d\"\";@"));
// Cell show "2016年12月"
Assert.IsTrue(DateUtil.IsADateFormat(164, "yyyy\"\"m\"\";@"));
// Cell show "12月8日"
Assert.IsTrue(DateUtil.IsADateFormat(168, "m\"\"d\"\";@"));
// Cell show "十二月八日"
Assert.IsTrue(DateUtil.IsADateFormat(181, "[DBNum1][$-404]m\"\"d\"\";@"));
// Cell show "贰零壹陆年壹拾贰月捌日"
Assert.IsTrue(DateUtil.IsADateFormat(177, "[DBNum2][$-804]yyyy\"\"m\"\"d\"\";@"));
// Cell show "2016年12月8日"
Assert.IsTrue(DateUtil.IsADateFormat(178, "[DBNum3][$-804]yyyy\"\"m\"\"d\"\";@"));
}
}
}

0 comments on commit bcdb67f

Please sign in to comment.