Import-DbaCsv - Fix month/day swap when using -Culture with ambiguous dates#10340
Import-DbaCsv - Fix month/day swap when using -Culture with ambiguous dates#10340andreasjordan wants to merge 4 commits intodevelopmentfrom
Conversation
… dates (issue #10338) When -Culture is specified without -DateTimeFormats, derive datetime format strings from the culture's DateTimeFormat patterns (ShortDatePattern + LongTimePattern, etc.) and apply them explicitly. This forces ParseExact (unambiguous field order) instead of Parse (which defaults to MM.dd order, swapping day/month when both values <= 12). (do Import-DbaCsv) Co-authored-by: Andreas Jordan <andreasjordan@users.noreply.github.com>
|
@claude test failes: |
|
Claude finished @andreasjordan's task in 28m 6s —— View job Fixed:
|
… (issue #10338) When DateTimeFormats are auto-derived from Culture, do not set Culture on csvOptions. The library bypasses DateTimeFormats when Culture is also set, falling back to a broken DateTime.Parse path. Omitting Culture forces ParseExact with the derived format strings, correctly preserving dd/MM order. (do Import-DbaCsv) Co-authored-by: Andreas Jordan <andreasjordan@users.noreply.github.com>
|
@claude Tests still fail. |
|
Claude finished @andreasjordan's task in 24m 38s —— View job Fixed: Restore number parsing + stop LongDate format exceptions (issue dataplat/dbatools.library#48)
Root cause analysis: Test 1 (date test — "Expected 2, but got $null") The derived format list included Test 2 (number test — "Expected 789.12, but got 78912.00") The previous fix removed Fix applied (
|
… (issue #10338) Two issues with the previous fix: 1. The derived format list included LongDatePattern strings (e.g. "dddd, d. MMMM yyyy HH:mm:ss") that contain locale-specific tokens (MMMM, dddd). The CsvDataReader library, when iterating these formats, throws on locale-specific patterns and immediately falls back to DateTime.Parse without a Culture set -- causing the import to fail entirely (null result). 2. Removing Culture from csvOptions broke the existing number-parsing behavior (comma as decimal separator for de-DE/de-CH etc.). Fix: use only short purely-numeric patterns derived from ShortDatePattern (e.g. "dd.MM.yyyy HH:mm:ss", "dd.MM.yyyy HH:mm", "dd.MM.yyyy"). These never cause format exceptions, so the library uses ParseExact cleanly and returns the correct day/month order. Also restores Culture on csvOptions so number parsing continues to work correctly. (do Import-DbaCsv) Co-authored-by: Andreas Jordan <andreasjordan@users.noreply.github.com>
|
Ok, Claude can not solve this. But I don't have time until next weekend. So this will sit here for some days... |
|
@claude can you read the source code for the CsvDataReader class at https://github.com/dataplat/dbatools.library/blob/main/project/dbatools/Csv/Reader/CsvDataReader.cs ? I think this a bug in the library itself. |
|
Claude finished @andreasjordan's task in 8m 8s —— View job Analysis: CsvDataReader Culture + DateTimeFormats Bug
Root Cause (library bug confirmed by behavior)You're right — this is a bug in how the library interacts with .NET's Issue 1: Unescaped
|
Root cause: DateTime.ParseExact treats unescaped ':' in format strings as a placeholder for the culture's TimeSeparator. On Linux with ICU, de-CH has TimeSeparator='.' so 'HH:mm:ss' would expect '17.09.41' but CSV data uses '17:09:41', causing a CSV parse error. Also avoided using $dtf.ShortDatePattern/$dtf.LongTimePattern directly as these differ between Windows (NLS) and Linux (ICU). Instead derive from stable primitives: DateSeparator + date-order from ShortDatePattern. Fix: escape time colons as HH':'mm':'ss for literal ':' matching, and construct date pattern from DateSeparator + detected day/month order. (do Import-DbaCsv) Co-authored-by: Andreas Jordan <andreasjordan@users.noreply.github.com>
|
@potatoqualitee Claude can not help here and my local tests also fail. I think this is a bug in the library. Can you have a look at this? |
Fixes dataplat/dbatools.library#48
Root Cause
When
-Cultureis specified without-DateTimeFormats, the underlying CsvDataReader library doesn't apply the culture's date format order during DateTime parsing. It falls back toDateTime.Parsewithout an explicit format, which defaults to MM.dd order - swapping day and month when both values are ≤ 12.Fix
When
-Cultureis specified but-DateTimeFormatsis not, automatically derive unambiguous datetime format strings from the culture's DateTimeFormat patterns. For de-CH this produces"dd.MM.yyyy HH:mm:ss","dd.MM.yyyy HH:mm","dd.MM.yyyy", etc.This forces ParseExact (which respects field order) instead of Parse (which guesses and defaults to month-first).
Generated with Claude Code