MySQLite
MySQLite copied to clipboard
DateTime object parsing
The following code works for me:
$Date = Get-Date '01/01/2000'
$StateEntry = [PSCustomObject]@{
ID = (New-Guid).Guid
Date = $Date
}
$DbPath = "$env:TEMP\state.db"
[System.GC]::Collect()
if (Test-Path -Path $DbPath) {
Remove-Item -Path $DbPath -Force -ErrorAction SilentlyContinue
}
$StateEntry | ConvertTo-MySQLiteDB -Path $DbPath -TableName State -TypeName State -Primary ID -Force
[System.GC]::Collect()
ConvertFrom-MySQLiteDB -Path $DbPath -TableName State -PropertyTable propertymap_State
However this does not work in the UK when the date is set to 29/02/2024; the raw table text contains the date in MM/DD/YYYY format, which fails to parse back to DateTime.
This is because of how PowerShell converts DateTime to String. When you use .ToString() it uses the current culture settings. When using string interpolation, it uses the default invariant culture.
> (Get-Date).ToString()
29/02/2024 00:00:00
> "$(Get-Date)"
02/29/2024 00:00:00
To fix this, you'd either need to store the strings in the local culture format (by calling .ToString() on them), or decode them differently:
$dateString = '29/02/2024 00:00:00'
$format = 'dd/MM/yyyy HH:mm:ss'
$culture = [System.Globalization.CultureInfo]::InvariantCulture
$date = [DateTime]::ParseExact($dateString, $format, $culture)