Skip to content

DbValue.GetDateTime throws a FormatException when converting datetime string to a DateTime value. #1173

Open
@deAtog

Description

@deAtog

Under certain circumstances, the call to Convert.ToDateTime in this method will throw a FormatException when attempting to retrieve a DateTime value inserted into a text column:

public DateTime GetDateTime()
{
return _value switch
{
DateTimeOffset dto => dto.DateTime,
FbZonedDateTime zdt => zdt.DateTime,
_ => Convert.ToDateTime(_value, CultureInfo.CurrentCulture.DateTimeFormat),
};
}

Steps to reproduce this issue are as follows:

  1. Create at table with a text column.
  2. Set the current locale to the invariant culture using: CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
  3. Insert a DateTime value with the date of "2024-02-29 00:00:00" into the column using a parameterized query with a DateTime parameter.
  4. Change the locale to ar-SA using: CultureInfo.CurrentCulture = CultureInfo.GetCulture("ar-SA");
  5. Select the text value from the table without casting the value in the query to date/time type.
  6. Use FbDataReader.GetDateTime() to retrieve the value and observe the exception.

A format exception is generated because the call to Convert.ToDateTime uses the current culture of the calling application and does not correctly parse the value that was stored in the database. This method should either attempt a direct cast to a DateTime and throw an InvalidCastException or perform an implict conversion from the string value to a DateTime value using the inverse conversion as performed by the database upon insert. The latter provides consistent insert and select functionality.

It may be sufficient to use the InvariantCulture in the call to Convert.ToDateTime here, but I personally do not know how Firebase converts DateTime values to text. Additional tests may be required for other locale combinations to ensure correct functionality.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions