Skip to content

Commit 66adca2

Browse files
committed
set datetime kind to utc when timestamp is declared as a DateTime
1 parent e5ba98c commit 66adca2

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

src/TableStorage.Abstractions.TableEntityConverters/EntityConvert.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ internal static string GetPropertyNameFromExpression<T>(Expression<Func<T, objec
193193

194194
if (timestampProperty.PropertyType == typeof(DateTime))
195195
{
196-
timestampProperty.SetValue(o, entity.Timestamp.DateTime);
196+
timestampProperty.SetValue(o, entity.Timestamp.UtcDateTime);
197197
}
198198

199199
if (timestampProperty.PropertyType == typeof(string))

src/TableStorage.Abstractions.TableEntityConverters/TableStorage.Abstractions.TableEntityConverters.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<PackageProjectUrl>https://github.com/giometrix/TableStorage.Abstractions.TableEntityConverters</PackageProjectUrl>
1212
<RepositoryUrl>https://github.com/giometrix/TableStorage.Abstractions.TableEntityConverters</RepositoryUrl>
1313
<PackageTags>table-storage azure-table-storage poco table-entities</PackageTags>
14-
<PackageReleaseNotes>Allow for custom json serialization</PackageReleaseNotes>
14+
<PackageReleaseNotes>Datetime kind for timestamp (when declared as a datetime) is now utc instead of unspecified </PackageReleaseNotes>
1515
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1616
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
1717
<PackageLicenseExpression>MIT</PackageLicenseExpression>
@@ -22,7 +22,7 @@
2222
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
2323
<AssemblyVersion>1.3.2.0</AssemblyVersion>
2424
<Nullable>disable</Nullable>
25-
<PackageVersion>1.4.0.0</PackageVersion>
25+
<PackageVersion>1.4.1.0</PackageVersion>
2626
</PropertyGroup>
2727

2828
<ItemGroup>

src/TableStorage.Abstractions.UnitTests/Employee.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,9 @@ public class EmployeeWithTimestampAsString : Employee
3535
{
3636
public string Timestamp { get; set; }
3737
}
38+
39+
public class EmployeeWithTimestampAsDateTime : Employee
40+
{
41+
public DateTime Timestamp { get; set; }
42+
}
3843
}

src/TableStorage.Abstractions.UnitTests/EntityConvertTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,31 @@ public void convert_from_entity_table_with_timestamp_as_string()
9090
Assert.Equal(tableEntity.Timestamp.ToString(), employee.Timestamp);
9191
}
9292

93+
[Fact]
94+
public void convert_from_entity_table_with_timestamp_as_datetime()
95+
{
96+
var emp = new EmployeeWithTimestampAsString
97+
{
98+
Company = "Microsoft",
99+
Name = "John Smith",
100+
Department = new Department
101+
{
102+
Name = "QA",
103+
Id = 1,
104+
OptionalId = Guid.Parse("12ae85a4-7131-4e8c-af63-074b066412e0")
105+
},
106+
Id = 42,
107+
ExternalId = Guid.Parse("e3bf64f4-0537-495c-b3bf-148259d7ed36"),
108+
HireDate = DateTimeOffset.Parse("Thursday, January 31, 2008 ")
109+
};
110+
var tableEntity = emp.ToTableEntity(e => e.Company, e => e.Id);
111+
tableEntity.Timestamp = DateTime.UtcNow;
112+
var employee = tableEntity.FromTableEntity<EmployeeWithTimestampAsDateTime, string, int>(e => e.Company, e => e.Id);
113+
Assert.Equal(Guid.Parse("12ae85a4-7131-4e8c-af63-074b066412e0"), employee.Department.OptionalId);
114+
Assert.Equal(DateTimeKind.Utc, employee.Timestamp.Kind);
115+
Assert.Equal(tableEntity.Timestamp, employee.Timestamp);
116+
}
117+
93118
[Fact]
94119
public void convert_from_entity_table_complex_key()
95120
{

0 commit comments

Comments
 (0)