From 7be71f99075e9951cef0f1a31c8eec121150baed Mon Sep 17 00:00:00 2001 From: tschumpr Date: Tue, 30 Apr 2024 08:24:55 +0200 Subject: [PATCH] Use tryParse to extract dates and years --- Geodatenbezug/Processors/GdalLayer.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Geodatenbezug/Processors/GdalLayer.cs b/Geodatenbezug/Processors/GdalLayer.cs index 640bb72..e0c59b1 100644 --- a/Geodatenbezug/Processors/GdalLayer.cs +++ b/Geodatenbezug/Processors/GdalLayer.cs @@ -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 {