Skip to content

Commit

Permalink
Merge pull request #960 from karakasa/allow_illformed_datetime_string
Browse files Browse the repository at this point in the history
Able to open files with ill-formed datetime, exported by SAS
  • Loading branch information
tonyqus authored Dec 29, 2022
2 parents b9b3f7c + 70c203d commit 56cc2f7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
12 changes: 11 additions & 1 deletion main/SS/Util/Format.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,17 @@ public override object ParseObject(string source, int pos)
}
public DateTime Parse(string source)
{
var dt = DateTime.Parse(source, CultureInfo.InvariantCulture);
if (!DateTime.TryParse(source, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dt))
{
// Compatibility stub for SAS-generated ill-formed datetime, such as 2020-07-03T 9:41:11-04:00
// There should be a leading zero before the one-digit hour.
// See https://github.com/nissl-lab/npoi/issues/846 for more information.

const string AlternativeDateTimeFormat = "yyyy-MM-ddT H:mm:sszzz";

dt = DateTime.ParseExact(source, AlternativeDateTimeFormat, CultureInfo.InvariantCulture);
}

return TimeZone != null ? TimeZoneInfo.ConvertTime(dt, TimeZone) : dt;
}

Expand Down
1 change: 1 addition & 0 deletions testcases/main/NPOI.TestCases.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@
<Compile Include="SS\Util\TestAreaReference.cs" />
<Compile Include="SS\Util\TestCellAddress.cs" />
<Compile Include="SS\Util\BaseTestCellUtil.cs" />
<Compile Include="SS\Util\TestSimpleDateFormat.cs" />
<Compile Include="SS\Util\TestDateFormatConverter.cs" />
<Compile Include="SS\Util\TestHSSFCellUtil.cs" />
<Compile Include="SS\Util\TestSheetUtil.cs" />
Expand Down
54 changes: 54 additions & 0 deletions testcases/main/SS/Util/TestSimpleDateFormat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/

using System;
using System.Globalization;
using System.IO;
using NPOI.SS.Util;
using NPOI.Util;
using NUnit.Framework;

namespace TestCases.SS.Util
{
[TestFixture]
public class TestSimpleDateFormat
{
/*
* Test SimpleDateFormat.Parse with ill-formed datetime string.
*/
[Test]
public void TestIllformedDatetimeParsing()
{
// See https://github.com/nissl-lab/npoi/issues/846 for more information.

var format = new SimpleDateFormat();

DateTime standardForm = default;
DateTime illForm = default;

Assert.DoesNotThrow(() => standardForm = format.Parse("2020-07-03T09:41:11-04:00"));
Assert.DoesNotThrow(() => illForm = format.Parse("2020-07-03T 9:41:11-04:00"));
Assert.Throws<FormatException>(() => format.Parse("2020-07-03T09: 1:11-04:00"));

Assert.AreEqual(standardForm, illForm);
}

}

}

0 comments on commit 56cc2f7

Please # to comment.