Skip to content

Commit

Permalink
Use tryParse to extract dates and years
Browse files Browse the repository at this point in the history
  • Loading branch information
tschumpr committed Apr 30, 2024
1 parent c193c62 commit 7be71f9
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions Geodatenbezug/Processors/GdalLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,14 @@ public void CopyFeatures()
}
else if (fieldType == FieldType.OFTDateTime)
{
var dateTimeValues = inputFeature.GetFieldAsString(fieldName).Split("-");
var year = int.Parse(dateTimeValues[0], CultureInfo.InvariantCulture);
var month = dateTimeValues.Length > 1 ? int.Parse(dateTimeValues[1], CultureInfo.InvariantCulture) : 1;
var day = dateTimeValues.Length > 2 ? int.Parse(dateTimeValues[2], CultureInfo.InvariantCulture) : 1;
newFeature.SetField(fieldName, year, month, day, 0, 0, 0, 0);
var inputValue = inputFeature.GetFieldAsString(fieldName);
if (!DateTime.TryParse(inputValue, out var inputDate))
{
var inputYear = int.Parse(inputValue, CultureInfo.InvariantCulture);
inputDate = new DateTime(inputYear, 1, 1);
}

newFeature.SetField(fieldName, inputDate.Year, inputDate.Month, inputDate.Day, 0, 0, 0, 0);
}
else
{
Expand Down

0 comments on commit 7be71f9

Please sign in to comment.