From 4b4407d337f2c8b3a51d55d8e93bb86af9231569 Mon Sep 17 00:00:00 2001 From: Sander ten Brinke Date: Thu, 15 Aug 2024 16:05:01 +0200 Subject: [PATCH 1/3] Use Long instead of int for epoch values outside of 2038 will silently be converted to its original value, in this case, a datetime. --- sdk/src/Services/DynamoDBv2/Custom/DocumentModel/Document.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/src/Services/DynamoDBv2/Custom/DocumentModel/Document.cs b/sdk/src/Services/DynamoDBv2/Custom/DocumentModel/Document.cs index dd306ce886de..44ba5fa83de1 100644 --- a/sdk/src/Services/DynamoDBv2/Custom/DocumentModel/Document.cs +++ b/sdk/src/Services/DynamoDBv2/Custom/DocumentModel/Document.cs @@ -254,7 +254,7 @@ internal static DynamoDBEntry EpochSecondsToDateTime(DynamoDBEntry entry, string // Converts a user-supplied DateTime-convertible DynamoDBEntry to epoch seconds stored in a Numeric Primitive. internal static DynamoDBEntry DateTimeToEpochSeconds(DynamoDBEntry entry, string attributeName) { - int? epochSeconds = null; + long? epochSeconds = null; try { var dateTime = entry.AsDateTime(); @@ -270,7 +270,7 @@ internal static DynamoDBEntry DateTimeToEpochSeconds(DynamoDBEntry entry, string if (epochSeconds.HasValue) { - entry = (Primitive)(epochSeconds.Value); + entry = (Primitive)epochSeconds.Value; } return entry; From 60c71901e85f4a564b33e33a6ff470c9f5bd82ba Mon Sep 17 00:00:00 2001 From: Sander ten Brinke Date: Thu, 15 Aug 2024 16:08:19 +0200 Subject: [PATCH 2/3] Update AWSSDKUtils to return a long instead of int --- sdk/src/Core/Amazon.Util/AWSSDKUtils.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/src/Core/Amazon.Util/AWSSDKUtils.cs b/sdk/src/Core/Amazon.Util/AWSSDKUtils.cs index 4e4d3ee207cd..629669c33f7a 100644 --- a/sdk/src/Core/Amazon.Util/AWSSDKUtils.cs +++ b/sdk/src/Core/Amazon.Util/AWSSDKUtils.cs @@ -722,9 +722,9 @@ public static DateTime ConvertFromUnixEpochMilliseconds(long milliseconds) return new DateTime(milliseconds * 10000L + EPOCH_START.Ticks, DateTimeKind.Utc).ToLocalTime(); } - public static int ConvertToUnixEpochSeconds(DateTime dateTime) + public static long ConvertToUnixEpochSeconds(DateTime dateTime) { - return Convert.ToInt32(GetTimeSpanInTicks(dateTime).TotalSeconds); + return Convert.ToInt64(GetTimeSpanInTicks(dateTime).TotalSeconds); } public static string ConvertToUnixEpochSecondsString(DateTime dateTime) From f9eac890aac86a24a590ce01d0072163268f2809 Mon Sep 17 00:00:00 2001 From: Sander ten Brinke Date: Thu, 15 Aug 2024 17:07:23 +0200 Subject: [PATCH 3/3] Use int64 -> string method and reset the code for the int method This prevents big API changes --- sdk/src/Core/Amazon.Util/AWSSDKUtils.cs | 4 ++-- .../Services/DynamoDBv2/Custom/DocumentModel/Document.cs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/sdk/src/Core/Amazon.Util/AWSSDKUtils.cs b/sdk/src/Core/Amazon.Util/AWSSDKUtils.cs index 629669c33f7a..4e4d3ee207cd 100644 --- a/sdk/src/Core/Amazon.Util/AWSSDKUtils.cs +++ b/sdk/src/Core/Amazon.Util/AWSSDKUtils.cs @@ -722,9 +722,9 @@ public static DateTime ConvertFromUnixEpochMilliseconds(long milliseconds) return new DateTime(milliseconds * 10000L + EPOCH_START.Ticks, DateTimeKind.Utc).ToLocalTime(); } - public static long ConvertToUnixEpochSeconds(DateTime dateTime) + public static int ConvertToUnixEpochSeconds(DateTime dateTime) { - return Convert.ToInt64(GetTimeSpanInTicks(dateTime).TotalSeconds); + return Convert.ToInt32(GetTimeSpanInTicks(dateTime).TotalSeconds); } public static string ConvertToUnixEpochSecondsString(DateTime dateTime) diff --git a/sdk/src/Services/DynamoDBv2/Custom/DocumentModel/Document.cs b/sdk/src/Services/DynamoDBv2/Custom/DocumentModel/Document.cs index 44ba5fa83de1..f5051a848726 100644 --- a/sdk/src/Services/DynamoDBv2/Custom/DocumentModel/Document.cs +++ b/sdk/src/Services/DynamoDBv2/Custom/DocumentModel/Document.cs @@ -254,11 +254,11 @@ internal static DynamoDBEntry EpochSecondsToDateTime(DynamoDBEntry entry, string // Converts a user-supplied DateTime-convertible DynamoDBEntry to epoch seconds stored in a Numeric Primitive. internal static DynamoDBEntry DateTimeToEpochSeconds(DynamoDBEntry entry, string attributeName) { - long? epochSeconds = null; + string epochSecondsAsString = null; try { var dateTime = entry.AsDateTime(); - epochSeconds = AWSSDKUtils.ConvertToUnixEpochSeconds(dateTime); + epochSecondsAsString = AWSSDKUtils.ConvertToUnixEpochSecondsString(dateTime); } catch (Exception e) { @@ -268,9 +268,9 @@ internal static DynamoDBEntry DateTimeToEpochSeconds(DynamoDBEntry entry, string attributeName, entry, e); } - if (epochSeconds.HasValue) + if (epochSecondsAsString != null) { - entry = (Primitive)epochSeconds.Value; + entry = new Primitive(epochSecondsAsString, saveAsNumeric: true); } return entry;