Skip to content

DateOnly is not supported #1156

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
Hugo-C opened this issue Jan 25, 2023 · 4 comments
Open

DateOnly is not supported #1156

Hugo-C opened this issue Jan 25, 2023 · 4 comments

Comments

@Hugo-C
Copy link

Hugo-C commented Jan 25, 2023

DateOnly, introduced in .NET 6 is not supported as it raises:

System.NotSupportedException : Don't know about System.DateOnly

I am interested in implementing it if I could get some pointers (maybe good starting points are #239 or #1012 ?) and the assurance it'll be reviewed.

@Bediruna
Copy link

else if (clrType == typeof(DateOnly)) { return "date"; }

in the SqlType method would fix this. But it needs to be in an updated library.

@DamianSuess
Copy link

DamianSuess commented Jan 5, 2025

Just ran into the same issue here as well. This may help the search engines find this issue report.

System.NotSupportedException: 'Don't know how to read System.DateOnly'

Example

CREATE TABLE User (
  [Id] INTEGER PRIMARY KEY NOT NULL,
  [Name] VARCHAR(25) NOT NULL,  -- Display name
  [DateOfBirth] DATE NULL);
public class User
{
  public int Id { get; set; }
  public string Name { get; set; }
  public DateOnly? DateOfBirth { get; set; }
}

public User? GetUser(int userId)
{
  var query = "SELECT Name, DateOfBirth FROM User WHERE Id = ?";
  var result = await Connection.QueryAsync<User>(query, userId);
  // ...
}

Similar Issues

@TheWatchfulOne
Copy link

Sqlite itself does not have a date data type. It supports text, integer, real, null, and blob. I suggest declaring your DateOfBirth column as text and storing an ISO date string. Alternatively, you could declare it as an integer and store Unix seconds (I think?) You can declare your column as a "date" but I think it will then store the data as a blob because it does not know what a date is. This page provides an in-depth explanation.

@DamianSuess
Copy link

DamianSuess commented Feb 22, 2025

It is possible to incorporate and convert as pointed out by the PRs in the previous comment. As a further example, the following is performed against, DateTime itself. This library would need to incorporate a similar converter for DateOnly and TimeOnly.

		public static string SqlType (TableMapping.Column p, bool storeDateTimeAsTicks, bool storeTimeSpanAsTicks)
		{
		//...
			else if (clrType == typeof (DateTime)) {
				return storeDateTimeAsTicks ? "bigint" : "datetime";
			}
		//...
		}

		// ...

		internal static void BindParameter (Sqlite3Statement stmt, int index, object value, bool storeDateTimeAsTicks, string dateTimeStringFormat, bool storeTimeSpanAsTicks)
		{
		//...
				else if (value is DateTime) {
					if (storeDateTimeAsTicks) {
						SQLite3.BindInt64 (stmt, index, ((DateTime)value).Ticks);
					}
					else {
						SQLite3.BindText (stmt, index, ((DateTime)value).ToString (dateTimeStringFormat, System.Globalization.CultureInfo.InvariantCulture), -1, NegativePointer);
					}
				}
		//...
		}

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants