Skip to content

Commit

Permalink
Merge pull request #214 from realm/yg-datetime
Browse files Browse the repository at this point in the history
Add support for DateTimeOffset properties
  • Loading branch information
fealebenpae committed Nov 23, 2015
2 parents ff1ef77 + 7853add commit c25c6c1
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 2 deletions.
2 changes: 1 addition & 1 deletion RealmNet.Shared/MarshalHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static IntPtr RealmColType(Type columnType)
return (IntPtr)9;
if (columnType == typeof(double))
return (IntPtr)10;
if (columnType == typeof(DateTime))
if (columnType == typeof(DateTimeOffset))
return (IntPtr)7;
if (columnType == typeof(bool))
return (IntPtr)1;
Expand Down
1 change: 1 addition & 0 deletions RealmNet.Shared/RealmNet.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<Compile Include="$(MSBuildThisFileDirectory)exceptions\RealmFileExistsException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)exceptions\RealmInvalidDatabaseException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)exceptions\RealmPermissionDeniedException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)extensions\DateTimeOffsetExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)GroupOpenMode.cs" />
<Compile Include="$(MSBuildThisFileDirectory)handles\Handled.cs" />
<Compile Include="$(MSBuildThisFileDirectory)handles\QueryHandle.cs" />
Expand Down
11 changes: 11 additions & 0 deletions RealmNet.Shared/RealmObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ protected T GetValue<T>(string propertyName)
var value = NativeTable.get_double(tableHandle, columnIndex, (IntPtr)rowIndex);
return (T)Convert.ChangeType(value, typeof(T));
}
if (typeof(T) == typeof(DateTimeOffset))
{
var unixTimeSeconds = NativeTable.get_datetime_seconds(tableHandle, columnIndex, (IntPtr)rowIndex);
var value = DateTimeOffsetExtensions.FromUnixTimeSeconds(unixTimeSeconds);
return (T)(object)value;
}
else
throw new Exception ("Unsupported type " + typeof(T).Name);
}
Expand Down Expand Up @@ -115,6 +121,11 @@ protected void SetValue<T>(string propertyName, T value)
double marshalledValue = Convert.ToDouble(value);
NativeTable.set_double(tableHandle, columnIndex, (IntPtr)rowIndex, marshalledValue);
}
else if (typeof(T) == typeof(DateTimeOffset))
{
Int64 marshalledValue = ((DateTimeOffset)(object)value).ToUnixTimeSeconds();
NativeTable.set_datetime_seconds(tableHandle, columnIndex, (IntPtr)rowIndex, marshalledValue);
}
else
throw new Exception ("Unsupported type " + typeof(T).Name);
}
Expand Down
19 changes: 19 additions & 0 deletions RealmNet.Shared/extensions/DateTimeOffsetExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.ComponentModel;

[EditorBrowsable(EditorBrowsableState.Never)]
internal static class DateTimeOffsetExtensions
{
private static readonly DateTimeOffset UnixEpoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);

internal static long ToUnixTimeSeconds(this DateTimeOffset @this)
{
return Convert.ToInt64((@this.ToUniversalTime() - UnixEpoch).TotalSeconds);
}

internal static DateTimeOffset FromUnixTimeSeconds(long seconds)
{
return UnixEpoch.AddSeconds(seconds);
}
}

