Skip to content

Commit

Permalink
Refactored code and updated test case based on review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishdhingra committed Feb 27, 2025
1 parent b7fb176 commit 5eba8fd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{
"core": {
"changeLogMessages": [
"Added method AWSSDKUtils.ConvertFromUnixLongEpochSeconds() for converting Unix epoch seconds to DateTime structure."
],
"type": "patch",
"updateMinimum": true
},
"services": [
{
"serviceName": "DynamoDBv2",
Expand Down
22 changes: 10 additions & 12 deletions sdk/src/Services/DynamoDBv2/Custom/DocumentModel/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,12 @@ internal static DynamoDBEntry DateTimeToEpochSeconds(DynamoDBEntry entry, string
// Differs from DateTimeToEpochSeconds by supporting dates after 2038.
internal static DynamoDBEntry DateTimeToEpochSecondsLong(DynamoDBEntry entry, string attributeName)
{
string epochSecondsAsString = null;
try
{
var dateTime = entry.AsDateTime();
epochSecondsAsString = AWSSDKUtils.ConvertToUnixEpochSecondsString(dateTime);
string epochSecondsAsString = AWSSDKUtils.ConvertToUnixEpochSecondsString(dateTime);
entry = new Primitive(epochSecondsAsString, saveAsNumeric: true);
return entry;
}
catch (Exception e)
{
Expand All @@ -327,16 +328,13 @@ internal static DynamoDBEntry DateTimeToEpochSecondsLong(DynamoDBEntry entry, st
attributeName, entry, e);

// To prevent the data from being (de)serialized incorrectly, we choose to throw the exception instead
// of swallowing it.
throw;
}

if (epochSecondsAsString != null)
{
entry = new Primitive(epochSecondsAsString, saveAsNumeric: true);
}

return entry;
// of swallowing it. This was a purposeful change from the existing DateTimeToEpochSeconds to avoid issues
// silently fallback to string format DateTime.
throw new AmazonDynamoDBException(
string.Format("Encountered error attempting to convert '{0}' with value '{1}' to epoch seconds.",
attributeName, entry.AsDateTime()),
e);
}
}

internal static Document FromAttributeMap(Dictionary<string, AttributeValue> data, IEnumerable<string> epochAttributes, IEnumerable<string> epochLongAttributes)
Expand Down
11 changes: 7 additions & 4 deletions sdk/test/Services/DynamoDBv2/IntegrationTests/DataModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,14 +354,15 @@ public void TestContext_CustomDateTimeConverter(bool retrieveDateTimeInUtc)
Context.ConverterCache.Add(typeof(DateTime), new DateTimeUtcConverter());

var currTime = DateTime.Now;
var currTimeBefore1970 = new DateTime(1969, 6, 12, 4, 56, 43, DateTimeKind.Local);
var longEpochTime = new DateTime(2039, 2, 5, 17, 49, 55, DateTimeKind.Local); // DateTime.Now returns Local kind.

var employee = new AnnotatedNumericEpochEmployee
{
Name = "Bob",
Age = 45,
CreationTime = currTime,
EpochDate2 = currTime,
EpochDate2 = currTimeBefore1970,
NonEpochDate1 = currTime,
NonEpochDate2 = currTime,
LongEpochDate = longEpochTime
Expand All @@ -372,13 +373,14 @@ public void TestContext_CustomDateTimeConverter(bool retrieveDateTimeInUtc)
// Since we are adding a custom DateTimeUtcConverter, the expected time will always be in the UTC time zone.
// regardless of RetrieveDateTimeInUtc value.
var expectedCurrTime = currTime.ToUniversalTime();
var expectedCurrTimeBefore1970 = currTimeBefore1970.ToUniversalTime();
var expectedLongEpochTime = longEpochTime.ToUniversalTime();

// Load
var storedEmployee = Context.Load<AnnotatedNumericEpochEmployee>(employee.CreationTime, employee.Name);
Assert.IsNotNull(storedEmployee);
ApproximatelyEqual(expectedCurrTime, storedEmployee.CreationTime);
ApproximatelyEqual(expectedCurrTime, storedEmployee.EpochDate2);
ApproximatelyEqual(expectedCurrTimeBefore1970, storedEmployee.EpochDate2);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate1);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate2);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate);
Expand All @@ -391,7 +393,7 @@ public void TestContext_CustomDateTimeConverter(bool retrieveDateTimeInUtc)
storedEmployee = Context.FromQuery<AnnotatedNumericEpochEmployee>(new QueryOperationConfig { Filter = filter }).First();
Assert.IsNotNull(storedEmployee);
ApproximatelyEqual(expectedCurrTime, storedEmployee.CreationTime);
ApproximatelyEqual(expectedCurrTime, storedEmployee.EpochDate2);
ApproximatelyEqual(expectedCurrTimeBefore1970, storedEmployee.EpochDate2);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate1);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate2);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate);
Expand All @@ -402,7 +404,7 @@ public void TestContext_CustomDateTimeConverter(bool retrieveDateTimeInUtc)
storedEmployee = Context.Scan<AnnotatedNumericEpochEmployee>().First();
Assert.IsNotNull(storedEmployee);
ApproximatelyEqual(expectedCurrTime, storedEmployee.CreationTime);
ApproximatelyEqual(expectedCurrTime, storedEmployee.EpochDate2);
ApproximatelyEqual(expectedCurrTimeBefore1970, storedEmployee.EpochDate2);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate1);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate2);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate);
Expand Down Expand Up @@ -2085,6 +2087,7 @@ public class EpochEmployee : Employee
[DynamoDBProperty(StoreAsEpoch = true)]
public virtual DateTime CreationTime { get; set; }

[DynamoDBProperty(StoreAsEpoch = true)]
public DateTime EpochDate2 { get; set; }

[DynamoDBProperty(StoreAsEpoch = false)]
Expand Down

0 comments on commit 5eba8fd

Please sign in to comment.