SevenZipExtractor icon indicating copy to clipboard operation
SevenZipExtractor copied to clipboard

DateTime parsing error when the system has customized datetime format

Open strawhatboy opened this issue 1 year ago • 2 comments

Env

Win11pro 23H2 dotnet8

How to reproduce

  1. Change system date time format: Control Panel-> Region-> Additional Settings-> "Date" tab-> Short date.
  2. Change the "Short date" from "M/d/yyyy" to "M/d/yyyy/dddd" which will append the dayofweek, like "10/25/2024/Friday"
  3. Try to extract something by calling ArchiveFile.Extract

Expected

Extracted successfully.

Actual

System.FormatException was thrown

System.FormatException: String '' was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(ReadOnlySpan`1 s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at System.Convert.ToDateTime(String value, IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at SevenZipExtractor.ArchiveFile.GetProperty[T](UInt32 fileIndex, ItemPropId name) in C:\Users\Andy\lab\ztmz_pacenote\src\SevenZipExtractor\SevenZipExtractor\ArchiveFile.cs:line 249
   at SevenZipExtractor.ArchiveFile.GetPropertySafe[T](UInt32 fileIndex, ItemPropId name) in C:\Users\Andy\lab\ztmz_pacenote\src\SevenZipExtractor\SevenZipExtractor\ArchiveFile.cs:line 213
   at SevenZipExtractor.ArchiveFile.get_Entries() in C:\Users\Andy\lab\ztmz_pacenote\src\SevenZipExtractor\SevenZipExtractor\ArchiveFile.cs:line 174
   at SevenZipExtractor.ArchiveFile.Extract(Func`2 getOutputPath) in C:\Users\Andy\lab\ztmz_pacenote\src\SevenZipExtractor\SevenZipExtractor\ArchiveFile.cs:line 104
   at SevenZipExtractor.ArchiveFile.Extract(String outputFolder, Boolean overwrite) in C:\Users\Andy\lab\ztmz_pacenote\src\SevenZipExtractor\SevenZipExtractor\ArchiveFile.cs:line 80

because in T result = (T)Convert.ChangeType(value.ToString(), underlyingType);, value.ToString() will output "10/25/2024/Friday 18:00:00", while this format cannot be parsed to DateTime by default. thus in method private T GetPropertySafe<T>(uint fileIndex, ItemPropId name), only InvalidCastException was handled, but FormatException was not. it's not safe now XD.

strawhatboy avatar Oct 25 '24 11:10 strawhatboy