6 changes: 6 additions & 0 deletions RealmNet.Shared/native/NativeTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ internal static extern IntPtr add_column(TableHandle tableHandle, IntPtr type,
[DllImport(InteropConfig.DLL_NAME, EntryPoint = "table_add_empty_row", CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr add_empty_row(TableHandle tableHandle);

[DllImport(InteropConfig.DLL_NAME, EntryPoint = "table_set_datetime_seconds", CallingConvention = CallingConvention.Cdecl)]
internal static extern void set_datetime_seconds(TableHandle tablePtr, IntPtr columnNdx, IntPtr rowNdx, Int64 value);

[DllImport(InteropConfig.DLL_NAME, EntryPoint = "table_get_datetime_seconds", CallingConvention = CallingConvention.Cdecl)]
internal static extern Int64 get_datetime_seconds(TableHandle handle, IntPtr columnIndex, IntPtr rowIndex);

[DllImport(InteropConfig.DLL_NAME, EntryPoint = "table_set_string", CallingConvention = CallingConvention.Cdecl)]
internal static extern void set_string(TableHandle tablePtr, IntPtr columnNdx, IntPtr rowNdx,
[MarshalAs(UnmanagedType.LPWStr)] string value, IntPtr valueLen);
Expand Down
2 changes: 1 addition & 1 deletion RealmNetWeaver/ModuleWeaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void Execute()
AddSetter(prop, columnName, genericSetValueReference); // with casting in the RealmObject methods, should just work
}
else if (prop.PropertyType.Namespace == "System"
&& (prop.PropertyType.IsPrimitive || prop.PropertyType.Name == "String"))
&& (prop.PropertyType.IsPrimitive || prop.PropertyType.Name == "String" || prop.PropertyType.Name == "DateTimeOffset"))
{
AddGetter(prop, columnName, genericGetValueReference);
AddSetter(prop, columnName, genericSetValueReference);
Expand Down
54 changes: 54 additions & 0 deletions Tests/IntegrationTests.Shared/DateTimeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using NUnit.Framework;
using RealmNet;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Linq;

namespace IntegrationTests.Shared
{
[TestFixture]
public class DateTimeTests
{
//TODO: this is ripe for refactoring across test fixture classes

protected string _databasePath;
protected Realm _realm;

[SetUp]
public void Setup()
{
_databasePath = Path.GetTempFileName();
_realm = Realm.GetInstance(_databasePath);
}

[TearDown]
public void TearDown()
{
_realm.Dispose();
}

[Test]
public void SetAndGetPropertyTest()
{
var turingsBirthday = new DateTimeOffset(1912, 6, 23, 0, 0, 0, TimeSpan.Zero);

Person turing;
using (var transaction = _realm.BeginWrite())
{
turing = _realm.CreateObject<Person>();
turing.FirstName = "Alan";
turing.LastName = "Turing";
turing.Birthday = turingsBirthday;
transaction.Commit();
}

// perform a db fetch
var turingAgain = _realm.All<Person>().ToList()[0];

Assert.That(turingAgain.Birthday, Is.EqualTo(turingsBirthday));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<Import_RootNamespace>IntegrationTests.Shared</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)DateTimeTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IntegrationTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PerformanceTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Person.cs" />
Expand Down
2 changes: 2 additions & 0 deletions Tests/IntegrationTests.Shared/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class Person : RealmObject
public double Latitude { get; set; }
public double Longitude { get; set; }

public DateTimeOffset Birthday { get; set; }

// Property that's not persisted in Realm
[Ignored]
public bool IsOnline { get; set; }
Expand Down
1 change: 1 addition & 0 deletions Tests/IntegrationTests.Win32/IntegrationTests.Win32.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Win32DateTimeTests.cs" />
<Compile Include="Win32IntegrationTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Win32PerformanceTests.cs" />
Expand Down
15 changes: 15 additions & 0 deletions Tests/IntegrationTests.Win32/Win32DateTimeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using IntegrationTests.Shared;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntegrationTests.Win32
{
[TestFixture]
public class Win32DateTimeTests : DateTimeTests
{
}
}
15 changes: 15 additions & 0 deletions wrappers/table_cs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ REALM_EXPORT size_t table_get_string(const Table* table_ptr, size_t column_ndx,
});
}

REALM_EXPORT int64_t table_get_datetime_seconds(const Table* table_ptr, size_t column_ndx, size_t row_ndx)
{
return handle_errors([&]() {
return table_ptr->get_datetime(column_ndx, row_ndx).get_datetime();
});
}

REALM_EXPORT void table_set_bool(Table* table_ptr, size_t column_ndx, size_t row_ndx, size_t value)
{
return handle_errors([&]() {
Expand Down Expand Up @@ -108,6 +115,14 @@ REALM_EXPORT void table_set_string(Table* table_ptr, size_t column_ndx, size_t r
});
}

REALM_EXPORT void table_set_datetime_seconds(Table* table_ptr, size_t column_ndx, size_t row_ndx, int64_t value)
{
return handle_errors([&]() {
DateTime dt(value);
table_ptr->set_datetime(column_ndx, row_ndx, dt);
});
}

REALM_EXPORT Query* table_where(Table* table_ptr)
{
return handle_errors([&]() {
Expand Down

0 comments on commit c25c6c1

Please sign in to comment.