Skip to content

Commit ea2bf54

Browse files
committed
Mixed case am/pm fixes. More tests. Bump version
1 parent 4f53a2f commit ea2bf54

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

src/ExcelNumberFormat/ExcelNumberFormat.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>net20;netstandard1.0;netstandard2.0</TargetFrameworks>
5-
<VersionPrefix>1.0.7</VersionPrefix>
5+
<VersionPrefix>1.0.8</VersionPrefix>
66
<GenerateDocumentationFile>true</GenerateDocumentationFile>
77
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
88
<Description>.NET library to parse ECMA-376 number format strings and format values like Excel and other spreadsheet softwares.</Description>

src/ExcelNumberFormat/Formatter.cs

+22-10
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ private static string FormatTimeSpan(TimeSpan timeSpan, List<string> tokens, Cul
129129

130130
private static string FormatDate(DateTime date, List<string> tokens, CultureInfo culture)
131131
{
132-
var containsAmPm = tokens.Contains("AM/PM") || tokens.Contains("A/P");
132+
var containsAmPm = ContainsAmPm(tokens);
133+
133134
var result = new StringBuilder();
134135
for (var i = 0; i < tokens.Count; i++)
135136
{
@@ -202,7 +203,7 @@ private static string FormatDate(DateTime date, List<string> tokens, CultureInfo
202203
{
203204
var digits = token.Length;
204205
if(containsAmPm)
205-
result.Append(date.ToString("%h"));
206+
result.Append((date.Hour % 12).ToString("D" + digits));
206207
else
207208
result.Append(date.Hour.ToString("D" + digits));
208209
}
@@ -227,14 +228,7 @@ private static string FormatDate(DateTime date, List<string> tokens, CultureInfo
227228
else if (string.Compare(token, "am/pm", StringComparison.OrdinalIgnoreCase) == 0)
228229
{
229230
var ampm = date.ToString("tt", CultureInfo.InvariantCulture);
230-
if (char.IsUpper(token[0]))
231-
{
232-
result.Append(ampm.ToUpperInvariant());
233-
}
234-
else
235-
{
236-
result.Append(ampm.ToLowerInvariant());
237-
}
231+
result.Append(ampm.ToUpperInvariant());
238232
}
239233
else if (string.Compare(token, "a/p", StringComparison.OrdinalIgnoreCase) == 0)
240234
{
@@ -299,6 +293,24 @@ private static bool LookBackDatePart(List<string> tokens, int fromIndex, string
299293
return false;
300294
}
301295

296+
private static bool ContainsAmPm(List<string> tokens)
297+
{
298+
foreach (var token in tokens)
299+
{
300+
if (string.Compare(token, "am/pm", StringComparison.OrdinalIgnoreCase) == 0)
301+
{
302+
return true;
303+
}
304+
305+
if (string.Compare(token, "a/p", StringComparison.OrdinalIgnoreCase) == 0)
306+
{
307+
return true;
308+
}
309+
}
310+
311+
return false;
312+
}
313+
302314
static string FormatNumber(double value, DecimalSection format, CultureInfo culture)
303315
{
304316
bool thousandSeparator = format.ThousandSeparator;

test/ExcelNumberFormat.Tests/Class1.cs

+6
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,14 @@ public void TestDate()
131131
Test(new DateTime(2010, 9, 26, 12, 34, 56, 123), "m/d/yy hh:mm:ss.000", "9/26/10 12:34:56.123");
132132
Test(new DateTime(2010, 9, 26, 12, 34, 56, 123), "YYYY-MM-DD HH:MM:SS", "2010-09-26 12:34:56");
133133
Test(new DateTime(2020, 1, 1, 14, 35, 55), "m/d/yyyy\\ h:mm:ss AM/PM;@", "1/1/2020 2:35:55 PM");
134+
Test(new DateTime(2020, 1, 1, 14, 35, 55), "m/d/yyyy\\ h:mm:ss aM/Pm;@", "1/1/2020 2:35:55 PM");
135+
Test(new DateTime(2020, 1, 1, 14, 35, 55), "m/d/yyyy\\ h:mm:ss am/PM;@", "1/1/2020 2:35:55 PM");
134136
Test(new DateTime(2020, 1, 1, 14, 35, 55), "m/d/yyyy\\ h:mm:ss A/P;@", "1/1/2020 2:35:55 P");
137+
Test(new DateTime(2020, 1, 1, 14, 35, 55), "m/d/yyyy\\ h:mm:ss a/P;@", "1/1/2020 2:35:55 p");
138+
Test(new DateTime(2020, 1, 1, 14, 35, 55), "m/d/yyyy\\ h:mm:ss A/p;@", "1/1/2020 2:35:55 P");
135139
Test(new DateTime(2020, 1, 1, 14, 35, 55), "m/d/yyyy\\ h:mm:ss;@", "1/1/2020 14:35:55");
140+
Test(new DateTime(2020, 1, 1, 14, 35, 55), "m/d/yyyy\\ hh:mm:ss AM/PM;@", "1/1/2020 02:35:55 PM");
141+
Test(new DateTime(2020, 1, 1, 16, 5, 6), "m/d/yyyy\\ h:m:s AM/PM;@", "1/1/2020 4:5:6 PM");
136142
}
137143

138144
[TestMethod]

0 commit comments

Comments
 (0)