diff --git a/Libraries/src/Amazon.Lambda.RuntimeSupport/Context/LambdaContext.cs b/Libraries/src/Amazon.Lambda.RuntimeSupport/Context/LambdaContext.cs index 26ba8aab4..99acb2355 100644 --- a/Libraries/src/Amazon.Lambda.RuntimeSupport/Context/LambdaContext.cs +++ b/Libraries/src/Amazon.Lambda.RuntimeSupport/Context/LambdaContext.cs @@ -21,7 +21,7 @@ namespace Amazon.Lambda.RuntimeSupport { internal class LambdaContext : ILambdaContext { - private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1); + private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); private LambdaEnvironment _lambdaEnvironment; private RuntimeApiHeaders _runtimeApiHeaders; @@ -69,6 +69,6 @@ public LambdaContext(RuntimeApiHeaders runtimeApiHeaders, LambdaEnvironment lamb public int MemoryLimitInMB => _memoryLimitInMB; - public TimeSpan RemainingTime => TimeSpan.FromMilliseconds(_deadlineMs - (DateTime.Now - UnixEpoch).TotalMilliseconds); + public TimeSpan RemainingTime => TimeSpan.FromMilliseconds(_deadlineMs - (DateTime.UtcNow - UnixEpoch).TotalMilliseconds); } } diff --git a/Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/Amazon.Lambda.RuntimeSupport.UnitTests/LambdaContextTests.cs b/Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/Amazon.Lambda.RuntimeSupport.UnitTests/LambdaContextTests.cs new file mode 100644 index 000000000..c134705f4 --- /dev/null +++ b/Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/Amazon.Lambda.RuntimeSupport.UnitTests/LambdaContextTests.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using Xunit; + +namespace Amazon.Lambda.RuntimeSupport.UnitTests +{ + public class LambdaContextTests + { + private readonly TestEnvironmentVariables _environmentVariables; + + public LambdaContextTests() + { + _environmentVariables = new TestEnvironmentVariables(); + } + + [Fact] + public void RemainingTimeIsPositive() + { + var deadline = DateTimeOffset.UtcNow.AddHours(1); + var deadlineMs = deadline.ToUnixTimeMilliseconds().ToString(CultureInfo.InvariantCulture); + + var headers = new Dictionary> + { + ["Lambda-Runtime-Aws-Request-Id"] = new[] { Guid.NewGuid().ToString() }, + ["Lambda-Runtime-Deadline-Ms"] = new[] { deadlineMs }, + ["Lambda-Runtime-Invoked-Function-Arn"] = new[] { "my-function-arn" } + }; + + var runtimeApiHeaders = new RuntimeApiHeaders(headers); + var lambdaEnvironment = new LambdaEnvironment(_environmentVariables); + + var context = new LambdaContext(runtimeApiHeaders, lambdaEnvironment); + + Assert.True(context.RemainingTime >= TimeSpan.Zero, $"Remaining time is not a positive value: {context.RemainingTime}"); + } + } +}