Skip to content

Commit 0f27567

Browse files
committed
Adjustments to fix failing DateTimes asserts in tests due to local/UTC inconsistencies.
1 parent a75cfac commit 0f27567

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"core": {
3+
"changeLogMessages": [
4+
"Adjustments to fix failing DateTimes asserts in tests due to local/UTC inconsistencies."
5+
],
6+
"type": "patch",
7+
"updateMinimum": true
8+
}
9+
}

sdk/src/Core/Amazon.Runtime/Credentials/RefreshingAWSCredentials.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ public CredentialsRefreshState(ImmutableCredentials credentials, DateTime expira
5252
internal bool IsExpiredWithin(TimeSpan preemptExpiryTime)
5353
{
5454
var now = AWSSDKUtils.CorrectedUtcNow;
55-
var exp = Expiration;
55+
var exp = Expiration.ToUniversalTime();
5656
return now > exp - preemptExpiryTime;
5757
}
5858

5959
internal TimeSpan GetTimeToLive(TimeSpan preemptExpiryTime)
6060
{
6161
var now = AWSSDKUtils.CorrectedUtcNow;
62-
var exp = Expiration;
62+
var exp = Expiration.ToUniversalTime();
6363

6464
return exp - now + preemptExpiryTime;
6565
}

sdk/test/Services/DynamoDBv2/UnitTests/Custom/DynamoDBTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,10 @@ public void TestDateTimeDeserializationWithDdbContext()
227227
{
228228
//Arrange
229229
var dateWithNoDecimals = "2022-05-05T11:56:11Z";
230-
var expectedDateNoDecimal = DateTime.Parse(dateWithNoDecimals);
230+
var expectedDateNoDecimal = DateTime.Parse(dateWithNoDecimals, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
231231

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

235235
var jsonDateWithNoDecimals = JsonMapper.ToJson(new
236236
{

sdk/test/UnitTests/Custom/Runtime/EC2InstanceMetadataServlet.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,30 @@ public EC2InstanceMetadataServlet()
2929
new DisposableSwitch(
3030
onStart: () => SetEC2InstanceMetadataEndpoint(ServiceURL),
3131
onEnd: () => SetEC2InstanceMetadataEndpoint(currentEndpointUrl));
32+
33+
// JsonMapper.ToJson's DateTime writing exporter will output any date as a string formatted
34+
// as a local or unspecified time. This changes it so the data is mocked as IMDS would
35+
// actually return it which is "yyyy-MM-ddTHH:mm:ssZ" ending in a Z specified as UTC.
36+
JsonMapper.RegisterExporter<DateTime>(
37+
(DateTime date, JsonWriter writer) => {
38+
if(date == DateTime.MinValue && date.Kind != DateTimeKind.Utc)
39+
{
40+
//Do not use .ToUniversalTime on a min datetime as it will adjust the hours.
41+
DateTime.SpecifyKind(date, DateTimeKind.Utc);
42+
}
43+
else if(date.Kind != DateTimeKind.Utc)
44+
{
45+
date = date.ToUniversalTime();
46+
}
47+
48+
writer.Write(date.ToString("yyyy-MM-ddTHH:mm:ssZ"));
49+
}
50+
);
3251
}
3352

3453
public override void Dispose()
3554
{
55+
JsonMapper.UnregisterExporters();
3656
_metadataServiceEndpointSwitch.Dispose();
3757

3858
ResetUseNullToken();

0 commit comments

Comments
 (0)