Skip to content

[V4] Adjustments to fix failing DateTimes asserts in tests due to local/UTC inconsistencies. #3601

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

Merged
merged 1 commit into from
Jan 9, 2025
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"core": {
"changeLogMessages": [
"Adjustments to fix failing DateTimes asserts in tests due to local/UTC inconsistencies."
],
"type": "patch",
"updateMinimum": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ public CredentialsRefreshState(ImmutableCredentials credentials, DateTime expira
internal bool IsExpiredWithin(TimeSpan preemptExpiryTime)
{
var now = AWSSDKUtils.CorrectedUtcNow;
var exp = Expiration;
var exp = Expiration.ToUniversalTime();
return now > exp - preemptExpiryTime;
}

internal TimeSpan GetTimeToLive(TimeSpan preemptExpiryTime)
{
var now = AWSSDKUtils.CorrectedUtcNow;
var exp = Expiration;
var exp = Expiration.ToUniversalTime();

return exp - now + preemptExpiryTime;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,10 @@ public void TestDateTimeDeserializationWithDdbContext()
{
//Arrange
var dateWithNoDecimals = "2022-05-05T11:56:11Z";
var expectedDateNoDecimal = DateTime.Parse(dateWithNoDecimals);
var expectedDateNoDecimal = DateTime.Parse(dateWithNoDecimals, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);

var dateWithDecimals = "2022-05-05T11:56:11.000Z";
var expectedDateDecimal = DateTime.Parse(dateWithDecimals);
var expectedDateDecimal = DateTime.Parse(dateWithDecimals, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);

var jsonDateWithNoDecimals = JsonMapper.ToJson(new
{
Expand Down
20 changes: 20 additions & 0 deletions sdk/test/UnitTests/Custom/Runtime/EC2InstanceMetadataServlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,30 @@ public EC2InstanceMetadataServlet()
new DisposableSwitch(
onStart: () => SetEC2InstanceMetadataEndpoint(ServiceURL),
onEnd: () => SetEC2InstanceMetadataEndpoint(currentEndpointUrl));

// JsonMapper.ToJson's DateTime writing exporter will output any date as a string formatted
// as a local or unspecified time. This changes it so the data is mocked as IMDS would
// actually return it which is "yyyy-MM-ddTHH:mm:ssZ" ending in a Z specified as UTC.
JsonMapper.RegisterExporter<DateTime>(
(DateTime date, JsonWriter writer) => {
if(date == DateTime.MinValue && date.Kind != DateTimeKind.Utc)
{
//Do not use .ToUniversalTime on a min datetime as it will adjust the hours.
DateTime.SpecifyKind(date, DateTimeKind.Utc);
}
else if(date.Kind != DateTimeKind.Utc)
{
date = date.ToUniversalTime();
}

writer.Write(date.ToString("yyyy-MM-ddTHH:mm:ssZ"));
}
);
}

public override void Dispose()
{
JsonMapper.UnregisterExporters();
_metadataServiceEndpointSwitch.Dispose();

ResetUseNullToken();
Expand Down
Loading