Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

datetimeoffset as 10 bytes #933

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions src/SQLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2676,7 +2676,7 @@ public static string SqlType (TableMapping.Column p, bool storeDateTimeAsTicks,
return storeDateTimeAsTicks ? "bigint" : "datetime";
}
else if (clrType == typeof (DateTimeOffset)) {
return "bigint";
return "blob";
}
else if (clrType.GetTypeInfo ().IsEnum) {
if (p.StoreAsText)
Expand Down Expand Up @@ -3002,8 +3002,9 @@ internal static void BindParameter (Sqlite3Statement stmt, int index, object val
SQLite3.BindText (stmt, index, ((DateTime)value).ToString (dateTimeStringFormat, System.Globalization.CultureInfo.InvariantCulture), -1, NegativePointer);
}
}
else if (value is DateTimeOffset) {
SQLite3.BindInt64 (stmt, index, ((DateTimeOffset)value).UtcTicks);
else if (value is DateTimeOffset dto) {
var dtoAsBytes = DateTimeOffsetToBytes(dto);
SQLite3.BindBlob(stmt, index, dtoAsBytes, dtoAsBytes.Length, NegativePointer);
}
else if (value is byte[]) {
SQLite3.BindBlob (stmt, index, (byte[])value, ((byte[])value).Length, NegativePointer);
Expand Down Expand Up @@ -3037,7 +3038,21 @@ internal static void BindParameter (Sqlite3Statement stmt, int index, object val
}
}
}

static DateTimeOffset BytesToDateTimeOffset(byte[] bytes)
{
var date = BitConverter.ToInt64(bytes, 0);
var offset = BitConverter.ToInt16(bytes, 8);
return new DateTimeOffset(new DateTime(date), TimeSpan.FromMinutes(offset));
}
static byte[] DateTimeOffsetToBytes(DateTimeOffset dto)
{
var arr1 = BitConverter.GetBytes(dto.DateTime.Ticks);
var arr2 = BitConverter.GetBytes((short)dto.Offset.TotalMinutes);
var result = new byte[10];
Array.Copy(arr1, 0, result, 0, arr1.Length);
Array.Copy(arr2, 0, result, 8, arr2.Length);
return result;
}
class Binding
{
public string Name { get; set; }
Expand Down Expand Up @@ -3101,7 +3116,7 @@ object ReadCol (Sqlite3Statement stmt, int index, SQLite3.ColType type, Type clr
}
}
else if (clrType == typeof (DateTimeOffset)) {
return new DateTimeOffset (SQLite3.ColumnInt64 (stmt, index), TimeSpan.Zero);
return BytesToDateTimeOffset(SQLite3.ColumnByteArray(stmt, index));
}
else if (clrTypeInfo.IsEnum) {
if (type == SQLite3.ColType.Text) {
Expand Down Expand Up @@ -4379,7 +4394,7 @@ public static byte[] ColumnByteArray (Sqlite3Statement stmt, int index)
}
return new byte[0];
}

public static Result EnableLoadExtension (Sqlite3DatabaseHandle db, int onoff)
{
return (Result)Sqlite3.sqlite3_enable_load_extension (db, onoff);
Expand All @@ -4399,7 +4414,7 @@ public static ExtendedResult ExtendedErrCode (Sqlite3DatabaseHandle db)
{
return (ExtendedResult)Sqlite3.sqlite3_extended_errcode (db);
}

public static Sqlite3BackupHandle BackupInit (Sqlite3DatabaseHandle destDb, string destName, Sqlite3DatabaseHandle sourceDb, string sourceName)
{
return Sqlite3.sqlite3_backup_init (destDb, destName, sourceDb, sourceName);
Expand Down
6 changes: 3 additions & 3 deletions tests/DateTimeOffsetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace SQLite.Tests
public class DateTimeOffsetTest
{
class TestObj
{
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }

Expand Down Expand Up @@ -49,7 +49,7 @@ void TestAsyncDateTimeOffset (SQLiteAsyncConnection db)
// Ticks
//
o = new TestObj {
ModifiedTime = new DateTimeOffset (2012, 1, 14, 3, 2, 1, TimeSpan.Zero),
ModifiedTime = new DateTimeOffset (2012, 1, 14, 3, 2, 1, TimeSpan.FromMinutes(120)),
};
db.InsertAsync (o).Wait ();
o2 = db.GetAsync<TestObj> (o.Id).Result;
Expand All @@ -66,7 +66,7 @@ void TestDateTimeOffset (TestDb db)
// Ticks
//
o = new TestObj {
ModifiedTime = new DateTimeOffset (2012, 1, 14, 3, 2, 1, TimeSpan.Zero),
ModifiedTime = new DateTimeOffset (2012, 1, 14, 3, 2, 1, TimeSpan.FromMinutes(-120)),
};
db.Insert (o);
o2 = db.Get<TestObj> (o.Id);
Expand Down